samedi 13 avril 2019

Copy values with same name across classes

I am trying to get a dialogue JSON's content and copy its properties(stored to the class Dialogues) to another class deriving from MonoBehaviour(DialogueManager). Anyway, the GetValue method returns null even if GetProperty returns the correct propery names. I can't just read the Json to DialogueManager, because JSONUtility apparently needs to export to a non-MonoBehaviour class.

using System.IO;
using UnityEngine;

public class DialogueManager : MonoBehaviour
{
    //999 IS RETURN TO MENU
    //1000 IS CLOSE GAME
    public static DialogueManager instance = null;
    Dialogues dial;
    public string text1 { get; set; }
    public string choices1 { get; set; }
    public string consequences1 { get; set; }
    public string text2 { get; set; }
    public string choices2 { get; set; }
    public string consequences2 { get; set; }
    public string text3 { get; set; }
    public string choices3 { get; set; }
    public string consequences3 { get; set; }
    public string text4 { get; set; }
    public string choices4 { get; set; }
    public string consequences4 { get; set; }
    public string text5 { get; set; }
    public string choices5 { get; set; }
    public string consequences5 { get; set; }
    public string text6 { get; set; }
    public string choices6 { get; set; }
    public string consequences6 { get; set; }
    public string text7 { get; set; }
    public string choices7 { get; set; }
    public string consequences7 { get; set; }
    public string text8 { get; set; }
    public string choices8 { get; set; }
    public string consequences8 { get; set; }
    public string text9 { get; set; }
    public string choices9 { get; set; }
    public string consequences9 { get; set; }
    public string text10 { get; set; }
    public string choices10 { get; set; }
    public string consequences10 { get; set; }
    public string text11 { get; set; }
    public string choices11 { get; set; }
    public string consequences11 { get; set; }
    public string text12 { get; set; }
    public string choices12 { get; set; }
    public string consequences12 { get; set; }
    public string text13 { get; set; }
    public string choices13 { get; set; }
    public string consequences13 { get; set; }
    public string text14 { get; set; }
    public string choices14 { get; set; }
    public string consequences14 { get; set; }
    public string text15 { get; set; }
    public string choices15 { get; set; }
    public string consequences15 { get; set; }
    public string text16 { get; set; }
    public string choices16 { get; set; }
    public string consequences16 { get; set; }
    public string text17 { get; set; }
    public string choices17 { get; set; }
    public string consequences17 { get; set; }
    public string text18 { get; set; }
    public string choices18 { get; set; }
    public string consequences18 { get; set; }
    public string text19 { get; set; }
    public string choices19 { get; set; }
    public string consequences19 { get; set; }
    public string text20 { get; set; }
    public string choices20 { get; set; }
    public string consequences20 { get; set; }
    public string text21 { get; set; }
    public string choices21 { get; set; }
    public string consequences21 { get; set; }
    public string text22 { get; set; }
    public string choices22 { get; set; }
    public string consequences22 { get; set; }
    public string text23 { get; set; }
    public string choices23 { get; set; }
    public string consequences23 { get; set; }
    string fixedName;

    string[] splits;
    string text;
    int[] consequences;
    private void Awake()
    {
        if(instance == null)
        {
            instance = this;
        }
        else
        {
            enabled = false;
        }
        dial = new Dialogues();
        StreamReader sR = new StreamReader(Application.dataPath + "/GameData/dialogues.json");
        string json = sR.ReadToEnd();
        dial = JsonUtility.FromJson<Dialogues>(json);
        Assign();
    }

