GUID saving/loading scene changing status

Discussion and help for Easy Save 3
Post Reply
Notso
Posts: 50
Joined: Sat May 08, 2021 6:53 pm

GUID saving/loading scene changing status

Post by Notso »

My problem should be a simple one. I searched the forums a bit and found a save script, that works for the basics.
My objects I want to save are simple prefabs (just a single object no children).
So take a Rusty Pipe (weapon), I want to save the active or deactive state of it. I also use Easy Pooling so I just send it back to the pool manager and it deactivates it. OnDisable I save the state and that works, yay I can change scenes and no more duplicates.
NOW the issue is, when I drop the object from inventory, it spawns a new one from a resources prefab(pool manager uses it) and this is always the same GUID but I have multiple pipes throughout the level so each one would end with the same if I picked it up and dropped it (at start they are all different). I attempted to generate a new GUID and I think I succeeded in that but not sure if it will always create a new one each time the scene loads or not since I am having issues loading the saved transform.position.
Also not sure why but the save transform always sets it to 0,0,0 onEnable even though it is not.
This screen shot is taken the moment I hit DROP from my inventory and the new clone is created.
(Also not sure but it does not show that object is active, just the transform since there is a new GUID on the clone one)
Image
So in essence, I want to save each objects on/off state and position/rotation for scene changing
Do not laugh at my code! haha, it is not my strong suit but I am trying!

Code: Select all

	public string guid = Guid.NewGuid().ToString();
    static bool isApplicationQuitting = false;
    private static bool uidIssued;
    void Awake() 
	{
		if (!uidIssued)
		{
			guid = Guid.NewGuid().ToString();
			uidIssued = true;
		}
		gameObject.SetActive(ES3.Load<bool>(guid, enabled));
		ES3.Load<Vector3>(guid, transform.localPosition);
	}


    void OnDisable()
	{
        if (gameObject.scene.isLoaded && !isApplicationQuitting)
            ES3.Save<bool>(guid, false);
	}

	void OnEnable()
	{
        if (gameObject.scene.isLoaded && !isApplicationQuitting)
        {  
	        ES3.Save<bool>(guid, true);
	        ES3.Save<Vector3>(guid, transform.localPosition);
        }
	}

    void OnApplicationQuit()
    {
        isApplicationQuitting = true;
    }
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: GUID saving/loading scene changing status

Post by Joel »

Hi there,

Your GUID will be different each time, so this is not a suitable way of creating a unique key for an object.

Generally in these situations you would store an array of your prefab instances so that you don't need to create a unique ID for them.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Notso
Posts: 50
Joined: Sat May 08, 2021 6:53 pm

Re: GUID saving/loading scene changing status

Post by Notso »

Wow, I had a reply and was logged out while in here so it deleted it all...great...
Anyway, in short
I modified the code. However It always saves it to 0,0,0. (See image in first post) I finally got it to remove the object on load, and spawn one if it needed it. Does this look better?

Code: Select all

	public string guid = System.Guid.NewGuid().ToString();
	static bool isApplicationQuitting = false;
	
	void Awake() 
	{
		if(ES3.KeyExists(guid)) ES3.Load(guid, gameObject);
		gameObject.SetActive(ES3.Load<bool>(guid, enabled));
	}

	void OnDisable()
	{
		if (gameObject.scene.isLoaded && !isApplicationQuitting)
		{
			ES3.Save<bool>(guid, false);
			ES3.Save(guid, this.gameObject);
		}
	}

	void OnEnable()
	{
		if (gameObject.scene.isLoaded && !isApplicationQuitting)
		{
			ES3.Save<bool>(guid, true);
			ES3.Save(guid, this.gameObject);
		}
	}

	void OnApplicationQuit()
	{
		isApplicationQuitting = true;
	}
EDIT:
I might have gotten it to work, just added a coroutine to get the transform about 5 seconds after it is dropped (might put a check if timescale is 0 since you can only drop when paused in inventory).
But this loaded the object. Now how to do I save the script on it? It loads the prefab but that has a collectible item script on it. That has 2 references to in scene scriptable objects.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: GUID saving/loading scene changing status

Post by Joel »

Hi there,

For information on getting custom scripts to be saved, please see the Saving and Loading GameObjects guide:

https://docs.moodkie.com/easy-save-3/es ... s-prefabs/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Notso
Posts: 50
Joined: Sat May 08, 2021 6:53 pm

Re: GUID saving/loading scene changing status

Post by Notso »

Been using that. I am using the the save gameobject from that page already.
But in order to get it to save scripts it appears I need change the write methods or something etc... which turns this into complicatedsave3. :D
I am not strong in the coding area of things, but as you can see, I am trying.
The good news is all my objects derive from the inventoryitem class so if I can this 1 to work then they will all work.

The main issue is that the primary in scene object is disabled on pickup and when you drop it from your inventory it spawns a new one from the resources.

So I stepped back and turned on the autosave. When I drop it, it of course sets 0,0,0 on quit. However it shows all the info even from the script (in the save file) great. but on load it does not seem to spawn the actual gameobject. (Not so concerned with the 0,0,0 at the moment, first just want ot get it spawned with the right info. Later I remove the autosave setting and go back to save/load with the script onenable/disable like before.)
So I have a script that spawns the object but no collectibleitem script and an autosave that saves the script but does not spawn the object....
I do have the es3autosave on the prefab and even replaced the prefab in the scene with the new one that has autosave on it.

Should the autosave spawn the object if it is missing? (It shows in the autosavemanager in scene that the object is managed as well)
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: GUID saving/loading scene changing status

Post by Joel »

Hi there,

Please could you replicate this in a new project with a very simple scene and private message it to me with instructions?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Notso
Posts: 50
Joined: Sat May 08, 2021 6:53 pm

Re: GUID saving/loading scene changing status

Post by Notso »

This is an almost completed project. I tried the last 2 hours to get a scene exported, but it is so large(the dependencies) and then it doesn't export everything, like missing input settings, using a mixture of old and new import system. The scene itself was the player, HUD/UI a pipe, a plane and the scene changer trigger so wasn't trying to get a full scene.
Probably around 20 different script references on the player and inventory etc....
And Unity has a horrid export procedure. The right click select dependencies then select the scene still tries to export enemies and animations when there are none and no reference to them. Along with the purchased assets that also are referenced on the player and coded into the scripts to work makes it huge. (well 700 meg but still does not work even with that)
I will make a video showing what is going on and how it is setup so you can see.
If you are on Discord I could we could use that as well if easier.
I do appreciate the help and really want to get this working.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: GUID saving/loading scene changing status

Post by Joel »

Hi there,

Unfortunately I would need to see this replicated in a new project rather than an existing one, otherwise it wouldn't be possible for me to determine what is due to Easy Save and what is due to the logic of your project.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply