Jump to content

Model Copy() Loads Duplicates?

Recommended Posts

I'm trying to load a model as a copy instead of the default instance but for some reason it loads in twice.


	printf("entities before: %d\n",world->CountEntities());
	groundmodel=(Model *)Model::Load("Models/level/ground.mdl")->Copy(true);
//	groundmodel=(Model *)Model::Load("Models/level/ground.mdl");
	printf("entities after: %d\n",world->CountEntities());

However, if I comment out the Copy line and uncomment the one below it (instance load), it works as expected.  Is this a bug or am I doing something wrong?

Share this post

Link to post

For me this behaviour you described makes perfectly sense. The call to "Load" creates your first instance and the call to "Copy" creates your second instance by copying the first one. So if you could get a hand on your other model instance which comes from the same asset, you could go ahead and call other_instance->Copy(true) and you would get what you wanted.

Furthermore, the documentation of Load (https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_Model_Load) mentions that there is a second parameter called "flags" but it does not say, which ones are available. The description is simply "asset load parameters". That is something, you might want to add to the documentation @Josh

From the header file Asset.h, I find the following four constants defined, which I believe are those mentioned asset load parameters: Unmanaged, CreateNew, LoadQuiet and SkipTextures. You might want to give the following a shot (I haven't tried it myself):

Model::Load("Models/level/ground.mdl", Asset::CreateNew);
  • Upvote 1

Share this post

Link to post

My understanding of the function was that it would load a single model as a copy instead of an instance, so you could modify just that copy.  It makes no sense to me for it to load 2 copies into the groundmodel variable and that I then can't even interact with the first one (if I do groundmodel->Move, for example, it only moves the second loaded model).  If you do this multiple times, you'll have twice as many models as you need.  And this still doesn't explain why Release doesn't release both of them.  The number of entities keeps going up, every time I run that function, even though it should in theory clear both.

Edit: I'll try to see what happens if I do an entity copy instead of an entity load.  Thanks for the suggestion.

Share this post

Link to post

Thank you for the suggestion Ma-Shell.  The copy trick from an entity ended up working:

Model *temp=NULL;
temp=(Model *)Model::Load("Models/level/ground.mdl");
groundmodel=(Model *)temp->Copy(true);

It's weird to need to do this and hopefully Josh or someone else can also chime in on this but I guess I got my workaround for now.

Share this post

Link to post

That way makes total sense to me. With Load() you are creating the first instance and with Copy() you are copying that instance... Copying (as opposed to instancing) makes only sense if you already have an object using that model, so you can just use that one. Have you tried the suggestion using the "Asset::CreateNew"-parameter?

  • Upvote 1

Share this post

Link to post

Asset::CreateNew also works so far but when I last dealt with this I think I had some issue when I tried to assign different materials.  Only copy seemed to work for that.  I don't know if in that thread I didn't notice that I had an extra copy or what.  Thank you for the help!

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Create New...