Sign in to follow this  

Nodes accessing scene

This topic is 4733 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 have several nodes communicating with a scene class (nodes and scene contained within a container class). i have sorted a temporary solution up for node<->scene communication... and its a bad one
        static Scene *Instance() { return scene_acc; }
    private:
        /*stores the latest instance of Scene and is initialized
          in the constructor with this*/
        static Scene *scene_acc;


The only solution i can think of is having the container class passing a const pointer on initialization of the nodes which is stored inside the classes.

Can anyone think of a better method to achieve this?

Share this post


Link to post
Share on other sites
I think scene will have a list of nodes and each node will have a pointer to the scene. So if you want to comunicate with a node scene will find the node in her list; if a node wants to comunicate to its scene, it has the pointer ready.

To initialize pointer, you'll have a method Add in your scene:


void Scene::Add(Node * pNode)
{
/* <- add pNode in the Node list -> */

pNode->SetScene(this);
}

void Node::SetScene(Scene * pScene)
{
m_pScene=pScene;
}



Hope that's help.

Share this post


Link to post
Share on other sites

erm... thanks :P


i sorta meant that the communication is one way, scene is basically a scene graph, so objects communicate with the subsystem through it.

This means there has to be a method of communication between scene and objects contained within the container class, im thinking of breaking the biggest rule and just making scene static/global (passing references would be hell, as functions would have to carry multiple pointers into deep levels). Thats why i asked in case anyone had any better ideas.


Thanks

Share this post


Link to post
Share on other sites
Ok, I try another answer then. [embarrass]

Your Scene is a Singleton (it has the method Instance()). Nodes must access this class and your problem is in letting them know the value of the scene pointer.
If that is correct I cannot understand where the problem is. You can get the scene pointer everywhere in your source doing something like that:

Scene::Instance()->SomeMethod();

I usually initialize the singleton that way:

Scene * Scene::scene_acc=NULL;
(you have this line of code in your Scene implementation file)

and Scene::Instance() looks like this:


Scene * Scene::Instance()
{
if (scene_acc==NULL)
scene_acc=new Scene();
return scene_acc;
}



(remember to destroy that instance to avoid memory leaks ;-) )

If that not answer your question (sorry for that), please try to explain better who is the container class that contains both nodes and scene. :)

Share this post


Link to post
Share on other sites
Ooops![oh]
When I have classes communicating like your classes, I use singleton and other pointer as you do. I think is a good solution, not a bad one.[lol]

Share this post


Link to post
Share on other sites

This topic is 4733 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this