History: Multiple Entities With A For Loop
Source of version: 2 (current)
Copy to clipboard
Here's a nice, quick way to add multiple entities and attach them to scene nodes by using a 1 for loop, and 2 arrays (1 for the entities, and 1 for the scene nodes).
Just add this to your function which creates the scene:
{CODE(wrap="1", colors="c++")}for (int i = 0; i < (sizeof(entity) / sizeof(entity[0])); i++) // Loop through the entities
{
// Since array elements start from 0, we add 1, so the entity and node names start from 1 :)
Ogre::String number = Ogre::StringConverter::toString(i + 1);
// Add the current element number to the entity/scene node name to avoid confusion
entity[i] = mSceneMgr->createEntity("Head " + number, "ogrehead.mesh");
node[i] = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node " + number);
// Distance the nodes from each other, so they aren't at the same place, and then attach them
node[i]->setPosition(i * 100 , 0, 0);
node[i]->attachObject(entity[i]);
// Let us know how many entities we have on screen, completely unnecessary
printf("Created Entity No. %i \n", i);
}{CODE}