• Create Account

### #Actualjohnmarinelli

Posted 27 January 2013 - 12:34 PM

Hi guys,

I'm programming a game with various game states.  I've created an Entity Manager class, and in that class I add all my objects.  However, I also create a special, static pointer to my Player.  This is for transferring the Player data between game states.

/*pseudocode*/

c_Entity_manager{
private:
static c_Player *player;

public:
static void addPlayer( c_Player *p ){ player = p; };
static c_Player* getPlayer(){return player;};
};

c_State1{
private:
c_Player*  state1_player;

public:
state1_player = c_Entity_manager::getPlayer();
};

c_State2{
private:
c_Player*  state2_player;

public:
state2_player = c_Entity_manager::getPlayer();
};

/*etc...*/


I'm almost certain this is considered a "singleton" pattern, and although I know that some argue that I should focus on whatever solves my problem and not worry too much about design, I know this is a common problem and I would like to break what could be a bad habit.

So, my questions are:
Is this considered a singleton pattern?  If not, is it an effective way of passing data between states?

If it is, what is an alternative way to transfer a persistent objects' (for example, a player) data  between states?

### #1johnmarinelli

Posted 27 January 2013 - 12:22 PM

Hi guys,

I'm programming a game with various game states.  I've created an Entity Manager class, and in that class I add all my objects.  However, I also create a special, static pointer to my Player.  This is for transferring the Player data between game states.

/*pseudocode*/

c_Entity_manager{
private:
static c_Player *player;

public:
static void addPlayer( c_Player *p ){ player = p; };
static c_Player* getPlayer(){return player;};
};

c_State1{
private:
c_Player*  state1_player;

public:
state1_player = c_Entity_manager::getPlayer();
};

c_State2{
private:
c_Player*  state2_player;

public:
state2_player = c_Entity_manager::getPlayer();
};

/*etc...*/


I'm almost certain this is considered a "singleton" pattern, and although I know that some argue that I should focus on whatever solves my problem and not worry too much about design, I know this is a common problem and I would like to break what could be a bad habit.

So, my questions are:
Is this considered a singleton pattern?

If it is, what is an alternative way to transfer a persistent objects'(for example, a player) data  between states?

PARTNERS