4

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

R-nold
  • 242
  • 2
  • 11
  • 1
    You'd probably want to create an instance of the GameOptions class that has instances of each option Enum. You could have a constructor with no parameters where you automatically set each of these to their "default" value. – Wilson Jul 13 '14 at 17:46
  • 1
    There's a [gamedev StackExchange site](http://gamedev.stackexchange.com/) now. You'll probably want to ask there. – Chris Hayes Jul 13 '14 at 17:52
  • @ChrisHayes thank you, I've asked the question there as well, http://gamedev.stackexchange.com/questions/80210/best-way-to-store-game-wide-variables – R-nold Jul 13 '14 at 18:47
  • @Wilson thank you for the comment, sounds like Scott's answer below, which seems like the way to go :) – R-nold Jul 13 '14 at 18:48
  • 1
    @ChrisHayes: This question is not specific to game development, it is about a common problem that occurs in a variety of applications. In a game, the global options may be *difficulty*, *resolution* and *full screen*, whereas in an office application, the same question will concern other options such as *tooltip help formatting*, *toolbar icon size* and ... *full screen* (maximize usable working area). – O. R. Mapper Jul 14 '14 at 11:41
  • @O.R.Mapper Hence why I didn't say "this doesn't belong here, take it there". But this is such a common question in game development that it would be silly to ignore that they're the experts on this topic, particularly if there's any wrinkles specific to game development that a more generalist site won't capture. – Chris Hayes Jul 14 '14 at 16:23
  • There's been different answers over there, so I'm glad I was pointed that way as well. Not to mention it was obviously a gaming related question to me, therefore now I know that place exists I'll ask any future gaming-specific questions there :) Anyways, thank you all for your help! – R-nold Jul 14 '14 at 17:59

2 Answers2

7

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.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Thank you for this! Should I then store resolutionWidth and Height as int's? public int resolutionWidth { get; set; } public int resolutionHeight { get; set; } And get the values from the enums as so? resolutionWidth = (int)ResolutionWidth.WIDESCREEN; resolutionHeight = (int) ResolutionHeight.WIDESCREEN; – R-nold Jul 13 '14 at 19:07
  • ^ This is so horribly formatted. Sorry. I've tried to format it better to no avail :/ – R-nold Jul 13 '14 at 19:10
  • Honestly I would store the resolution as its own class and not as a enum at all. `public class Resolution { public int Width {get; set;} public int Height {get; set; }` – Scott Chamberlain Jul 13 '14 at 19:17
1

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

alex
  • 8,904
  • 6
  • 49
  • 75