I'm making a 2D game with C# & XNA. I'm working on saving and loading currently, all of the data is stored in text files.
Each sprite has a state:
public enum SpriteState
{
Alive,
Dead,
Chasing,
Sleeping,
Waiting
}
When saving i'm simply executing this line of code:
StreamWriter.WriteLine(gameState);
Now when i'm loading a game i'm having to read that line of the text file store it in a string variable and perform near enough the following:
string inType = StreamReader.ReadLine();
if(inType == "Alive")
//Set the sprites state to alive
else if(inType == "Dead")
//Set the sprites state to alive
And so on... So my question is: Is there a better way of reading a enum type from a text file and assigning it?
Many thanks
You're looking for
(SpriteState) Enum.Parse(typeof(SpriteState), inType)
This will parse a string into an enum value.
You may also want to have a Dictionary<SpriteState, Action<...>>
mapping states to delegates (lambda expressions) that take the appropriate action.
try this:
Enum.Parse(typeof(SpriteState), yourString);
Also you can use this method:
public static T ParseEnum<T>(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.