Constructor code ignored?

Discussion and help for Easy Save 3
Post Reply
BFA
Posts: 8
Joined: Wed Jul 06, 2022 5:52 am

Constructor code ignored?

Post by BFA »

Hi,

I've mostly got saving and loading working in my game, but it seems like when I load a non-unity object class, any changes made by code that executes in its parameterless constructor are subsequently ignored?

Is this by design or am I making a mistake somewhere?

Example:
Class A holds an array of instances of Class B. Class A's constructor subscribes to an action in Class B. On game start, this all works, with Class B's action triggering the subscribed changes in Class A . After loading, however, Class B's action doesn't trigger any changes in Class A (again, despite Class A having subscribed to Class B's action in Class A's constructor, which definitely still runs during loading).

What is the problem?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Constructor code ignored?

Post by Joel »

Hi there,

It's difficult to understand what is happening from what you've described, and I've not been able to replicate it at my end. Please could you create a script which I can drop into a new scene of a new project containing Easy Save which replicates this issue?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BFA
Posts: 8
Joined: Wed Jul 06, 2022 5:52 am

Re: Constructor code ignored?

Post by BFA »

Sorry, yes I can get a project together but I realize my last explanation was jumbled so let me try explaining it a different way.

I have saved a non-object class, MyClass, that includes a variable MyVar. In the save file, MyVar is set to 100. In MyClass’s parameterless constructor, I set MyVar to 500. When I debug.log MyVar from the constructor, it shows as 500 as expected. But when I debug.log MyVar after loading MyClass, it shows as 100. Is this expected behaviour?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Constructor code ignored?

Post by Joel »

Hi there, and thanks for the clarification.

This is expected behaviour as the deserialization of fields happens after the constructor is called, as is necessary at runtime.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BFA
Posts: 8
Joined: Wed Jul 06, 2022 5:52 am

Re: Constructor code ignored?

Post by BFA »

Ah ok, thank you for clarifying. As a follow-up question, it appears that even values I don't save are 'reset' or lost after being loaded.

Example:
In my game I have a City class that holds an instance of a Grid class. The Grid class has a System.Action that the City class subscribes to via a call in the City class's constructor.

On first boot up this all works, and I can save and load the City class just fine (which also saves its Grid instance). I do not save the Grid's System.Action, as it is not serializable and I am trying to avoid custom Types.

Upon loading my saved City class (which also loads its saved Grid instance), the call to subscribe to Grid's System.Action is made in City's constructor, and I confirm that Grid's System.Action has a subscriber via a debug.log immediately after the call is made (still in the constructor).

However, when I check after the City class is loaded, its Grid reports that its System.Action has no subscribers.

is that a result of the same issue? That is, should I be doing this call to subscribe in a separate Init() method or something, after City is finished loading?
BFA
Posts: 8
Joined: Wed Jul 06, 2022 5:52 am

Re: Constructor code ignored?

Post by BFA »

I've noticed that the general pattern of subscribing to actions in a loaded class's constructor actually seems to work elsewhere in our code. The difference seems to be that the City class is trying to subscribe to an action owned by its own member instance of the Grid class (vs subscribing to an action owned by some other, already-loaded class).

Could it be because the City is loaded first (and both builds its Grid and subscribes to that Grid's action in the City constructor), but then - as the Load("city") call continues - its saved member Grid is loaded 'on top of' the newly created Grid, thereby wiping out the action subscribers of the created Grid?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Constructor code ignored?

Post by Joel »

Hi there,

Easy Save won't touch fields/properties which aren't serializable, so this will be left as it's default value unless you assign something to it yourself.

Non-Unity Object classes are loaded by value, so each time you load it will create a new instance of your classes. If your City class subscribes to a Grid and then you load new Grid objects, the City class will be referring to a different object. The way around this is to use LoadInto instead, which allows you to tell Easy Save what instance(s) you want to load the data into, rather than creating a new instance:

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

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BFA
Posts: 8
Joined: Wed Jul 06, 2022 5:52 am

Re: Constructor code ignored?

Post by BFA »

Ok, but in this case I'm not doing a separate Load("Grid") call or anything, the grid is just being loaded via Load("city"), since the City has a member Grid.

I was setting some of that Grid's values in the City's constructor, but after the City was loaded those values reverted to defaults.

If I'm understanding you correctly, that's because the member Grid is loaded after the City's constructor runs, and since I wasn't saving those values, they are returned to defaults at that time (essentially overwriting the non-default values I had assigned in the City's constructor). Is that right?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Constructor code ignored?

Post by Joel »

Hi there,

In this case the Grids will still be loaded after the City, as an instance of the City needs to be created before we are able to load it's fields.

To be able to tell whether this is the case for your project I would need to see a basic project or script which I can use the replicate the issue and see what is happening.

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