Thursday, June 30, 2022
HomeGame DevelopmentAttempting to reference a vector of sprites - C++

Attempting to reference a vector of sprites – C++


Howdy,
the title of the submit is just not very to specific 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 verify 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 technique too
void SomeClass::checkSizes()
{
        CCLOG("%i <", _enemies.dimension());
		CCLOG("%i <<<<", GameGlobals::enemies.dimension());
}

okey, so now within the helloWorld file I check 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 technique 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 flawed as a result of whereas the dimensions of GameGlobals::enemies retains growing 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 accomplished a little bit of analysis apparently it’s a extra complicated subject as a result of it’s about eradicating a component from a vector whereas iterating by way of it. I’ll submit an answer if I discover it in case it assist different.

You’re passing the vector by worth, as a substitute 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;

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments