Changing Mover.cs using Brian's Json Save System to take Multiple Parameters

If you haven’t been using the binary formatter and instead opted to use the Json save system that Brian provided here: Home · Wiki · Brian K. Trotter / RPGMods · GitLab, then you should be able to change the class as follows:

        public JToken CaptureAsJToken()
        {
            Dictionary<string, JToken> data = new Dictionary<string, JToken>();
            data["position"] = transform.position.ToToken();
            data["rotation"] = transform.eulerAngles.ToToken();

            JToken returnToken = JsonConvert.SerializeObject(data);
            return returnToken;
        }

        public void RestoreFromJToken(JToken state)
        {
            if (navMeshAgent == null)
            {
                navMeshAgent = GetComponent<NavMeshAgent>();
            }

            JObject jsonObject = JObject.Parse(state.ToString());
            Dictionary<string, JToken> myDictionary = jsonObject.ToObject<Dictionary<string, JToken>>();

            navMeshAgent.enabled = false;
            transform.position = myDictionary["position"].ToVector3();
            transform.eulerAngles = myDictionary["rotation"].ToVector3();
            navMeshAgent.enabled = true;
            GetComponent<ActionScheduler>().CancelCurrentAction();
            
        }

Just as a note, I had to reset the navMeshAgent here as I got null reference errors occasionally.

This seemed to work for me. I hope it is correct and helps someone else out if they need it.

Below is the change for the STRUCT type… I separated the 3 save/restore ways into different methods, which is why. Each one is called from the standard save / restore methods, and is just a call to the correct method


        public JToken CaptureAsJToken()
        {
            //return SaveNormal();
            // return SaveAsDictionary();
            return SaveAsStruct();
            
        }

        public void RestoreFromJToken(JToken state)
        {
            // RestoreNormal(state);
            // RestoreFromDictionary(state);
            RestoreFromStruct(state);
        }
...
...
...
...

// Below is how to save / restore data using multiple fields using a struct
        [System.Serializable]
        private struct MoverSaveData
        {
            public JToken position;
            public JToken rotation;
        }

        private JToken SaveAsStruct()
        {
            MoverSaveData data  = new MoverSaveData();
            data.position = this.transform.position.ToToken();
            data.rotation = this.transform.eulerAngles.ToToken();

            JToken returnToken = JsonConvert.SerializeObject(data);

            return returnToken;
        }

        private void RestoreFromStruct(object state)
        {
            JObject jsonObject = JObject.Parse(state.ToString());
            MoverSaveData data = jsonObject.ToObject<MoverSaveData>();

            navMeshAgent.enabled = false;
            this.transform.position = data.position.ToVector3();
            this.transform.eulerAngles = data.rotation.ToVector3();
            navMeshAgent.enabled = true;
            GetComponent<ActionScheduler>().CancelCurrentAction();
        }

Privacy & Terms