Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

load Json datas into a static class to store character datas

Discussion in 'Scripting' started by Shadauwa, Aug 8, 2020.

  1. Shadauwa

    Shadauwa

    Joined:
    Jan 12, 2020
    Posts:
    16
    Hello Guys,
    I am stuck about how to do well things arround Json.

    I have a static class to store my character datas (name, attaque, level etc.)
    I have a class to load JSON datas coming from a php script.

    My problem is to store my json datas into a static class to keep it during all the game, but I can't make it directly :(

    Global Class for all project :
    Code (csharp):
    1. public class GlobalVar {
    2. public static CharacterJson characterJson = new CharacterJson();
    3. }
    My Character class, that is not a static one to load easly from JSON
    Code (csharp):
    1. [System.Serializable]
    2. public class CharacterJson {
    3.     public string name;
    4.     public int attaque;
    5.     public int level;
    6. }
    JSON load correctly my datas
    Code (csharp):
    1. GlobalVar.characterJson = JsonUtility.FromJson<CharacterJson>(JsonDataFromServer);
    2. info.text = GlobalVar.characterJson.name;
    info.text display the data correctly, but when i will use my Character class elswhere i got a Null.that's why i am looking to store my Json directly in a static Character class.

    I dont want to create a "copy" static class to pass data. like :
    Code (csharp):
    1. public static class Character {
    2.     public static string name;
    3.     public static int attaque;
    4.     public static int level;
    5. }
    I want to avoid a thing like that in my script, especialy if i must to change my Character class to add more datas.
    Code (csharp):
    1. Character.name = GlobalVar.characterJson.name;
    2. Character.attaque = GlobalVar.characterJson.attaque;
    3. Character.level = GlobalVar.characterJson.level;
    I need to know how to put my datas from Json directly in my static class.

    Well i hope i am clear enought.
    Cheers
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can you show how you are accessing the data? What you shared seems like it should work, as long as things are done in the correct order (data is loaded before you access it).
     
  3. Shadauwa

    Shadauwa

    Joined:
    Jan 12, 2020
    Posts:
    16
    Your are right, this point is fixed by acessing the data after its loaded :) ...basic things but... well thanks !!

    And now the main point, what can i do to load my Json data direclty in my static Character class without doing that way => for all variables... Character.name = GlobalVar.characterJson.name; etc...
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You can't really. I'm not sure what advantage the static Character class would be giving you.
     
  5. Shadauwa

    Shadauwa

    Joined:
    Jan 12, 2020
    Posts:
    16
    Hi, good point again :)
    Well thanks a lot you helped me to understand why i where stuck and how to work arround. i can go further now, this is great, you make my day ^^