    //This doesn't work for some reason
    public void Assign()
    {
        foreach(var property in typeof(Dialogues).GetProperties())
        {
            typeof(DialogueManager).GetProperty(property.Name).SetValue(this, property.GetValue(dial));
        }
    }
    public string GetText(int currentText)
    {
        fixedName = "text" + currentText;
        string output = typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null).ToString();
        return output;
    }
    public string GetTextChoices(int textNum)
    {
        fixedName = "choices" + textNum;
        string output = typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null).ToString();
        return output;
    }
    public int[] GetChoicesConsequences(int textNum)
    {
        fixedName = "consequences" + textNum;
        string output = typeof(DialogueManager).GetProperty(fixedName).GetValue(this, null).ToString();
        splits = output.Split('§');
        consequences = new int[splits.Length];
        for (int i = 0; i < splits.Length; i++)
        {
            consequences[i] = int.Parse(splits[i]);
        }
        return consequences;
    }
    private void DumbAssign()
    {
        text1 = dial.text1;
        choices1 = dial.choices1;
        consequences1 = dial.consequences1;
        text2 = dial.text2;
        choices2 = dial.choices2;
        consequences2 = dial.consequences2;
        text3 = dial.text3;
        choices3 = dial.choices3;
        consequences3 = dial.consequences3;
        text4 = dial.text4;
        choices4 = dial.choices4;
        consequences4 = dial.consequences4;
        text5 = dial.text5;
        choices5 = dial.choices5;
        consequences5 = dial.consequences5;
        text6 = dial.text6;
        choices6 = dial.choices6;
        consequences6 = dial.consequences6;
        text7 = dial.text7;
        choices7 = dial.choices7;
        consequences7 = dial.consequences7;
        text8 = dial.text8;
        choices8 = dial.choices8;
        consequences8 = dial.consequences8;
        text9 = dial.text9;
        choices9 = dial.choices9;
        consequences9 = dial.consequences9;
        text10 = dial.text10;
        choices10 = dial.choices10;
        consequences10 = dial.consequences10;
        text11 = dial.text11;
        choices11 = dial.choices11;
        consequences11 = dial.consequences11;
        text12 = dial.text12;
        choices12 = dial.choices12;
        consequences12 = dial.consequences12;
        text13 = dial.text13;
        choices13 = dial.choices13;
        consequences13 = dial.consequences13;
        text14 = dial.text14;
        choices14 = dial.choices14;
        consequences14 = dial.consequences14;
        text15 = dial.text15;
        choices15 = dial.choices15;
        consequences15 = dial.consequences15;
        text16 = dial.text16;
        choices16 = dial.choices16;
        consequences16 = dial.consequences16;
        text17 = dial.text17;
        choices17 = dial.choices17;
        consequences17 = dial.consequences17;
        text18 = dial.text18;
        choices18 = dial.choices18;
        consequences18 = dial.consequences18;
        text19 = dial.text19;
        choices19 = dial.choices19;
        consequences19 = dial.consequences19;
        text20 = dial.text20;
        choices20 = dial.choices20;
        consequences20 = dial.consequences20;
        text21 = dial.text21;
        choices21 = dial.choices21;
        consequences21 = dial.consequences21;
        text22 = dial.text22;
        choices22 = dial.choices22;
        consequences22 = dial.consequences22;
    }
}

[System.Serializable]
public class Dialogues
{
    public string text1 { get; set; }
    public string choices1 { get; set; }
    public string consequences1 { get; set; }
    public string text2 { get; set; }
    public string choices2 { get; set; }
    public string consequences2 { get; set; }
    public string text3 { get; set; }
    public string choices3 { get; set; }
    public string consequences3 { get; set; }
    public string text4 { get; set; }
    public string choices4 { get; set; }
    public string consequences4 { get; set; }
    public string text5 { get; set; }
    public string choices5 { get; set; }
    public string consequences5 { get; set; }
    public string text6 { get; set; }
    public string choices6 { get; set; }
    public string consequences6 { get; set; }
    public string text7 { get; set; }
    public string choices7 { get; set; }
    public string consequences7 { get; set; }
    public string text8 { get; set; }
    public string choices8 { get; set; }
    public string consequences8 { get; set; }
    public string text9 { get; set; }
    public string choices9 { get; set; }
    public string consequences9 { get; set; }
    public string text10 { get; set; }
    public string choices10 { get; set; }
    public string consequences10 { get; set; }
    public string text11 { get; set; }
    public string choices11 { get; set; }
    public string consequences11 { get; set; }
    public string text12 { get; set; }
    public string choices12 { get; set; }
    public string consequences12 { get; set; }
    public string text13 { get; set; }
    public string choices13 { get; set; }
    public string consequences13 { get; set; }
    public string text14 { get; set; }
    public string choices14 { get; set; }
    public string consequences14 { get; set; }
    public string text15 { get; set; }
    public string choices15 { get; set; }
    public string consequences15 { get; set; }
    public string text16 { get; set; }
    public string choices16 { get; set; }
    public string consequences16 { get; set; }
    public string text17 { get; set; }
    public string choices17 { get; set; }
    public string consequences17 { get; set; }
    public string text18 { get; set; }
    public string choices18 { get; set; }
    public string consequences18 { get; set; }
    public string text19 { get; set; }
    public string choices19 { get; set; }
    public string consequences19 { get; set; }
    public string text20 { get; set; }
    public string choices20 { get; set; }
    public string consequences20 { get; set; }
    public string text21 { get; set; }
    public string choices21 { get; set; }
    public string consequences21 { get; set; }
    public string text22 { get; set; }
    public string choices22 { get; set; }
    public string consequences22 { get; set; }
    public string text23 { get; set; }
    public string choices23 { get; set; }
    public string consequences23 { get; set; }
}






Aucun commentaire:

Enregistrer un commentaire