Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store game-wide variables

Tags:

c#

enums

2d-games

I have an options Screen for things like Difficulty, Resolution, Full Screen, etc but I'm struggling to find the best way to store/obtain these variables at game time.

The way I've currently decided to do it is to create a 'Constants' class which contains all the GameOption enums. But how do I choose a default for all of these options, and how do I get the currently selected enum?

Especially with that of the resolution - I've decided to store the values but unsure how to get the default or currently stored value?

namespace V1.test.RPG
{
    public class GameOptions
    {
        public enum Difficulty
        {
            EASY,
            MEDIUM,
            HARD
        }

        public enum Sound
        {
            ON,
            QUIET,
            OFF
        }

        public enum Music
        {
            ON,
            QUIET,
            OFF
        }

        public enum ResolutionWidth
        {
            SMALL = 1280,
            MEDIUM = 1366,
            LARGE = 1920,
            WIDESCREEN = 2560
        }

        public enum ResolutionHeight
        {
            SMALL = 800,
            MEDIUM = 768,
            LARGE = 1080,
            WIDESCREEN = 1080
        }

        public Boolean fullScreen = false;
    }
}

any direction would be great, thanks :)

like image 271
R-nold Avatar asked Feb 13 '23 12:02

R-nold


2 Answers

The Enums are like classes themselves, they are not variables. Here is a better way to set up the layout.

namespace V1.test.RPG
{
    public class GameOptions
    {
        public GameOptions()
        {
             FullScreen = false;
             DifficultyLevel = Difficulty.Normal;
             SoundSetting = Sound.On;
             // And so on.
        }

        public Boolean FullScreen { get; set; }
        public Difficulty DifficultyLevel { get; set; }
        public Sound SoundSetting { get; set; }
        //And so on...
    }

    public enum Difficulty 
    {
        EASY,
        MEDIUM,
        HARD
    }

    public enum Sound 
    {
        ON,
        QUIET,
        OFF
    }

    public enum Music
    {
        ON,
        QUIET,
        OFF
    }

    public enum ResolutionWidth
    {
        SMALL = 1280,
        MEDIUM = 1366,
        LARGE = 1920,
        WIDESCREEN = 2560
    }

    public enum ResolutionHeight
    {
        SMALL = 800,
        MEDIUM = 768,
        LARGE = 1080,
        WIDESCREEN = 1080
    }

}

You can now set all the default values in the constructor of your GameOptions class, you then just pass a copy of your variable around to the classes that need to read the options and it just reads the setting it needs.

public class GameEngine
{
    private readonly GameOptions _options;
    public GameEngine()
    {
        _options = new GameOptions();    
    }

    public void ShowSetupScreen()
    {
        //If the setup screen modifies the values inside the _gameOptions variable 
        // by passing the variable in it removes the need for "Global variables"
        using(var setupScreen = new SetupScreen(_gameOptions);
        {
            setupScreen.Show();
        }
    }

    public void Enemy CreateEnemy()
    {
        //Creates a new enemy and passes the options in so it can read the settings
        // like the difficulty level.
        return new Enemy(_gameOptions);
    }

    //And so on.

By using a instance variable instead of a static global variable it makes it a lot easier to save and load your settings using things like XmlSerializer so a users settings will be remembered after they close the game.

like image 91
Scott Chamberlain Avatar answered Feb 15 '23 11:02

Scott Chamberlain


I'm using IsolatedStorageSettings to save AppSettings:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

//set value
settings.Add("Difficulty", "EASY");

//get value
object difficulty = settings["Difficulty"];

sure you can use your enum to get the key: Difficulty.EASY.ToString()

like image 38
dieter Avatar answered Feb 15 '23 11:02

dieter