How to get character info from a file?

Viewed 145

I created a file which named by a given name by user, and add some stats in it for example : healt, power, speed, inventory like so. Now, i want to create a function that get stats from given path. It need find the file from given name and get the stat.

I tried re-read the file as note/json so on and it didnt work very well. I want to get variables like : if(inventory.Contains("Bread"))

Note : I tried to save files as .json but it saved as unknown note i dont know how and why.

        ...
        ...
        CharacterData Character = new CharacterData()
        {
            health = 100,
            power = 100,
            speed = 100,
            dexterity = 100,
            hungry = 100,
            weapon = "Fist",
            inventory = new List<string>()
            {
                "Bread"
            }
        };
        string stringjson = JsonConvert.SerializeObject(Character);
        File.WriteAllText(characterPath, stringjson);
    }
    public int GetCharInfo(string charName,string stat)
    {
        //return (stat value)

    }
2 Answers

you can do something like this:

CharacterData Character = new CharacterData()
{
    health = 100,
    power = 100,
    speed = 100,
    dexterity = 100,
    hungry = 100,
    weapon = "Fist",
    inventory = new List<string>()
    {
        "Bread"
    }
};


string stringjson = JsonConvert.SerializeObject(Character);

string path = @"C:\DEV\StackOverflow\";
string characterPath = path + "johnny.json";

File.WriteAllText(characterPath, stringjson);

public int GetCharInfo(string charName, string stat)
{
    string json = File.ReadAllText(path + $"{charName}.json");
    JObject obj = JObject.Parse(json);
    return (int)obj[stat];
}

now if you call:

GetCharInfo("johnny", "power")

you will get the value:

100

also, if you want to see if the key exists you can use the ContainsKey method on JObject like such:

if(obj.ContainsKey(stat))
        return (int)obj[stat];

UPDATE

if you want to get a "stat" of different type I suggest doing this:

have this method that gets the character as a json object

public JObject GetCharacterJSON(string charName){
    string json = File.ReadAllText(path + $"{charName}.json");
    return JObject.Parse(json);
}

now you can load this inside a variable

JObject johnny = GetCharacterJSON("johnny);

And you can get the inventory property like such:

List<string> inventory = johnny["inventory"].ToObject<List<string>>();

As a suggestion, from my point of view if I were you I would try to deserialize the json string into a CharacterData object and then use that like this:

public CharacterData GetCharacterData(string charName){
    string json = File.ReadAllText(path + $"{charName}.json");
    return JsonConvert.DeserializeObject<CharacterData>(json);
}

CharacterData characterData = GetCharacterData("johnny");

int power = characterData.power; //100

List<string> inventory = characterData.inventory; //["Bread"]

if(inventory.Contains("Bread"){
 //do something
}

Note: I didn't take into account error handling in all examples for when the file does not exist or property does not exist etc. I've just outlined the general principle in order to be simple to understand.

Thanks

Related