Jump to content
  • Advertisement
Sign in to follow this  
pauls_1979

Scene Management Design Issues

This topic is 3348 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

I’m in the process of writing a new scene management system, and I've encountered a design issue which I was hoping someone out there might be able to help me with. When an entity is added to a scene I want to be able to store a pointer to that scene within the entity, so that the entity’s components can query the scene (for interactions, collision, lighting and so on). In previous systems I've be able to use a simple forward declaration like this:
// Forward declaration of scene class.
class Scene;

// Entity class.
class Entity
{
	private:

		Scene	*m_pScene;
};



In my new system I'm using smart pointers with intrusive ownership, and this makes the forward declaration method impossible because the compiler only lets you instantiate pointers or references to a forward declared class. I could get around this by storing a native pointer and manually incrementing/decrementing the scene's reference counter, but this feels like a last resort rather than a solution. Now I’m wondering whether there’s a better way of orgainising things. Would it be possible to create a system where you don’t even need to store a scene pointer within an entity? I don't want to go down the route of limiting myself to just one scene, but I can't think of another way in which an entity could find out which scene it belongs to. Any suggestions would be greatly appreciated, thanks in advance. [Edited by - pauls_1979 on October 15, 2009 11:26:49 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by pauls_1979
In my new system I'm using smart pointers with intrusive ownership, and this makes the forward declaration method impossible because the compiler only lets you instantiate pointers or references to a forward declared class.


But that's all you need. Did you try this:

// Forward declaration of scene class.
class Scene;

// Entity class.
class Entity
{
private:
SmartPtr<Scene> m_pScene;
};

Share this post


Link to post
Share on other sites
Do you really need a smart pointer to the scene in every entity (not saying you don't)? I don't know how your system works, but are scenes determined unused/removable when there are no entities in them? This seems like a strange scenario. Do you need the ownership aspects of the pointer in the Entity class?

Share this post


Link to post
Share on other sites
Thanks guys.

Quote:
Original post by Gage64
But that's all you need. Did you try this:


// Forward declaration of scene class.
class Scene;

// Entity class.
class Entity
{
private:
SmartPtr<Scene> m_pScene;
};


I have tried that yeah, but because the smart pointer uses intrusive ownership it needs to access the scene's reference counter (which unfortunately it doesn't know anything about because it's just a forward declaration). Does this mean that there's something wrong with my smart pointer class? I might give it a go with a good old fashioned boost::shared_ptr and see if that works.

Quote:
Original post by extralongpants
Do you really need a smart pointer to the scene in every entity (not saying you don't)? I don't know how your system works, but are scenes determined unused/removable when there are no entities in them? This seems like a strange scenario. Do you need the ownership aspects of the pointer in the Entity class?


You're right, I don't need a smart pointer inside the entity (like you say the ownership aspects aren't necessary), however I would at least like to be able to do something like this:


class Entity
{
public:

inline ScenePtr GetScene() { return(ScenePtr(m_pScene)); }

private:

Scene *m_pScene;
};









This would ensure that ownership outside the entity class is handled safely.

I'm just wondering if I need this pointer at all? Could anyone describe how other games/engines might handle situations such as the player querying the scene's collision system to find the height of the ground, or perhaps a camera searching to find the position of it's target? Could a simple messaging system be the solution?

Share this post


Link to post
Share on other sites
Quote:
Original post by pauls_1979
I would at least like to be able to do something like this:

*** Source Snippet Removed ***

This would ensure that ownership outside the entity class is handled safely.


I don't think it's worth the trouble. The caller of GetScene should know that it should not delete the pointer.

Actually, why do you need to expose the scene pointer?

Quote:
I'm just wondering if I need this pointer at all? Could anyone describe how other games/engines might handle situations such as the player querying the scene's collision system to find the height of the ground, or perhaps a camera searching to find the position of it's target. Could a simple entity messaging system be the solution?


If the entity has an Update function where it does all that stuff, you can pass the scene as an argument to that function.

This will also make it easier to support multiple scenes - you just pass the scene the entity is currently in.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
If the entity has an Update function where it does all that stuff, you can pass the scene as an argument to that function.

This will also make it easier to support multiple scenes - you just pass the scene the entity is currently in.


Well that is an extremely simple solution, but usually the simple solutions are also the best, so I might just give it a try. Entities themselves don't actually have an update function, but they are built from components, and I could probably pass a pointer to the scene as a parameter of any component functions that might need access to the scene. Of course I'm still open to any other suggestions, scene management is one of those topics that can be tackled from so many different angles and I'm interested in hearing how other people solve these problems.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!