Handling reference types

Discussion and help for Easy Save 3
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Handling reference types

Post by Uchiha I_T_H_ »

I have noticed that easy save create a new individual objects for reference types.
I want to know easy save can preserve reference?
Last edited by Joel on Wed Jun 09, 2021 7:32 am, edited 1 time in total.
Reason: Creators of other assets sometimes don't like their assets being mentioned in posts on our forums as it redirects their traffic here, so we edit them out where possible :)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handling reference types

Post by Joel »

Hi there,

Currently we only do this for UnityEngine.Object types because this is the only situation where it's guaranteed to work in Unity.

However, you can achieve the same thing by assigning your own unique ID to each object and saving this instead of the object itself. Then when loading, simply look for the object with that given ID.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Re: Handling reference types

Post by Uchiha I_T_H_ »

its not like that.
Also loadInto doesn't load the data to existing objects but rather create new objects and point the reference to it.
It is also possible as Unity jsonUtility.FromJsonOverwrite() does that.
am I unaware of some features or it is not yet available?
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handling reference types

Post by Joel »

Also loadInto doesn't load the data to existing objects but rather create new objects and point the reference to it.
LoadInto does load data into existing objects and should not create a new instance of the object, and we've had no reports of this happening. Please could you replicate this in a new, blank project with a very simple scene and private message it to me?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Re: Handling reference types

Post by Uchiha I_T_H_ »

sure, I will send it to you tomorrow.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handling reference types

Post by Joel »

Hi there,

Thanks for sending over your project. I've tested it and they both appear to work in the same way? I.e.
  1. I press 'L' to save the List<string>.
  2. I press 'C' to change the List<string>.
  3. I press 'K' to load the List<string>.
  4. Both Lists are exactly the same after loading.
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Re: Handling reference types

Post by Uchiha I_T_H_ »

it should reference to same list object after loading given no new object is created rather values are injected in the existing objects.
check the video I shared or rather try to change the values of the list1 and check if those values reflect on list2( I do that by setting breakpoint you can assert as well)
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Re: Handling reference types

Post by Uchiha I_T_H_ »

Anyway forget about as I have changed my plans and no longer need loadInto.
for your own interest you can check it out though.
thanks for the help.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handling reference types

Post by Joel »

Hi there,

I understand now. This is because you're loading into the EasySaveTest class, not the List. We only load into the object, not it's fields, because there are common situations where this would not be desirable to do so.

For example, let's say that the class we were loading into had a field which holds the weapon that the character is currently holding and how much ammo it has. The user saves their data, and then they pick up a different weapon. If we then loaded into this class at this point and it also loaded into each of the fields as you've described, they would have the weapon they just picked up, but it would have the ammo of the weapon that they saved.

Easy Save previously worked the way you described, and we would very regularly get issues relating to the above situation. Since changing it quite a few years ago, yours is the first message we've had regarding issues of doing it the way we currently do :)

If you do want it to behave that way, you should manually add the items to the array. I.e.

Code: Select all

// To Save
ES3.Save("myList1", myObject.myList1);

// To load
myObject.myList1.Clear();
myObject.myList1.AddRange(ES3.Load<List<string>>("myList1"));
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Uchiha I_T_H_
Posts: 20
Joined: Tue Apr 20, 2021 4:34 am

Re: Handling reference types

Post by Uchiha I_T_H_ »

you mentioned that easy save behaves that way previously. How you guys achieved that?
I got tired searching all over the google on how this can be achieved but I didn't find any.(I found automapper library but it doesn't work or maybe I don't know how to work with it)
Also if possible, How can I get the older version of easy save. I want to know how does that work.
Thanks for the support
Post Reply