Michael_J Posted March 27, 2014 Share Posted March 27, 2014 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 Quote --"There is no spoon" Link to comment Share on other sites More sharing options...
Josh Posted March 27, 2014 Share Posted March 27, 2014 The command just sets a value. It does not attempt to manage the pointer in any way. Quote 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 More sharing options...
Rick Posted March 27, 2014 Share Posted March 27, 2014 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. 1 Quote Link to comment Share on other sites More sharing options...
Michael_J Posted March 27, 2014 Author Share Posted March 27, 2014 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 Quote --"There is no spoon" Link to comment Share on other sites More sharing options...
Roland Posted March 27, 2014 Share Posted March 27, 2014 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. 1 Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Michael_J Posted March 27, 2014 Author Share Posted March 27, 2014 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 1 Quote --"There is no spoon" Link to comment Share on other sites More sharing options...
Roland Posted March 27, 2014 Share Posted March 27, 2014 In that tutorial you will have a memory leak. Should be a 'delete' in the App destructor and a variable to hold the created object until you can delete it I can certainly understand why you asked. 1 Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Michael_J Posted March 27, 2014 Author Share Posted March 27, 2014 In that tutorial you will have a memory leak. Should be a 'delete' in the App destructor and a variable to hold the created object until you can delete it Precisely why I asked 1 Quote --"There is no spoon" Link to comment Share on other sites More sharing options...
Josh Posted March 27, 2014 Share Posted March 27, 2014 (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.) Quote 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 More sharing options...
Recommended Posts
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.