Jump to content

Delete User Data


Michael_J
 Share

Recommended Posts

Quick question:

 

If I do something like:

 

EntityData* entitydata = new EntityData;

model->SetUserData(entitydata);

 

Since I use a "new" do I have to "delete" the data before destroying the entity, or does the entity's destructor handle that for me?

 

Thanks smile.png

--"There is no spoon"

Link to comment
Share on other sites

Unless this changed generally you don't create an EntityDate object. SetUserData is generally a way for you to pass around any object. You should be able to cast anything to fit in that function so you can retrieve it later on and cast it back with GetUserData().

 

I use this inside classes often and pass 'this' pointer to it so my class and the LE entity (which is what LE is based on for everything) are then "linked" in a way so that I can get my class pointer from just the entity itself.

 

I found this useful for collisions. Collisions are handled with an LE callback (which is global to the app) where the entities in question are passed in. This breaks object oriented programming, so to bring it back I make a base class that has a virtual OnCollide(...) function. Then all my things I care about derive from this and inside it's ctor I do the entity->GetUserData((char*)this);. Then inside the 1 global collision function I cast the LE entities passed in to my base class after getting it's data and then call it's OnCollide(). This makes it so my classes can get the collisions and it brings back the object oriented approach to my projects.

  • Upvote 1
Link to comment
Share on other sites

Thanks, Josh--just checking if I had to do clean up or not.

 

Thanks for the extra info, Rick. Yeah, I already have collision callbacks working and was using the documented method to pass data. Being able to link them as you describe certainly streamlines things a bit smile.png

--"There is no spoon"

Link to comment
Share on other sites

If you have done 'new' you must always do 'delete'.

 

Without knowing the inner workings of any object (in this case model)

it should never delete any object attached to it. That would be really

bad design.

 

Understood, but if you look at the tutorial a delete is never done so obviously I wanted to verify...

 

edit: the tutorial in question, by the way: http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/object/objectsetuserdata-r22

  • Upvote 1

--"There is no spoon"

Link to comment
Share on other sites

(If the parameter were an Object pointer, I would have incremented the reference count, and decremented it when it was un-set. However, I decided to just use a void* so it can work with any data.)

My job is to make tools you love, with the features you want, and performance you can't live without.

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