Trying to save a transform with child objects, but having difficulty.

Discussion and help for Easy Save 3
Post Reply
Ducktor_Cid
Posts: 2
Joined: Tue May 25, 2021 12:39 am

Trying to save a transform with child objects, but having difficulty.

Post by Ducktor_Cid »

I'm making a game where you can create these marble run tracks using cuboro-styled (and legally distinct ;) ) blocks. I want to be able to save these blocks (which are all children of a gameobject called TrackParent)

Some preliminary testing showed that everything seemed to be fine but I soon found a few issues:

The actual marble object, which has a script that stops it from sleeping (prevents physics bugs) and as-well as resetting it when the simulation stops and enabling it's rigidbody when the simulation starts (player has control over whether or not the simulation is running by clicking a button), wasn't being saved.

I made a new type for it but then things seemed to have become worse. Some blocks now just become pink (caused by it being loaded with extra materials in it's renderer's materials array despite it only having one at the time of saving) and the marble creates an error saying it doesn't have a rigidbody when it does (I assume being caused by the fact the script is loaded before the rigidbody?)

To save, I just do (doesn't include the code for checking if save already exists. It's just a simple ES3.KeyExists if-statement)

Code: Select all

ES3.Save(saveNameInput.text, GameObject.Find("TrackParent"));
To load, I do this.

Code: Select all

        
        //Delete currently loaded map
        
        Destroy(GameObject.Find("TrackParent"));
        
	//Sometimes map won't be loaded but TrackParent is still destroyed. I guess this is some sort of async issue??? Putting it into a coroutine with a delay to when ES3.Load is called fixes it.
	
        StartCoroutine(loadMap());
        
        //Hide load dialogue screen
        ToggleLoadDialogue();
The coroutine in which I call ES3.Load

Code: Select all

    IEnumerator loadMap()
    {
        yield return new WaitForSeconds(1f); //Test delay
        
        GameObject parent = (ES3.Load<GameObject>(savedTracks.options[savedTracks.value].text));
        parent.transform.position = Vector3.zero;

    }
If anyone could point me in the right direction/tell me what I'm doing wrong, that would be great!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to save a transform with child objects, but having difficulty.

Post by Joel »

Hi there,

Please could you replicate this in a new, blank project and private message it to me with instructions so I can see what is happening at my end?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Ducktor_Cid
Posts: 2
Joined: Tue May 25, 2021 12:39 am

Re: Trying to save a transform with child objects, but having difficulty.

Post by Ducktor_Cid »

Hey Joel. Thanks for the help but I was able to fix it on my own this morning.

To fix the rigidbody issue, I just checked in the script if it's still null after trying to get the component, if so then just wait a few frames before trying again which seems to have fixed it.

As per the pink object bug, that was a bug on my end because in my game I have a script for runtime transform gizmos that let me manipulate objects like I would in editor. When selected, the object gets an extra material and if a map is saved while a object is selected, the outline material will not exist when the map is loaded again which is why the object loads in pink, because it's missing the outline material.

To fix this, I just call an event through my custom event manager just before I call ES3.Save where the transform gizmos script will deselect all objects and clear the outline materials on them which fixes the pink object bug.

I'll let you know if any other bugs occur, but for now it seems to be working perfectly!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to save a transform with child objects, but having difficulty.

Post by Joel »

Glad you managed to resolve your issues :)

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