how to iterate through a list of objects

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
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

how to iterate through a list of objects

Post by humbrol »

I am trying to simplify the creation of objects in the game. for example the planets.

an i going about the right line of thought with this? the next step would be to save and read from a file/database the information and create the objects with the saved info then to use this loop to create the actual objects in game. and yes i know the loop below would spawn 100 different objects on the same coordinates but once i figure out how

Code: Select all

int planetObjects[100];
planetSpawn()
{
   i = 0;   
   while(i <= 100)
   {
         scene::ISceneNode* node[i] = smgr->addCubeSceneNode(); 

        if (node[i])
           {
              node[i]->setMaterialFlag(video::EMF_LIGHTING, false);
              node[i]->setMaterialTexture(0,driver->
                             getTexture("../.. /media /skydome.jpg"));
              node[i]->setScale(vector3df(.1,.1,.1));
              node[i]->setPosition(core::vector3df(100,100,300));
           }
       
   }
 
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

Well ... with the code you use you would do something like this:

Code: Select all

  for (u32 i=0; i<100; i++) {
    planetObjects->...;
  }
I prefer using a list for that. This way you are not stuck with 100 objects.

Code: Select all

  list<ISceneNode *> lPlanets;

  for (u23 i=0; i<100; i++) lPlanets.push_back(<create your planet here>);
you iterate through the list like this:

Code: Select all

  list<ISceneNode *>::Iterator it;
  
  for (it=lPlanets.begin(); it!=lPlanets.end(); it++) {
    ISceneNode *pPlanet=*it;
    <do something with pPlanet>
  }
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

so this would be functioning code?

Code: Select all

list<ISceneNode *> lPlanets; 
list<ISceneNode *>::Iterator it;

  
 
  for (it=lPlanets.begin(); it!=lPlanets.end(); it++) {
    ISceneNode *pPlanet=*it;
   
        if (pPlanet)
           { 
              

             // insert function to fill in info for the planet from a text file or db
             // ie pPlanet.xCoord, pPlanet.yCoord, pPlanet,zCoord,scale
            // texture to use, lighting info, rotation info etc.
        
              scene::ISceneNode* pPlanet = smgr->addCubeSceneNode();
              pPlanet->setMaterialFlag(video::EMF_LIGHTING, false);
              pPlanet->setMaterialTexture(0,driver->
                             getTexture("../.. /media /skydome.jpg"));
              pPlanet->setScale(vector3df(.1,.1,.1));
              pPlanet->setPosition(core::vector3df(100,100,300));
           } 
  } 
my question would be, how would i be able to reference each planet. my end goal is each planets info will be stored in a object based off a planet class, and do the same for NPC ships, objects in space, stations, etc am i approaching this the right way?
thanks for the all the help. been pouring over google and the likes all night
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

Your code doesn't make much sense. You iterate through a list of scene nodes and create a planet for each entry. Before you can iterate through your list you have to fill it (e.g. using lPlanets.push_back).

From what I understand from you second question you would like to have a list of all objects. I suggest you create a class that wraps the scene node and additional information, and add instances of this class to the list.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

my question then would be, i have the class for the planets, how do i put them into a list and access them from the list
Brainsaw
Posts: 1176
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

If you don't know how to use the lists I recommend reading some article on C++ programming which coveres templates. But:

Code: Select all

class myPlanetClass {
...
};

list<myPlanetClass *> lPlanets;

for (u32 i=0; i<10; i++) lPlanets.push_back(new myPlanetClass());

-------------------------

list<myPlanetClass *>::Iterator it;

for (it=lPlanets.begin(); it!=lPlanets.end(); it++) {
  myPlanetClass *pPlanet=*it;
  myPlanet->updateOrDoWhateverYouWantToDo();
}
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post by monchito »

A simple planet class with the list inside. This is is an exercise code to practice not a correct one.

Code: Select all

class CPlanets
{
private:
  core::list<scene::ISceneNode*> planet;
  core::list<scene::ISceneNode*>::Iterator loc;

public:
  CPlanets () {};
 ~CPlanets () { planet.clear(); };

  int  GetSize ( );
  void Insert ( scene::ISceneNode* node );
  bool Find( char* name, int* id );
  scene::ISceneNode* CPlanets::FindByName( const char* name );
  scene::ISceneNode* CPlanets::FindByID( const int id );
  void Update();
  void SetPosition( const char* name, core:: vector3df pos );
  // remove one element, etc...

};

int CPlanets::GetSize ( )
  {
     return planet.getSize();
  }

void CPlanets::Insert ( scene::ISceneNode* node )
  {
    if( node )
     planet.push_back( node );
  }

scene::ISceneNode* CPlanets::FindByName( const char* name )
  {
    for ( loc = planet.begin(); loc != planet.end(); loc++ )
    {
      if ( strcmp( name, (*loc)->getName() ) == 0 )
      {
       // do some stuff here...rotate, translate, modify etc
        return (*loc);
      }   
    } 
    return NULL;
  }

scene::ISceneNode* CPlanets::FindByID( const int id )
  {
    for ( loc = planet.begin(); loc != planet.end(); loc++ )
    {
      if ( (*loc)->getID() == id )
      {
       // do some stuff here...rotate, translate, modify etc
        return (*loc);
      }   
    } 
    return NULL;
  }
  
void CPlanets::SetPosition( const char* name, core:: vector3df pos )
{
   scene::ISceneNode* p = FindByName( name );
   if(p)
     p->setPosition( pos );
}

void CPlanets::Update()
  {
    for ( loc = planet.begin(); loc != planet.end(); loc++ )
    {
       // do some stuff here...rotate, translate, modify etc
       scene::ISceneNode *node = *loc;
    } 
  }

CPlanets* pPlanetManager = new CPlanets;

humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

thanks alot, thats got enough to keep me reading for a day or 2
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

brain freezeing up

Post by humbrol »

trying to figure out the basics , am i on the right track here?


Code: Select all

#include <irrlicht.h>
#include <fstream.h>
#include "moon.cpp"
#include <vector>
#include <string>


using namespace std;

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class Friend
{
    public:
        Friend();
        ~Friend(){}
        float xPos,yPos,zPos;
        int age;
        float height;
};
//implementations
Friend::Friend()
{
    xPos = 0;
    yPos = 0;
    zPos = 0;
}


int main(int argc, char** argv)
{

    IrrlichtDevice *device =
        createDevice(EDT_SOFTWARE, dimension2d<u32>(640, 480), 16,
            false, false, false, 0);


    device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();


    guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
        rect<int>(10,10,200,22), true);


    smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

    // Create a vector of Friend objects
   vector<Friend> list;
   float xPos;
   float yPos;
   float zPos;

   Friend *f1;

   for (int n=0; n<3; n++)
   {
        cout <<"Enter xPos :"<<endl;
         cin >> xPos;
        cout <<"Enter yPos :"<<endl;
        cin >> yPos;
        cout <<"Enter zPos :"<<endl;
        cin>> zPos;

        f1 = new Friend;
        f1->xPos = xPos;
         f1->yPos = yPos;
        f1->zPos = zPos;
        list.push_back(*f1);
        cin.get(); //clear buffer
   }

    // Now setup an iterator loop through the vector
   vector<Friend>::iterator it;

   for ( it = list.begin(); it != list.end(); ++it ) {
      // For each friend, print out their info
       float  x,y,z;
       x = it->xPos;
       y = it->yPos;
       z = it->zPos;
      scene::ISceneNode* it = smgr->addCubeSceneNode();
              it->setMaterialFlag(video::EMF_LIGHTING, false);
              it->setScale(vector3df(.1,.1,.1));
              it->setPosition(core::vector3df(x,y,z));
   }


    while(device->run())
    {

        driver->beginScene(true, true, SColor(0,200,200,200));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }


    device->drop();

    return 0;
}
Post Reply