calling a function that creates a scenenode

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
Minotaur
Posts: 2
Joined: Tue Apr 04, 2006 3:21 pm

calling a function that creates a scenenode

Post by Minotaur »

this piece of script is taken from the int main

Code: Select all

IAnimatedMesh* mesh = smgr->getMesh("arrow.3ds");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
it all works well but when i try to replace it with a call to the create_mesh function it doesn't work :?

Code: Select all

create_mesh();
this is the create_mesh function :

Code: Select all

int create_mesh()

{
IAnimatedMesh* mesh = smgr->getMesh("arrow.3ds");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
return 0;
}
why doesn't it work? how can i make it work?
Ismarc
Posts: 6
Joined: Sun Apr 02, 2006 8:40 pm

Post by Ismarc »

What you did was move your mesh and node variables to be local to that function only. With that style of function, you would need to make smgr, mesh and node global. Another method would be to simply pass them as parameters.


With the variables global:

Code: Select all

int CreateMesh()
{
   mesh = smgr->getMesh("arrow.3ds");
   node = smgr->addAnimatedMeshSceneNode( mesh );
}
This is a (preferred) method to do it:

Code: Select all

IAnimatedMeshSceneNode* node CreateMesh(IAnimatedMesh* mesh, ISceneManager *smgr)
{
   node = smgr->addAnimatedSceneNode( mesh );
   return node;
}
I personally have seperated out the scene building portions of my code out into several classes, one of which adds the "actors" (people/vehicle shaped models). A quick massively snipped implementation (and probably won't compile as I'm creating this from memory):

Code: Select all

class MySceneLoader
{
   private:
      core::array<*IAnimatedMesh> mesh;
      core::array<*IAnimatedSceneNode> sceneNodes;
      scene::ISceneNodeManager *sceneMan;
   public:
      bool LoadMesh();
      bool LoadMesh( char * );
}

//Implementation
bool LoadMesh()
{
   bool succeed = LoadMesh("arrow.3ds");
   return succeed;
}

bool LoadMesh(char * filename)
{
   int meshSize = mesh->size();
   int sceneNodesSize = sceneNodes->size();
   mesh->push_back(sceneMan->getMesh(filename));
   if( meshSize < mesh->size() )
      sceneNodes->push_back(
         sceneMan->addAnimatedMeshSceneNode( mesh->last() );
   if( sceneNodesSize < sceneNodes->size() )
      return true;
   return false;
}
Please note that this is typed up during my lunch break at work, it's meant as a guide only, not cut and pastable code (as I probably screwed up namespaces or what Irrlicht's specific function name is or something).

Hopefully this is enough of an example to get you back on track. I highly suggest going to google and putting in "C++ tutorial scope" and fully understanding that before moving much further on. It shouldn't take a long time to fully grasp, but will save tons of headaches later on.
Minotaur
Posts: 2
Joined: Tue Apr 04, 2006 3:21 pm

Post by Minotaur »

wow, thank you a lot man.
i got it working now :)
Post Reply