how to use list

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
mataanjin
Posts: 19
Joined: Tue May 25, 2010 3:07 am

how to use list

Post by mataanjin »

how do i use core::list ?
i want to make a list of bullet that has update method.
can i make a list like in vb where i just make an array of control and then i can use listName(i).update() ?
some kind like that?
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

Code: Select all

class SomeObjectOrSomething
{
 public:
 SomeObjectOrSomething()
 {
  static irr::u32 someNumber=0;
  someNumber++;
  mySomeNumber = someNumber;//Assign the object a number, this isn't actually important for the list so don't sweat about it.
 }
 void update() {printf("LOL IM AN OBJECT OR SOMETHING #%d",mySomeNumber);}
 private:
 irr::u32 mySomeNumber;
};

int main(int argc,char* argv[])
{
 irr::core::list<SomeObjectOrSomething*> objectsList;//< This is how you define the list
 
 //Make some objects.
 objectList.insert(new SomeObjectOrSomething());
 objectList.insert(new SomeObjectOrSomething());
 objectList.insert(new SomeObjectOrSomething());
 objectList.insert(new SomeObjectOrSomething());
 
 //Now loop through the objects and update them...
 irr::core::list<SomeObjectOrSomething*>::Iterator it = objectsList.begin();
 while(it!=objectsList.end())
 {
  SomeObjectOrSomething* object = *it;
  ++it;
  object->update();
 }
}
That should do it! Written mostly from memory, so might be a misspelling or some other typo.

EDIT: If you want to do it with specific indicies, then use an irr::core::array instead.
mataanjin
Posts: 19
Joined: Tue May 25, 2010 3:07 am

Post by mataanjin »

can you explain to me the work of this code?

Code: Select all

//Now loop through the objects and update them... 
 irr::core::list<SomeObjectOrSomething*>::Iterator it = objectsList.begin(); 
 while(it!=objectsList.end()) 
 { 
  SomeObjectOrSomething* object = *it; 
  ++it; 
  object->update(); 
 }
i'm confused.

and there is no insert in list, only insert after an before.
and i need to use a iterator?
or is it safe to use push_back?

and how to erase object?
my code is

Code: Select all

irr::core::list<tembak*>::Iterator it = Bullets.begin(); 
while(it!=Bullets.end()) 
{ 
tembak* object = *it; 
				
u32 r;
r = object->update(device->getTimer()->getTime()); 
if ( r == 2 )
	Bullets.erase(it);
++it; 
}
but it gives error

Code: Select all

First-chance exception at 0x012b32ab in KP Tank 3D.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x012b32ab in KP Tank 3D.exe: 0xC0000005: Access violation reading location 0x00000000.

TQ
CuteAlien
Admin
Posts: 9926
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Iterators get invalid when the list is changed. After r == 2 you are calling ++it for an iterator that points to an element that just got deleted. But erase returns an iterator to the following object, so that should work:

Code: Select all

u32 r= object->update(device->getTimer()->getTime());
if ( r == 2 )
   it = Bullets.erase(it);
else 
   ++it; 
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Imho Irrlicht's array is much better than list :wink:
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

i used to choose array instead of list as i find it's easier to use. what's the advantage of using list? anyone can explain?
mataanjin
Posts: 19
Joined: Tue May 25, 2010 3:07 am

Post by mataanjin »

CuteAlien wrote:Iterators get invalid when the list is changed. After r == 2 you are calling ++it for an iterator that points to an element that just got deleted. But erase returns an iterator to the following object, so that should work:

Code: Select all

u32 r= object->update(device->getTimer()->getTime());
if ( r == 2 )
   it = Bullets.erase(it);
else 
   ++it; 
nice
that's soled the problem. thanks.

@ent1ty and Virion
list is used when you want to have a dynamic memory i think, where you can delete any object inside the list and free memory.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

so can do array
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Insomniacp
Posts: 288
Joined: Wed Apr 16, 2008 1:45 am
Contact:

Post by Insomniacp »

a list lets you quickly add lots of data fast since you just are setting some pointers where as in an array there is an array so when adding to it if you go over size then it must create a new array and copy over all the data which take longer than just setting some pointer variables. Also deleting and inserting into a list is much quicker than array since arrays you have to push all data back a spot or move it all forward.
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

Sorry about that, I did mean push_back, it was a little late when I wrote that. The iterator is basically if you did a for loop to go through the elements of an array.

And what Insomniacp said about lists vs arrays sounds about right from what I knew too, Irrlicht uses the irr::core::list's for the scene manager, scene nodes, GUI environment, elements, etc to manage children structures.
Post Reply