0
\$\begingroup\$

I am using C# and SFML.

I am having a problem understanding how to save a character in memory, so I can use the character in any game state.

Right now, the character is a class, and the player creates the character in the "Character Creation" GameState. However the actual variable is created in the GamePlayState class (see below).

class Character : IGameObject
{
    public Vector2f SpritePosition { get; set; }
    public Vector2f SpriteScale { get; set; }
    public string Name { get; set; }

However, I need all of the character data to be usable in any/all Game States.

I tried using a static game container (Dictionary, which held GameObjects- the player's character) but ran into some problems where I couldn't build. So I tried a different method to handle it.

I save all of my GameStates in a Dictionary which resides in the primary game loop.

stateSystem.AddState("Gameplay", new GameplayState(stateSystem));

I can call any specific GameState, but it acts as though the character (if created in Gameplay) cannot be access in other game states. The Game States can, however, access any other Game State.

For example, I tried to create the character in Gameplay - GameState

class GamePlayState : IGameObject
{

Character playerCharacter = new Character();

}

And in the class CharacterCreationState, I try to access that Character. It says "IGameObject does not contain a definition for playerCharacter".

Variable = stateSystem["Gameplay"].playerCharacter.variable;

I even created a function in my StateSYstem class which gets gameobjects.

        public IGameObject getGameObject(string stateID)
    {
        return _stateStore[stateID];
    }

This works to get the correct IGameObject, which would be the Gameplay State.

class GamePlayState : IGameObject
{

Character playerCharacter = new Character();

}

But once i have that IGameObject (the GamePlay Game State) I cannot grab the playerCharacter to manipulate it.

Perhaps I am just tired, but I am having a hard time figuring out how to do this without making the Character playerCharacter a global variable, creating in the main game loop.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Working from the framework you have now, can GamePlayState simply expose a public getPlayerCharacter() method? Then you can cast your IGameObject to a GamePlayState and call it. Something a bit like...

GamePlayState currentState = stateSystem.getGameObject("Gameplay") as GamePlayState;
if(currentState != null)
  PlayerCharacter playerCharacter = currentState.getPlayerCharacter();

If you want to change things up a bit, an approach I generally use is to have a singleton PlayerManager class, from which all game states (or anything else) can request any info they need about the player(s). Like:

PlayerCharacter player1Character = PlayerManager.GetInstance().GetLocalPlayer(1).GetActiveCharacter();
\$\endgroup\$
1
  • \$\begingroup\$ I figured it out, but this was pretty close to the solution I got before you posted. Thanks! \$\endgroup\$
    – user36200
    Dec 10, 2013 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .