Get data between separate classes

Hi!
I have 3 classes:
class A - player class
class B - scene class where A class located
class C - weapon class
The question is, how to transmit a position of player class (A) in game scene(B) to weapon class©?
With example code please.
Many thx to answers.

A few ways I can think of off the top of my head…

Have an explicit setPlayer method on class C

void Weapon::setPlayer(Player* player)
{
    CC_SAFE_RELEASE(_player);
    _player = player;
    CC_SAFE_RETAIN(player);
}

Or you could set it in the constructor…

class Weapon
{
public:

    Weapon(Player* player)
        : _player(player)
    {}
};

Or, you could have the player class have a static method that returns the current player.

Player* Player::_currentPlayer = nullptr;

Player* Player::getCurrentPlayer() const
{
    return _currentPlayer;
}

This method would be declared static in the header, and you would need a related setter as well. When you change the player you use Player::setCurrentPlayer(player); and you can use auto player = Player::getCurrentPlayer(); at anytime in any code to get the current player, just include Player.h

Justin

1 Like

I use a GameController class (singleton) for this sort of thing.

My GameController is instantiated in the AppDelegate and handles creating the scene, adding layers, creating sprites etc.

So the Game Controller has access to everything - now if my weapon needs to know where the player is it can ask the controller

GameController::getInstance()->getPlayerPosition();

I’m often lazy., and just make the Player object visible, so use

GameController::getInstance()->player->getPosition();

But I shudder whenever I do it :smile:

Maxxx can you write a code of GameController class and how it looks in AppDelegate, and how it handles creating the scene, adding layers, creating sprites.

Player* Player::_currentPlayer = nullptr;

Player* Player::getCurrentPlayer() const
{
return _currentPlayer;
}
When i try this method it’s crushed invalid use “_currrentPlayer” in static method function

Make _currentPlayer static as well.

I also tried to wrote static function in GameScene
Player* GameScene:: GetPlayer( )
{
return m_pPlayer;
}
but the same result - invalid use “m_pPlayer” in static method function.

mannewalis
can you explain maxxx method?

A singleton pattern is similar to a static method, except that the method returns the instance to the class.

class Singleton
{
public:

    Singleton* getInstance()
    {
        static Singleton* _instance = nullptr;
        if (nullptr == _instance)
            _instance = new Singleton;
        return _instance;
    }

Now you can access this class from anywhere after including the header.

auto foo = Singleton::getInstance()->someMethod();

Be careful how you use this, it’s ok for major workhorse classes like System or AudioManager etc. But you wouldn’t want to make all your scenes like this.

Also, be aware that this initializes lazily, i.e. it creates the class when you call it the first time, so don’t rely on it initializing before other things.

Ok, thank you very much.
Your method
Player* Player::_currentPlayer = nullptr;

Player* Player::getCurrentPlayer() const
{
return _currentPlayer;
}
works fine as usually.

1 Like

Here’s my GameController instantiation…

GameController* GameController::iInstance = NULL;


GameController::GameController()
{
}

GameController* GameController::getInstance()
{
	if (iInstance == NULL)
	{
		iInstance = new GameController();
		srand(static_cast <unsigned> (time(0))); // initialise random number generation

	}

	return iInstance;
}

in Appdelegate I have

auto gameController = GameController::getInstance();


gameController->StartGame();

at the end of ApplicationDidFinishLaunching

My startGame function initialises everything - loads data, finds which level I am on, etc. then calls the main menu.

By the way @Smasell - if you type an @ then start typing the persons name in the forums, it will link to their name - that way they are notified about the post!