Skip to content

Maingamedata example

Main Game Data Example (C# - Unity 3D)

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using Newtonsoft.Json;
using DialogSys;
using System.Dynamic;

/// <summary>
/// ///////////////////////////////////////////////////////////////////////////////////////

/// This is the file that holds all variables that will interact with the dialog system. 
/// Likewise, you can use it to set all variables that will be accessed globally on your application

/// ///////////////////////////////////////////////////////////////////////////////////////
/// </summary>

namespace MainGD
{

    [Serializable]
    public class MainGameData : MonoBehaviour
    {
        [HideInInspector]
        public DialogSystem DialogSystem = null; //Will be set on DSysComponent
        public MainGlobaVariablesClass.Root GlobalVariables;
        public MainDialogSysDatasetClass Datasets;
        public DialogClassesOperatorClass DialogClassesOperator;

        // Start is called before the first frame update
        void Start()
        {
            GlobalVariables = new MainGlobaVariablesClass.Root();
            Datasets = new MainDialogSysDatasetClass();
            DialogClassesOperator = new DialogClassesOperatorClass();

        }



        [Serializable]
        public class MainGlobaVariablesClass
        {

            [Serializable]
            public class Root // GlobaVariablesClass
            {
                public Data data; 
            }

            [Serializable]
            public class A
            {
                public String c; 
                public String d; 
            }

            [Serializable]
            public class Teste
            {
                public A a; 
                public string b; 
            }

            [Serializable]
            public class Data
            {
                public bool welcome_message;
                public bool testing;
                public bool pass;
                public bool got_sword; 
                public bool show_message; 
                public int strength; 
                public float counter;
            }
        }



        public class DialogClassesOperatorClass
        {
            public List<DatasetClassListEntry> ListOfDatasetClasses;

            public class DatasetClassListEntry
            {
                //The dataset model id
                public string mdid;
                public Type classType;

                public DatasetClassListEntry(string modelDsId, Type type)
                {
                    mdid = modelDsId;
                    classType = type;
                }
            }

            // Mount the list of the all datasets classes used by Datasets of the DialogSystem
            // Theres one list for each type of Dataset. Why not just make one? Becouse it would 
            // be necessary create a list with a generic object (System.Object) that are not supported 
            // to be visualized on Unity Inspector pane

            public DialogClassesOperatorClass()
            {
                ListOfDatasetClasses = new List<DatasetClassListEntry>();


                //The list must be filled manualy, for each dataset model that exists..

                ListOfDatasetClasses.Add(new DatasetClassListEntry( "1", typeof(DatasetModel_1)));
                ListOfDatasetClasses.Add(new DatasetClassListEntry("2", typeof(DatasetModel_2)));

            }
        }

        public void MainDatasetsToDialogSysDatasets( )
        {

            Action<dynamic> act = MainDatasetsToDialogSysDatasetsDo;

            // A 'foreach' must be created for each dataset model..

            //Foreach for dataset model 1 
            foreach (var DS in Datasets.ListDatasetModel_1) {
                act(DS);
            }

            //Foreach for dataset model 2 
            foreach (var DS in Datasets.ListDatasetModel_2)
            {
                act(DS);
            }

        }

        private void MainDatasetsToDialogSysDatasetsDo(dynamic DS)
        {
            int k = DialogSystem.Datasets.FindIndex(t => t.Did == DS.__did);
            //If item found..
            if (k >= 0)
            {
                //Converting to json 
                String jsonText = JsonConvert.SerializeObject(DS);

                DialogSystem.Datasets[k].Data = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
            }
        }


        [Serializable]
        public class MainDialogSysDatasetClass
        {

            public List<DatasetModel_1> ListDatasetModel_1;
            public List<DatasetModel_2> ListDatasetModel_2;

            public MainDialogSysDatasetClass()
            {
                ListDatasetModel_1 = new List<DatasetModel_1>();
                ListDatasetModel_2 = new List<DatasetModel_2>();
            }
            public bool addElementToDatasetList(string mdid, string jsonText)
            {
                bool ret = false;

                if( mdid == "1")
                {
                    ListDatasetModel_1.Add(JsonConvert.DeserializeObject<DatasetModel_1>(jsonText));
                    ret = true;
                }
                else if( mdid == "2")
                {
                    ListDatasetModel_2.Add(JsonConvert.DeserializeObject<DatasetModel_2>(jsonText));
                    ret = true;
                }

                return ret;
            }

        }


        [Serializable]
        public class DatasetModel_1
        {

            public string __did;
            public float health; 
            public float magic; 
            public float location; 
            public string born; 
            public float funds; 
            public List<String> partners_id;

        }

        [Serializable]
        public class DatasetModel_2
        {

            public string __did;
            public float reliability; 
            public float courage; 
            public float shyness; 
            public float happiness; 
            public float anger; 
            public float strength;

        }

    }
}