Jump to content

Model Copy() Loads Duplicates?


gamecreator
 Share

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.

	if(groundmodel!=NULL)
	{
		groundmodel->Release();
		groundmodel=NULL;
	}

	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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
temp->Release();

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.

Link to comment
Share on other sites

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
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...