【问题标题】:Best way to store game-wide variables存储游戏范围变量的最佳方式
【发布时间】:2014-09-03 17:09:22
【问题描述】:

我有一个用于难度、分辨率、全屏等内容的选项屏幕,但我正在努力寻找在游戏时间存储/获取这些变量的最佳方法。

我目前决定这样做的方式是创建一个包含所有 GameOption 枚举的“常量”类。但是如何为所有这些选项选择默认值,以及如何获取当前选择的枚举?

尤其是分辨率的问题 - 我决定存储这些值但不确定如何获取默认值或当前存储的值?

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;
    }
}

任何方向都会很棒, 谢谢:)

【问题讨论】:

  • 您可能希望创建一个包含每个选项枚举实例的 GameOptions 类的实例。您可以有一个没有参数的构造函数,您可以在其中自动将每个参数设置为它们的“默认”值。
  • 现在有一个gamedev StackExchange site。你可能想在那里问。
  • @ChrisHayes 谢谢,我也问过这个问题,gamedev.stackexchange.com/questions/80210/…
  • @Wilson 谢谢你的评论,听起来像下面斯科特的回答,这似乎是要走的路:)
  • @ChrisHayes:这个问题不是专门针对游戏开发的,它是关于各种应用程序中出现的一个常见问题。在游戏中,全局选项可能是难度分辨率全屏,而在办公应用中,同样的问题将涉及其他选项例如工具提示帮助格式工具栏图标大小和...全屏(最大化可用工作区域)。

标签: c# enums 2d-games


【解决方案1】:

枚举本身就像类,它们不是变量。这是设置布局的更好方法。

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
    }

}

您现在可以在 GameOptions 类的构造函数中设置所有默认值,然后只需将变量的副本传递给需要读取选项的类,它就会读取所需的设置。

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.

通过使用实例变量而不是静态全局变量,可以更轻松地使用 XmlSerializer 之类的方式保存和加载您的设置,这样用户的设置将在他们关闭游戏后被记住。

【讨论】:

  • 谢谢你!然后我应该将 resolutionWidth 和 Height 存储为 int 吗?公共 int resolutionWidth { 得到;放; } 公共 int resolutionHeight { 得到;放;并从枚举中获取值? resolutionWidth = (int)ResolutionWidth.WIDESCREEN; resolutionHeight = (int) ResolutionHeight.WIDESCREEN;
  • ^ 这是非常可怕的格式。对不起。我试图更好地格式化它无济于事:/
  • 老实说,我会将分辨率存储为它自己的类,而不是一个枚举。 public class Resolution { public int Width {get; set;} public int Height {get; set; }
【解决方案2】:

我正在使用 IsolatedStorageSettings 来保存 AppSettings:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

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

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

确定您可以使用您的枚举来获取密钥:Difficulty.EASY.ToString()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-25
    • 2016-12-13
    • 2016-05-26
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多