Jump to content

fully deleting objects


Rick
 Share

Recommended Posts

Delete an actor and null it in the main code by all means (whether it'd good practice or not is purely personal opinion), but make sure any other class using that specific actor has it's pointer nulled too.

Yep, never reference things that go out of scope. Whatever mechanisms you use to prevent this are entirely up to you.

 

The thing is I have a list of these actors in my Gameplay class (where they get created from originally) and in there it's looping through them each frame calling Update() and Draw() functions on them. If I delete the Projectiles pointer of the actor it'll work for the Projectile class but the Gameplay classes list of these will then have the same issue. Gameplay won't know that projectile delete it. So same issue just on the other side.

Just a concideration Rick but I don't loop and call instantiated game objects update functions directly at all but use an event driven system that each game object simply subscribes to and they get called automatically every time I raise the event in the game loop. This is done by having support in a gameObject base class that all game objects are derived from (in much the same way that mumbles was describing). When an object gets deleted its destructor unsubscribes itself from the event system ensuring it never gets called once its been destroyed. This way only the creating object (usually a manager type object) needs to maintain a list of pointers and is responsible for deleting any objects it manages, no other pointers are involved to complicate the issue. All access to the Manager owned objects is via the Manager although the Manager may pass pointers to other Manager objects to its siblings so they can call other Manager public functions as required.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Just a concideration Rick but I don't loop and call instantiated game objects update functions directly at all but use an event driven system that each game object simply subscribes to and they get called automatically every time I raise the event in the game loop.

 

I was playing around with that idea just to avoid having to do loops directly for each list of things I make. I like the idea and will probably change to that, but it would seem in the end it's the same thing just a different way of calling the Update method. You store your stuff in a manager class I put it in my Gameplay state class. At the end of the day some class has a list of objects. You can think of my Gameplay class as sort of the same as your Manager class. It's the only class that creates these objects and the only class that deletes them too.

 

The big question is when one object needs to "target" another how do you define that relationship? Then the bigger question is when 4 objects need to "target" the one single object and one of those 4 objects could cause that single object to be "killed" which triggers a deletion (again the Gameplay class is the only class that deletes things), how do you tell the other 3 that their "target" is now invalid? This continues to be an issue for me. Everything I try still leads to some awkward behavior or crashes.

 

My latest try was to give Actors an OnDeleted event. Just before calling delete on an Actor I fire this event. The Projectiles bind to this event of their target Actor when the projectile is created so that they can NULL out their pointer of this Actor as their target pointer.

 

This all stems from you can have 4 wizards all shooting their projectile at the 1 enemy, and one of those projectiles causes the enemy target to die and be deleted before the other projectiles are finished. In my projectiles I have a pivot and each frame I set the pivot location to the target model location. If the target is no longer valid it'll just stop setting the position but still move towards the pivot. Basically moving towards it's target's last known location. What does happen in certain situations is CRASH/BOOM/BANG smile.png

 

Would really love some alternative ideas on how to handle this.

Link to comment
Share on other sites

I was playing around with that idea just to avoid having to do loops directly for each list of things I make. I like the idea and will probably change to that, but it would seem in the end it's the same thing just a different way of calling the Update method. You store your stuff in a manager class I put it in my Gameplay state class. At the end of the day some class has a list of objects. You can think of my Gameplay class as sort of the same as your Manager class. It's the only class that creates these objects and the only class that deletes them too.

Yes, but you indicated that some of these pointers will be held by other classes too, which is where it all starts to go wrong!

 

The only point I was trying to make is never pass pointers if you can avoid it. If any object needs info on another object then don't pass it a pointer, pass it some unique reference and query its manager for the information, if it's manager's deleted it it will pass you a relevant return, if you call a pointer to something that no longer exists you're going to crash! I honestly can't remember the last time I had a crash using this type of access to data because the access is always via the object that knows if the data still exists or not.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

I switched over to a resource manager and access things via an int ID. I hate having to go through a proxy like that to get the actual object since it's just easier if I can use the pointer directly but the crashes are gone so I'll just have to change :)

 

Thanks Pixel.

Link to comment
Share on other sites

You're welcome. The tempation is always there to just pass and reference pointers directly but it gets more difficult to keep it all togther as the complexity grows. It's less efficient but a lot more robust to use a proxy.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

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