Page 1 of 1

ES2 instantiates new objects into empty List on load

Posted: Tue Sep 12, 2017 11:41 pm
by Nerrolken
I'm an Easy Save newbie, and I'm trying to get it up and running in my game. I'm saving single values perfectly, so now I'm trying to save my more complex game data. Everything seems to be working, except that it seems to be erroneously instantiating custom-class objects into a List that was empty at time of saving.

Background: I've got a GNGame class, which includes (among other properties) a List of GNCity objects, each of which includes (among other properties) a List of GNFacility objects. All three are custom object classes (not Monobehaviours, for a number of reasons), and all three have been registered with ES2, with their own "ES2UserType_" files.

I can save the GNGame class and I get no errors. (Note: I am saving it without any GNCity objects. The List has been instantiated, but it's empty, and I've confirmed that repeatedly. And of course, no GNCity objects means no GNFacility objects either.)

When I try to load it again, it goes through a bunch of properties in the GNGame object (bools and strings and ints and even another List of custom classes) and everything works fine, until it gets to that List of GNCity objects. When it hits that line, it suddenly starts instantiating GNCity objects and then instantiating GNFacility objects within them, all using the "ES2UserType_" classes as though they were being loaded from the save. These objects have no data (all ints are 0, etc), and after a few iterations it hits a crash. For example right now I'm getting an "ArgumentOutOfRangeException" on a DateTime property in the 4th GNFacility object, but I've gotten other crashes too. I tried commenting out all of the "Read" calls in the GNFacility class and Unity froze entirely, which I have to assume is from an infinite loop of new empty GNFacility objects being "loaded".

I have scoured my code for anything that could be instantiating cities and facilities on load, and I can't find anything. Plus, it's happening in the "ES2UserType_" classes, so it seems like it's not something I'm doing but rather a problem with the load. I know that these kinds of bugs usually come down to user error, though, so I'll definitely keep looking, but I just wanted to swing by here and see if anyone had any ideas for what could be happening.

Has anyone experienced anything similar?

Re: ES2 instantiates new objects into empty List on load

Posted: Wed Sep 13, 2017 6:39 am
by Joel
Hi there,

I've never heard of anything like this happening before.

Would you be able to create a very basic test project which replicates the error and PM me the link and instructions to replicate it?

All the best,
Joel

Re: ES2 instantiates new objects into empty List on load

Posted: Wed Sep 27, 2017 7:31 pm
by Nerrolken
Turns out after a LOT of experimentation, it was the result of having an unequal number of "Write" and "Read" calls in the "ES2UserType_" file. I didn't want to save certain fields under certain circumstances, so I'd made some of the "Write" lines conditional, trusting that the later "Read" lines would return NULL on load if there was no data. Instead, it threw off the load process, so the data for later fields got messed up. Ensuring that the Read calls weren't made for fields that hadn't had Write called solved the problem.

Re: ES2 instantiates new objects into empty List on load

Posted: Wed Sep 27, 2017 7:50 pm
by Joel
Glad you managed to find the solution to your problem!

All the best,
Joel