Reputation: 829
I am having a real hard time with storing and accessing data for my rpg game.
Now I need to store some constants that are accessed quite often and should be global. What I did was create a static class that holds all the constants.
public static class IndexOf
{
public class Element
{
// Element ids in sprite array.
public const int Water = 0;
public const int Lava = 1;
public const int Ice = 2;
}
public class Nature
{
// Nature (placed on tile)
public const int Rock = 0;
public const int Bush = 1;
}
public class Biome
{
//Biomes ids.
public const int Mountain = 0;
public const int River = 1;
}
}
However, is there a better way of doing this or is this a good solution?
Upvotes: 2
Views: 2544
Reputation: 25307
I made a very similar question in another StackExchange site, about what are the best ways to store data inside my game. There is a VERY detailed answer over there, but here it is, in a nutshell, your options to store data in your game:
Depends greatly of how you want to manage your data, if you want to stay between scenes, how often do you want to read from it or other factors.
There is not one golden solution. You have to analyze the use case, and choose what is the best option for you.
Upvotes: 0
Reputation: 69
For constant values you could take a look at enum: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum. Enums are a constant set of integers that are easily readable and easily extendable.
Hope this helps, if you need a practical example let me know.
Upvotes: 0
Reputation: 3067
Have you considered converting your constants to enums?
public enum Element
{
// Element ids in sprite array.
Water,
Lava,
Ice
}
public enum Nature
{
// Nature (placed on tile)
Rock,
Bush
}
public enum Biome
{
//Biomes ids.
Mountain,
River
}
You could then access the elements as with any enum (Element.Water
, Biome.River
, etc).
Upvotes: 2
Reputation: 186813
I think a better (code will be more readable and maintainable) choice is to switch to enum
:
public enum Element {
Water = 0,
Lava = 1,
Ice = 2,
};
public enum Nature {
Rock = 0,
Bush = 1,
};
public enum Biome {
Moutain = 0,
River = 1,
};
etc. enum
are
enum
has been specially designed to hold constants) int biome = Nature.Rock;
since Biome biome = Nature.Rock;
doesn't compile.Sand
to Nature
) Upvotes: 5