How do I make the object in my plugin save its coordinates after exiting the game?

Hey! I decided to make my first plugin for the second lantern in the game. I got it, everything works, except for one BUT: I re-read SaveLoad Class in the MSCLoader Wiki several times, looked at the code of about a dozen other mods and could not do anything! Please help me!
C#:
using MSCLoader;
using UnityEngine;

namespace SecondLantern
{
    public class DoubleLantern : Mod
    {
        public override string ID => "SecondLantern";
        public override string Name => "SecondLantern";
        public override string Author => "Mr.Gitsen";
        public override string Version => "0";
        private static GameObject SecondLantern;

        public override void OnLoad()
        {
            SecondLantern = Object.Instantiate(GameObject.Find("lantern(itemx)"));
            SecondLantern.transform.localPosition = new Vector3(-10f, 0.2f, 6f);

        }
    }
}
P.S. about my skill - I'm still a noob in Unity and C#, but I already made file mods for MSC (replaced models and textures directly in the sharedassets itself). I learned to write plugins using the code from the Roman266 and zamp mods.
 
Last edited:
C#:
        public override void OnLoad()
        {
            MySaveObject myObject = SaveLoad.DeserializeSaveFile<MySaveObject>(this, "my_save_file.json");
            
            //Example usage after values are loaded into your custom object
            satsuma.setSpeedTo(myObject.getMyIntValue());
            //
        }
        public override void OnSave()
        {
            //Exmaple usage that would happen somewhere in your code
            myObject.setMyIntValue(200);
            //
            
            SaveLoad.SerializeSaveFile<MySaveObject>(this, myObject, "my_save_file.json");
        }
 
C#:
        public override void OnLoad()
        {
            MySaveObject myObject = SaveLoad.DeserializeSaveFile<MySaveObject>(this, "my_save_file.json");
           
            //Example usage after values are loaded into your custom object
            satsuma.setSpeedTo(myObject.getMyIntValue());
            //
        }
        public override void OnSave()
        {
            //Exmaple usage that would happen somewhere in your code
            myObject.setMyIntValue(200);
            //
           
            SaveLoad.SerializeSaveFile<MySaveObject>(this, myObject, "my_save_file.json");
        }
Oh, thank you so much! Honestly, I did not expect someone to answer me, but it happened! But, I had a couple of questions: Apparently, I did not understand what is meant by "MySaveObject" and "myObject" (well, I understood the meaning of these words, but, apparently, I wrote something wrong in my code instead) ... And now Visual Studio throws error CS1503. That variable (maybe it's called something different, he's not talking about that now ...) I marked // right here.
C#:
 public override string ID => "SecondLantern";
        public override string Name => "SecondLantern";
        public override string Author => "Mr.Gitsen";
        public override string Version => "0";
        private GameObject SecondLantern;

        public override void OnLoad()
        {
            DoubleLantern myObject = SaveLoad.DeserializeSaveFile<DoubleLantern>(this, "my_save_file.json");

            //Example usage after values are loaded into your custom object
            SecondLantern = Object.Instantiate(GameObject.Find("lantern(itemx)"));
            //
        }
        public override void OnSave()
        {
            //Exmaple usage that would happen somewhere in your code
            SecondLantern.transform.localPosition = new Vector3(-10f, 0.2f, 6f);
            //
                                                            //right here
            SaveLoad.SerializeSaveFile<DoubleLantern>(this, SecondLantern, "my_save_file.json");
        }
    }
}
P.S. Well, thank you again for helping novice modders!
 
Oh, thank you so much! Honestly, I did not expect someone to answer me, but it happened! But, I had a couple of questions: Apparently, I did not understand what is meant by "MySaveObject" and "myObject" (well, I understood the meaning of these words, but, apparently, I wrote something wrong in my code instead) ... And now Visual Studio throws error CS1503. That variable (maybe it's called something different, he's not talking about that now ...) I marked // right here.
C#:
 public override string ID => "SecondLantern";
        public override string Name => "SecondLantern";
        public override string Author => "Mr.Gitsen";
        public override string Version => "0";
        private GameObject SecondLantern;

        public override void OnLoad()
        {
            DoubleLantern myObject = SaveLoad.DeserializeSaveFile<DoubleLantern>(this, "my_save_file.json");

            //Example usage after values are loaded into your custom object
            SecondLantern = Object.Instantiate(GameObject.Find("lantern(itemx)"));
            //
        }
        public override void OnSave()
        {
            //Exmaple usage that would happen somewhere in your code
            SecondLantern.transform.localPosition = new Vector3(-10f, 0.2f, 6f);
            //
                                                            //right here
            SaveLoad.SerializeSaveFile<DoubleLantern>(this, SecondLantern, "my_save_file.json");
        }
    }
}
P.S. Well, thank you again for helping novice modders!
MySaveObject is an object you created (Visual studio -> Project -> create new element -> class)
(Or something like that, you need to create a new class)

That class needs to have fields
So for your lantern you eould probably have 3 fields
C#:
public int x;
public int y;
public int z;

You don't have to use an object you could also use a Dictionary (Look up how to use one in c# on google)
 
MySaveObject is an object you created (Visual studio -> Project -> create new element -> class)
(Or something like that, you need to create a new class)

That class needs to have fields
So for your lantern you eould probably have 3 fields
C#:
public int x;
public int y;
public int z;

You don't have to use an object you could also use a Dictionary (Look up how to use one in c# on google)

Hello Donner! It's okay, my mod works after a week of trial and error. Thanks for helping. And one more thing: if it's not a secret, how did you learn to make such cool mods?
 

Attachments

  • SL.jpg
    SL.jpg
    224.8 KB · Views: 107

Latest News

Are you buying car setups?

  • Yes

  • No


Results are only viewable after voting.
Back
Top