Page 1 of 2

Re. Creating an array of mesh nodes is it possible

Posted: Wed Feb 08, 2006 10:23 pm
by DGENXP
Hi I am trying to creat an array of scene nodes as in name[amt] but i cannot get this working

the reason i want this is because i am working on an aplication which will generate alot of these models

if there is any ideas i would like to know - I did see that somebody has done this on the forums but he has not released his code to look at

Cheers

Posted: Wed Feb 08, 2006 11:20 pm
by Baal Cadar
What exactly were you trying? (Code snippet) And what exactly was the error you were getting? (Exact compiler messages)
Also state your compilation environment and used Irrlicht version.

You might consider using irr::core::array or std::vector or some other container instead of C style arrays. They are *much* easier to use, dynamically growing and just as fast.

Posted: Wed Feb 08, 2006 11:36 pm
by DGENXP
Hi I hope this helps

when i try this it crashes before it even starts i have tried another method but this just places the object without a texture

I am using Dev C++ and IRR Version 0.12.0 with directX Support

Code: Select all

	IAnimatedMesh* mesh = smgr->getMesh("Objects/floor.x");
	IAnimatedMeshSceneNode* node[1] = {smgr->addAnimatedMeshSceneNode( mesh )};
	

	if (node[1])
	{
		node[1]->setMaterialFlag(EMF_LIGHTING, false);
		node[1]->setPosition(vector3df(128-256,0,78));
	//	node->setFrameLoop(0, 310);
		node[1]->setMaterialTexture( 0, driver->getTexture("Objects/Floor09.jpg") );
	}
I shall look forward to reading the solution

Posted: Wed Feb 08, 2006 11:39 pm
by bitplane
no need for the curly braces.

Posted: Wed Feb 08, 2006 11:40 pm
by DGENXP
bitplane wrote:no need for the curly braces.
I have tried it without and it does not compile

it says invalid initializer

Posted: Wed Feb 08, 2006 11:43 pm
by ElementoY
Arrays in C++ start from index 0, not 1, so an array with size 1 would have only the index 0, so to access it you'll need to change all references to node[1] to node[0] .
Hope it helped! :)

Posted: Wed Feb 08, 2006 11:47 pm
by ElementoY
This would be the fixed-up code:

Code: Select all

	IAnimatedMesh* mesh = smgr->getMesh("Objects/floor.x");
	IAnimatedMeshSceneNode* node[1];
        node[0] = smgr->addAnimatedMeshSceneNode( mesh ) ; // no need for braces here
	

	if (node[0])
	{
		node[0]->setMaterialFlag(EMF_LIGHTING, false);
		node[0]->setPosition(vector3df(128-256,0,78));
	//	node[0]->setFrameLoop(0, 310);
		node[0]->setMaterialTexture( 0, driver->getTexture("Objects/Floor09.jpg") );
	}

Posted: Wed Feb 08, 2006 11:49 pm
by DGENXP
It worked at the 0 point but when i tried to add more it crashes

Posted: Wed Feb 08, 2006 11:52 pm
by ElementoY
Try giving the array more elements, like in:
IAnimatedMeshSceneNode* node[x];
where x is the number of elements you want in the array.

Posted: Wed Feb 08, 2006 11:56 pm
by DGENXP
Hmm nope it still crashes

Posted: Wed Feb 08, 2006 11:57 pm
by DGENXP
Hmm nope it still crashes

Posted: Wed Feb 08, 2006 11:57 pm
by bitplane
d'oh! like Baal said, you should try using core::array instead because its much easier to use. I thought creating an array like x[3] = {a,b,c} makes constants, i dunno, anyway they're awkward and you cant 'redim' them.

Code: Select all

core::array<scene::ISceneNode*> aNodes;
aNodes.set_used(total);
aNodes[0] = smgr->addFooSceneNode();
...
aNodes[total-1] = smgr->addFooSceneNode();

Posted: Wed Feb 08, 2006 11:57 pm
by Baal Cadar
Or using std::vector (would be similiar with irr::core::array)

Code: Select all

#include <vector>
using namespace std;

//...snip...
IAnimatedMesh* mesh = smgr->getMesh("Objects/floor.x");

vector<IAnimatedMeshSceneNode*> nodes;
// Creating five of them:
for (int i = 0; i < 5; ++i)
{
   nodes.push_back(smgr->addAnimatedMeshSceneNode(mesh));
}
   
// Initialise the created nodes
for (size_t i = 0; i < nodes.size(); ++i)
{
      nodes[i]->setMaterialFlag(EMF_LIGHTING, false);
      nodes[i]->setPosition(vector3df(128-256,0,78));
      nodes[i]->setMaterialTexture( 0, driver->getTexture("Objects/Floor09.jpg"));
}
Using C style arrays in this context is just inflexible and troublesome. Prefer full functional containers like STL containers or the ones Irrlicht provides.

Posted: Thu Feb 09, 2006 12:03 am
by ElementoY
Baal Cadar wrote:Using C style arrays in this context is just inflexible and troublesome. Prefer full functional containers like STL containers or the ones Irrlicht provides.
True indeed! I forgot about Irrlicht's containers... :?

Posted: Thu Feb 09, 2006 12:15 am
by afecelis
and... where is the array part?