Simple pointer problem [SOLVED]

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
TnTonly
Posts: 14
Joined: Mon May 12, 2008 5:14 pm
Location: Hanoi
Contact:

Simple pointer problem [SOLVED]

Post by TnTonly »

I created a simple function in order to add objects into the scene easier

Code: Select all

void addMesh(IMeshSceneNode* node, const path &meshFile, const path &texFile, vector3df pos)
{
    IMesh* mesh = smgr->getMesh(meshFile);
    if (!mesh) {
        device->drop();
        return 1;
    }
    node = smgr->addMeshSceneNode(mesh);
    if (node) {
        node->setMaterialTexture(0, driver->getTexture(texFile));
    }
    node->setPosition(pos);
return 0;
}
so I can add a new node into the scene like

Code: Select all

IMeshSceneNode* node;
addMesh(node, "sydney.md2", "sydney.bmp", vector3df(0,0,0));
And yes, the node can be added into the scene. However after this function the node pointer remains unchanged, which means I will get an error if I want to do anything with the node like node->setPosition(...)

What goes wrong here? :(
Last edited by TnTonly on Thu Feb 17, 2011 7:13 pm, edited 1 time in total.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

What does the console say? What does the debugger say? Are you sure the model is in the proper place (should be near the .exe in this case)? Are you reusing the same pointer?

Code: Select all

//#include <stdio.h> // for printf
IMeshSceneNode* addMeshNode(const path &meshFile, const path &texFile, const vector3df& pos)
{
    IMesh* mesh = smgr->getMesh(meshFile);
    if (!mesh) 
    {
        printf("Node creation failed - Could not load mesh!\n");
        return 0; // failed- leave this function
    }

    IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
    if (node) 
    {
        node->setMaterialTexture(0, driver->getTexture(texFile));
        node->setPosition(pos);
        printf("Created node!\n");
        
       return node; // succeeded
    }
    
     return 0; // failed
}



IMeshSceneNode* node = addMeshNode(node, "sydney.md2", "sydney.bmp", vector3df(0,0,0)); 

if (node)
{
    printf("YAY!\n");
}

PS: for a void function you can call "return;" to leave before reaching the last functiion line, not return 0; which is a value(and won`t compile on a sensible compiler). Calling it at the last line is pointless- don`t call anything.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
TnTonly
Posts: 14
Joined: Mon May 12, 2008 5:14 pm
Location: Hanoi
Contact:

Post by TnTonly »

I have modified my code following your instructions and it works fine now. I have (kind of) understood it a bit. Thanks a lot.

P/S: Sorry I meant to put "int" in the beginning instead of "void". Just a copy paste problem.
Mikhail9
Posts: 54
Joined: Mon Jun 29, 2009 8:41 am

Re: Simple pointer problem

Post by Mikhail9 »

TnTonly wrote:I created a simple function in order to add objects into the scene easier

Code: Select all

void addMesh(IMeshSceneNode* node, const path &meshFile, const path &texFile, vector3df pos)
The problem is that you are passing a copy of a variable to a function. That copy is being modified by the function, but you don't see those changes in the calling scope. You are just being confused by the fact that pointers are involved.

node is a variable holding a pointer to an IMeshSceneNode. You send a copy of that pointer to your function. It creates an IMeshSceneNode object, but it can't change your node variable, because it only has a copy.

Change your approach to something like this, if you want pointers:

Code: Select all

void addMesh(IMeshSceneNode** node, const path &meshFile, const path &texFile, vector3df pos)
{
...
*node = smgr->addMeshSceneNode(mesh);
...
}
which you call this way:

Code: Select all

addMesh(&node, "sydney.md2", "sydney.bmp", vector3df(0,0,0));
Or use a reference for node. I avoid this approach because it isn't obvious by looking at the function call that node might be changed by the function.
TnTonly
Posts: 14
Joined: Mon May 12, 2008 5:14 pm
Location: Hanoi
Contact:

Post by TnTonly »

@Mikhail9: Thanks, I got it now. :P
Post Reply