irrArray problems

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
Kazooie
Posts: 13
Joined: Sat Jan 17, 2009 4:26 pm

irrArray problems

Post by Kazooie »

I'm having a couple problems with the irrArray class. In my project, I have a stats class, and a unit class. the unit stores a pointer to its own stats class. So to spawn a unit, I type this:

Code: Select all

CStats creepstats1=CStats(device, 102);
CUnit creepunit1=CUnit(device,&creepstats1);
unitlist.add(&creepunit1);
The unitlist is just a linked list which stores pointers to all the units in the game. I was thinking about making it store the actual data, but I thought it would be easier if I just used the built in irrArray class instead. So I made a creepspawn function which loops this code in certain intervals:

Code: Select all

statarray.push_back(CStats(device,101));
unitarray.push_back(CUnit(device,&statarray.getLast()));
unitlist.add(&unitarray.getLast());
The arrays are held in the main class, created like this:

Code: Select all

core::array<CUnit> unitarray;
core::array<CStats> statarray;
I can add one creep this way, but as soon as it loops again, the program can't access any of the data from the next unit created (resulting in a segmentation fault crash). From the debugger i can see that all the data is garbled.

Am I using the irrArray class correctly? Would it be better to have my Linked List UnitList class hold the stats and unit data directly? Thanks.
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

Well it seems that :
CUnit creepunit1=CUnit(device,&creepstats1);
unitlist.add(&creepunit1);
This is only temporary in that scope, as soon as that scope is over, the &creepunit1 becomes invalid..

Inside of the unitlist, keep an array rather, and inside that class, push back onto the list. I do it this way :

Inside of : CDataSystem::addObject(core::stringc& obj)

CDataObject newO = CDataObject();

newO.setObjectName(name);

if(addObject(newO))
return true;
Inside of addObject i have something like :

CDataSystem::addObject(CDataObject obj)
{
//mObjectlist is from CDataSystem
mObjectList.push_back(CDataObject(obj));

//notice the copy constructor used to create a local one
}


Im hoping my logic is right, i have used two different systems actually, but this one should work as you expect it to.
Kazooie
Posts: 13
Joined: Sat Jan 17, 2009 4:26 pm

Re: irrArray problems

Post by Kazooie »

Kazooie wrote:

Code: Select all

CStats creepstats1=CStats(device, 102);
CUnit creepunit1=CUnit(device,&creepstats1);
unitlist.add(&creepunit1);
This was just an example, as if it were right above the game loop in the main, so it would never go out of scope.

But your way does seem good, I'll have the UnitList class store the array data, maybe let it store both pointers and data. I still can't figure out why the arrays weren't working the way I had them in the first place though. Maybe it was the copy constructor thing. Hopefully your way works.
Kazooie
Posts: 13
Joined: Sat Jan 17, 2009 4:26 pm

Post by Kazooie »

For some reason, even with the method I said talked about trying, the irrArray had the same problem with losing the information after I added more than one member. I ended up expanding my UnitList linked list to hold the data, taking 3 days to rework everything, and now it's working =D. I still don't really know why the irrArray wasn't working, but it doesn't matter much now. Thanks for the advice on the unit data structures.
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

Weird, i also actually had that but it was my own bad implementation. I rewrote it entirely, and eventually moved onto std::map for the specifics i was doing.

Glad you got it working
Post Reply