Search code examples
c#enums2d-games

Best way to store game-wide variables


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 :)


Solution

  • 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.