Handling reference types
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Handling reference types
I have noticed that easy save create a new individual objects for reference types.
I want to know easy save can preserve reference?
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 :)
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 :)
Re: Handling reference types
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
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
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Re: Handling reference types
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?
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?
Re: Handling reference types
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?Also loadInto doesn't load the data to existing objects but rather create new objects and point the reference to it.
All the best,
Joel
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Re: Handling reference types
sure, I will send it to you tomorrow.
Re: Handling reference types
Hi there,
Thanks for sending over your project. I've tested it and they both appear to work in the same way? I.e.
Joel
Thanks for sending over your project. I've tested it and they both appear to work in the same way? I.e.
- I press 'L' to save the List<string>.
- I press 'C' to change the List<string>.
- I press 'K' to load the List<string>.
- Both Lists are exactly the same after loading.
Joel
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Re: Handling reference types
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)
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)
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Re: Handling reference types
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.
for your own interest you can check it out though.
thanks for the help.
Re: Handling reference types
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.
All the best,
Joel
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"));
Joel
-
- Posts: 20
- Joined: Tue Apr 20, 2021 4:34 am
Re: Handling reference types
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
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