Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

FunkyTune

Object destroying scene, thereby destroying itself

This topic is 5557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the game, a scene is the object that keeps track of the map, and of all the objects on it. When you call it''s destructor, it automatically destroys all associated objects. I have now created a "portal" object, which when you activate it, takes you to another place (another scene). However, by doing this, it destroys the current scene, and thereby destroys itself, which causes the program to crash. Could anyone help me on how I should design to avoid this? /John

Share this post


Link to post
Share on other sites
Advertisement
Maybe it could instead send a message or set a special set of variables that causes the game loop to destroy the scene and create the new one outside the scope of the object's behavior?

On a side note, I'm wondering if there is a OO design strategy that naturally finds this kind of problem, where an object can affect a parent, but depends on the parent for its existence.


[edited by - Waverider on April 2, 2003 3:27:46 PM]

Share this post


Link to post
Share on other sites
Well, why destroy the whole scene? Why not let the scene destroy the map and associated items, and then load a new map into the scene?

Share this post


Link to post
Share on other sites
quote:
Original post by NaliXL
Well, why destroy the whole scene? Why not let the scene destroy the map and associated items, and then load a new map into the scene?


The portal is one of those objects. That''s the problem.

OP: You''re likely going to have to do what Waverider said

Share this post


Link to post
Share on other sites
Maintain a pointer or reference to the current scene, not an actual object.

It seems to me here that we have a case of a failure to implement a copy constructor and assignment opperator for a type that allocates memory.

Golden Rule:
overload the assignment operator and implement a copy constructor in EVERY CLASS THAT ALLOCATES MEMORY IN ITS CONSTRUCOR AND RELEASES IT IN ITS DECONSTRUCTOR

Also, you probably don't want to be creating duplicates of your scenes because if you do what I said then you will be reallocating the entire scene. So put a debug output into the copy constructor so you can replace all those copys that are causing you to reallocate the entire scene with reference and pointer copys...



[edited by - snprbob86 on April 2, 2003 8:46:55 PM]

Share this post


Link to post
Share on other sites
Sounds to me like you need a garbage collection mechanism. Here''s the gist of my one (documented in the article I posted in this forum recently...)

Keep a list of all your game objects (derive them from a common class, and have that class''s constructor/destructor add/remove the class to/from the list).

Have the common base class include a flag which states whether the object is ''ready for collection'' or not.

Each game loop, go through the list and delete all objects which are flagged as ''ready for collection.'' Watch out for the possibility that you try and remove the object from the list twice, as the object destructor will remove the object from the list for you.

Make sure you have the destructor as virtual.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites
You have multiple scenes so you need something to manage the scenes. Rather than having the portal create and detroy scenes, have the scene manager do it. The portal is simply a marker that notifies the scene manager that a new scene needs to be created and the old scene needs to be destroyed. This way, you also avoid the problem of the implicit
delete this; 
.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!