I can't see any problems, but I just looking, I thought it was redundant to include the condition ( ! isHelmet00 ) with the else-if block.
Probably better to put a comment there instead. Otherwise you'd be wasting CPU cycles where you're doing logic checks on the obvious: What isn't isHelmet00 is going to be ! isHelmet00 anyway.
And, what exact line does it stop on?
What is the error?
The removeAll() call causes head_Head to drop all of its children. When this happens, the memory is freed back to the system, buy you still have a pointer to it [helmet01 and helmet00]. The next time through the loop the pointer gets dereferenced. You need to grab() scene nodes that you want to keep around after they've been removed from the scene graph. You might try something like this...
// this stuff is probably in some player class
core::array<scene::ISceneNode*> Helmets;
s32 Helmet; // init to -1
void addHelmet(scene::ISceneNode* helmet)
{
s32 found = Helmets.linear_search(helmet);
if (found != -1)
return;
Helmets.push_back(helmet);
helmet->grab();
}
void removeHelmet(scene::ISceneNode* helmet)
{
s32 found = Helmets.linear_search(helmet);
if (found == -1)
return;
Helmets.erase(found);
helmet->drop();
}
void removeAllHelmets()
{
u32 n;
for (n = 0; n < Helmets.size(); ++n)
Helmets[n]->drop();
Helmets.clear();
// user still has active helmet, you need to call pickNextHelmet
// so it will dissapear
}
void pickNextHelmet()
{
// remove selected helmet from head
if (Helmet != -1)
Helmets[Helmet]->remove();
// no helmets to choose from
if (Helmets.empty())
return;
// select a new one
Helmet = (Helmet + 1) % Helmets.size();
// put new one on head
Helmets[Helmet]->setParent(head_Head);
}
Travis
Last edited by vitek on Thu Aug 16, 2007 4:53 pm, edited 1 time in total.
Did you see the comment next to the declaration of Helmet? You need to set it to -1. I didn't do it myself because I don't know exactly how you are going to use the code. I would expect that you'd make Helmet and Helmets a member of a class and init them in the ctor, but you aren't doing that...
Works fine for me. You have a ctor that initialized head_Head to NULL, and then you have no way to set it to something valid. That could cause a crash. Also, you need a destructor that calls removeAllHelmets() so that they won't leak at program exit.