Retrieving Pointer From smgr->AddMeshSceneNode call

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
so1odo1o
Posts: 32
Joined: Sat Apr 03, 2010 10:29 am

Retrieving Pointer From smgr->AddMeshSceneNode call

Post by so1odo1o »

I currently have a class that holds a pointer to an IMeshSceneNode, found below:

Code: Select all

class GraphNode
{
public:
	GraphNode();
	scene::IMeshSceneNode* node;
};
The problem is that when I create a GraphNode and do something like this:

Code: Select all

GraphNode a;
a.node = smgr->addMeshSceneNode(//node properties..);
and then try something like:

Code: Select all

cout << a.node->getPosition().X << endl;
This fails at runtime. After looking through the a.node properties, it doesn't seem to hold the information for the node that was added. Am I doing something wrong here? Please let me know. Thank you.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

can you post a small app that shows the issue?
so1odo1o
Posts: 32
Joined: Sat Apr 03, 2010 10:29 am

Post by so1odo1o »

The program that I have is a bit long. But if I had created a class that holds a pointer to an IAnimatedMeshScene, shouldn't the above code work? I set the pointer in my class to the pointer that is returned from the smgr above.

When I run the program, it says in the debugger that the pointer node in my class is 0xcccccccc which makes the program crash.

Weird. The code below works but not the one outside the loop:

Code: Select all

	for(int i = 0; i < num; i++)
	{
		GraphNode gn = nodes.at(i);
		gn.node = smgr->addMeshSceneNode(meshes[0], 0, gn._id, core::vector3df(gn._x,gn._y,gn._z));
		cout << gn.node->getPosition().X << endl;
	}

	cout << nodes.at(1).node->getPosition().X << endl;
Last edited by so1odo1o on Fri May 14, 2010 12:40 am, edited 1 time in total.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Are you passing those graphnodes around, causing them to be copied, without having a default value for node or a copy constructor?
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

so1odo1o wrote: Weird. The code below works but not the one outside the loop:

Code: Select all

	for(int i = 0; i < num; i++)
	{
		GraphNode gn = nodes.at(i);
		gn.node = smgr->addMeshSceneNode(meshes[0], 0, gn._id, core::vector3df(gn._x,gn._y,gn._z));
		cout << gn.node->getPosition().X << endl;
	}

	cout << nodes.at(1).node->getPosition().X << endl;
Actually, it is not weird at all.

Code: Select all

GraphNode gn = nodes.at(i);
gn.node = smgr->addMeshSceneNode(...);
The first line creates a local copy of a GraphNode, and the second line updates it. When the loop iteration is finished, the copy goes out of scope and is destroyed. The original item in the collection is not updated. Then, when you get outside the loop, you try to access the element at index 1. Assuming that there are at least two elements in the collection, your code doesn't work because the collection was not updated inside the loop.

The easy thing to do is to use a reference to the element in the node collection instead of making a copy of the element.

Code: Select all

GraphNode& gn = nodes.at(i);
gn.node = smgr->addMeshSceneNode(...);
Travis
Post Reply