how to use list
how to use list
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?
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?
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();
}
}
EDIT: If you want to do it with specific indicies, then use an irr::core::array instead.
can you explain to me the work of this code?
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
but it gives error
TQ
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();
}
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;
}
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
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Imho Irrlicht's array is much better than list 
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!
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!
niceCuteAlien 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;
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.
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!
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:
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.
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.
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.