Loading child objects

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

Re: Loading child objects

Post by Notso »

If I use es3.load<vector3> says can't convert to a quaternion, if I use es3.load<Quaternion> says can't convert to a vector 3

Ideally I need to save the child objects position and rotation only. Nothing else on the doors (and drawers).
Will see if I can make something later. Recovering from a fractured leg, first night back to work and in pain from a 10hr overnight shift and can't focus.
(only been walking without assistance for 3 days now)
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading child objects

Post by Joel »

Unfortunately it's not possible for me to tell what is happening from what you've said. Please could you replicate this in a new script which I can drop into a new scene in a new project.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Notso
Posts: 51
Joined: Sat May 08, 2021 6:53 pm

Re: Loading child objects

Post by Notso »

Sorry, as i mentioned long shifts. I do not have any scripts at the moment for this, was work in progress.
Going to try a little tonight or tomorrow, got sucked into a perforce repo that just wouldn't work right on my server.
I hate perforce but it really is one of the best for team version control once you get it set up correctly.
Notso
Posts: 51
Joined: Sat May 08, 2021 6:53 pm

Re: Loading child objects

Post by Notso »

The main purpose here i am trying to avoid all these save as you go scripts (like activeobjects, destroyedObjects etc...) and save all at once.

So I am now trying to save the position (then rotation with a check box so I can say if it is a door or drawer) of interacted objects.
Drawers use position (slide in and out) and doors use rotation.
So I am trying to modify your positionsave script to create a dictionary or guid,transform.position then when they hit save save it. but I can't figure out how to load.
So I have on my Interactitem script a bool if true it fires off a methos on my position script which is on each scene object so it gets GUID. On my savescript I have a static dictionary the saveposition writes to.

Interactive door (this part works whenever I click to interact with it)

Code: Select all

Debug.Log("I am activating door  ");
    if (isDrawer)
    {
       GetComponent<SavePosition>().DoorPositionChange();
    }
SavePositionScript

Code: Select all

public string guid = System.Guid.NewGuid().ToString();
    [SerializeField] private bool savePositionInstead;
    [SerializeField] public GameObject drawer = null;

public void DoorPositionChange()
    {
        if (savePositionInstead && drawer != null)
        {
            if (NotsoSave.doorValues.ContainsKey(guid))
            {
                NotsoSave.doorValues[guid] = drawer.transform.localPosition;
            }
            else
            {
                NotsoSave.doorValues.Add(guid, drawer.transform.localPosition);
            }
        }
    }
now on the savegame script

Code: Select all

 public static Dictionary<string, Vector3> doorValues
 
 public void Save()
    {
 ES3.Save("Doors", doorValues);
 }
However I can't figure out how to load it back into the objects...
this

Code: Select all

 drawer.transform.localPosition = ES3.Load<Vector3>(guid, drawer.transform.localPosition);
is on your original script but I can't for the life of me figure out how to get that dictionary out and loaded back onto the drawer objects...
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading child objects

Post by Joel »

Hi there,

To load the Dictionary you would do:

Code: Select all

doorValues = ES3.Load<Dictionary<string, Vector3>>("Doors");
You would then need to assign your values back to the appropriate doors.

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

Re: Loading child objects

Post by Notso »

Would that be through the use of a for loop or foreach?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading child objects

Post by Joel »

Notso wrote: Thu Jan 04, 2024 12:11 am Would that be through the use of a for loop or foreach?
You could use either, but as that's a general coding question and a part of your logic rather than ours, I'm afraid I wouldn't be able to assist with that. I recommend Googling for "Looping over a Dictionary in C#" into Google for information on how to do that.

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

Re: Loading child objects

Post by Notso »

I was already looking into those. I know that this part is not part of easy save, so hoping I can still get help.

First to test I used (I made a new method just for testing)

Code: Select all

void DrawerStates()
   {
       doorValues = ES3.Load<Dictionary<string, Vector3>>("Doors");
       
       foreach(KeyValuePair<string, Vector3> kvp in doorValues)
       {
            Debug.Log("Key = {0} + Value = {1}" + kvp.Key + kvp.Value); 
       }
   }
And I did get 2 different GUID and values, as expected.
My problem is how to load those into the objects.
I then got stuck at this point. I know this is wrong or at least a very slow method, but either way it did not fire off the debug.

Code: Select all

void DrawerStates()
   {
       doorValues = ES3.Load<Dictionary<string, Vector3>>("Doors");
       
       foreach(KeyValuePair<string, Vector3> kvp in doorValues)
       {
           var guid = kvp.Key;
           if(guid == FindObjectOfType<SavePosition>().guid)
               Debug.Log(gameObject.name);
          // Debug.Log("Key = {0} + Value = {1}" + kvp.Key + kvp.Value); 
       }
   }
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading child objects

Post by Joel »

You would need to use FindObjectsOfType rather than FindObjectOfType to get an array of all of the SavePosition objects, and then iterate through that array and find the one with that GUID and apply the data to that object.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Notso
Posts: 51
Joined: Sat May 08, 2021 6:53 pm

Re: Loading child objects

Post by Notso »

Will give that a try.
It would be a pin in the ... but would creating a list of the drawers be quicker to reference than the entire scene?
I imagine it would.
Post Reply