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.