Jump to content

Copy / instance or prefab


wildcherrii
 Share

Recommended Posts

My project has a Main.lua which acts as the controller for the game loop.
I have a import("scripts/pre-cache.lua") which I use to load up imgs, sounds and other prefabs that each and every map uses. 

But theres a serious flaw.  For example

This is the pre-cache.lua that is imported from the main.lua script. It's attached to no entity or map object, it just holds the assets in a table for when they are needed.

debris ={}
debris.box = Model:Load("Models/Props/Land/box_01.mdl")  

Now, I have a box entity that is placed on the map via the editor, a simple script attached to it. Within that script I have this code - 

function Script:Start()

            self.de=debris.box:Copy()
            self.de:Show()
            

Everything is fine and dandy when I run the map through, but when I go to change maps world:clear() and a map:load()  things start to fall apart.

The code works fine for a few times,  I'm able to make a few additional copies of the box but then crash.. everythign freezes up and the window eventually closes. No error, even running in debug.

 

This isn't the only thing that causes this error. If i create a pivot() or copy a snd, or worest yet - copy a prefabbed entity over, i get random lock ups that follow no pattern.

To conveince myself this isnt an error on my part, i created a simple small project to test this and sure enough it does the same thing.  

 

Is copying/instancing global objects by map loaded entity scripts a NONO ?

 

 

 

 

Link to comment
Share on other sites

I had a similar ordeal happen to me as well. Trust me, it's not the engine, I fought an answer for weeks, eventually trying EVERYTHING imaginable in hopes I'd come across the cause, now I'm not saying this is your problem, but it's worth checking out.

I have a routine that Instances a prefabbed model as well, but I add the script in code based on the situation happening , well one of my logic scripts was placing a 3d sound on the entity that was being detached: but I needed that sound so I was going to be smart and create a pivot in the detach code and place it at the position the original entity was being Detached::  Come to find out  even though that pivot was being created on a global scope ( globally defined Table() outside of the Object),  it still had ties to the Object that originally created it, this object was destroyed.  It would never error on this, it waited until I changed maps then very unpredictable things would happen ( including some of the stuff you mentioned above, well actually all of that stuff ). 

After pinpointing the cause ( mind you that was a bout a 2 to 3 week debug ) I fixed it very easily, I created a personal Garbage Collector, so instead of immediately deleting and/or detaching the Object, I sent it to a GC table with a 5000 millisecs() timer attach to it.  This was enough time for all sounds, particle emitters or whatever to play out and Detach on their own freeing the original Object up. GC caught that it had expired and deleted the Object. So nothing lingered around and no memory leaks. Problem was solved.

Hope this helps in some way, I know good and well how frustrating it can get trying to track done stuff like that.  I've since better constructed any and every Object I add to my game these days.

Link to comment
Share on other sites

The world clears every entity in memory tied to it. You can load your model in the start function and then hide it. Also Copy() is slow. You should use Instance() if you're not going to change the model's material.

It's ok for Model:Load() to load the same model multiple times in multiple scripts.The engine will just instance existing models in memory. 

if you think it's inefficient for the engine to be unloading and reloading assets you know will be in every instance of your game, what you can do is precache your commonly used assets in a completely different world to keep it in memory. You would just rely on Model:Load() to make a new instance.

You would do this by making the "junkyard" world before your main world, load your common models and then create your game world along with using World:SetCurrent() to ensure your game world is current.

Memory management is really confusing to figure out as the engine tries to do everything for you without it being clear unless you ask Josh directly. This should be resolved in Ultra Engine with the use of smart pointers.

I should probably write something up on this even though Leadwerks will be shadowed by the new engine within the upcoming months.

  • Like 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...