Howdy,
the title of the submit is just not very to express so I’ll to be exact.I’ve the next code
// in gameGlobals.h
struct GameGlobals
{
public:
static Vector<cocos2d::Sprite*> enemies;
}
// in gameGlobals.cpp
Vector<Sprite*> GameGlobals::enemies;
Mainly I’ve a vector of tips to retailer the enemies that seem in recreation.
I’ve a category known as Enemy that push to that vector the sprite when an enemy is created. I printed out the dimensions of the vector after the push to test that it will increase. To this point so good.
Now I wish to use that vector (Vector<Sprite*> GameGlobals::enemies;), within the constructor of one other class. So if someplace it has been modifyied, say {that a} bullet has deleted an enemy from the vector,
the category will know. So I handed a vector paramater to the constructor:
// someClass.cpp
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
_enemies = enemies; // _enemies being a non-public variable within the header
// cocos2d::Vector<cocos2d::Sprite*> _enemies ;
}
// and I've this methodology too
void SomeClass::checkSizes()
{
CCLOG("%i <", _enemies.dimension());
CCLOG("%i <<<<", GameGlobals::enemies.dimension());
}
okey, so now within the helloWorld file I take a look at it. So I initiallize the category within the init. The init additionally has a scheduler that creates enemies each 5 seconds. So then within the replace methodology of HelloWorld i run SomeClass::checkSizes() and what I count on is that each sizes _enemies.dimension() and GameGlobals::enemies.dimension() are the identical since I’m passing that very same parameter. That is the code
bool HelloWorld::init()
{
//...
SomeClass::SomeClass(GameGlobals::enemies);
// ... then I've the scedule create enemy occasion
}
void HellowWorld::replace(float dt)
{
SomeClass::checkSizes();
}
However one thing when incorrect as a result of whereas the dimensions of GameGlobals::enemies retains rising as anticipated, the result’s that the dimensions of _enemies is all the time zero.
Why is that? Since I did this
bool HelloWorld::init()
{
SomeClass::SomeClass(GameGlobals::enemies)
}
and someClass.cpp I outlined it as
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
_enemies = enemies;
}
why is it not the identical? I’m certain it’s some foolish mistake however I can’t see it.
Thanks,
R
okey, hvind completed a little bit of analysis apparently it’s a extra advanced matter as a result of it’s about eradicating a component from a vector whereas iterating by it. I’ll submit an answer if I discover it in case it assist different.
You’re passing the vector by worth, as an alternative of by reference, which implies it’s making a brand new copy of it. So, any adjustments to the vector in SomeClass impacts its native copy, not the worldwide object.
Change it to this to see if it helps:
SomeClass::SomeClass(Vector<Sprite*>& enemies)
{
_enemies = enemies;
}
// the place _enemies is
Vector<Sprite*>& _enemies;
Thanks on your reply R101.
I did attempt that earlier than posting however It was crashing and I although that I used to be doing one thing incorrect. Prove the crash was coming from some place else, however you’re proper, I ought to was handed it reference .
Lengthy story brief, it’s about iterating the vector go by reference, and most significantly, having the ability to delete whereas looping as proven right here.
https://stackoverflow.com/questions/9927163/erase-element-in-vector-while-iterating-the-same-vector
cheers,
R