ES3.Save makes Unity crash

Discussion and help for Easy Save 3
Post Reply
Passero
Posts: 10
Joined: Mon Oct 07, 2019 12:05 pm

ES3.Save makes Unity crash

Post by Passero »

I'm starting to use ES3 and I already created an object that contains most of the data that I want to save.
The type of my data container is Dictionary<string, Dictionary<string, IDataClass>>
IDataClass is my own and I have several of these in the dictionary.

What could be causing the crash? I don't see anything in the Unity console before it crashes so not sure how to troubleshoot.

I inspected my object before saving and it's not recursive so it couldn't get into an infinity loop.
IDataClass does contain references to prefabs so not sure if that could be an issue?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES3.Save makes Unity crash

Post by Joel »

Hi there,

It wouldn't be possible for me to tell from what you've said. Please could you PM me a basic project and instructions to replicate this?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Passero
Posts: 10
Joined: Mon Oct 07, 2019 12:05 pm

Re: ES3.Save makes Unity crash

Post by Passero »

After some research it looks there is some recursive reference.
I changed some and now I get a stackoverflow instead of a crash so it's improving...

It's difficult to create a project as I'm already far into my project before I integrated ES3 so that's probably my issue as well... I didn't keep serialization in mind while designing my components and references.
Passero
Posts: 10
Joined: Mon Oct 07, 2019 12:05 pm

Re: ES3.Save makes Unity crash

Post by Passero »

So, I manage to fix some things but I notice it's not easy...

For example I have a lot of references to components instead of gameObjects and that's one of the issues.
If I replace something like this:

public MyComponent comp

with

public GameObject go

MyComponent = go.getComponent<MyComponent>();

it is able to save without crashing or causing a stackoverflow.
Why doesn't ES3 handle component references gracefully?

I also have a lot of "normal" classes that aren't components and reference those in my gameObjects and other classes. It are these references that are causing issues as ES3 appears to not save them as reference but instead is always serializing the entire object so if I have:

Code: Select all

public class MyClass {
   public OtherClass otherClass;
}
public class OtherClass {
  public MyClass myClass;
}
This is causing ES3 to crash as it creates an infinite loop between those two instead of keeping track of a reference of those.

I either have to convert all of these classes to components and change the reference to GameObject and use the getComponent method...
A lot of work...

Any other, better solution for this?

Here's a concrete example:

In the economical aspect of my game, a planet has several companies who can write out orders. A company also has a bankAccount to receive payment. Other companies can fulfill those orders and pay so the model looks like this:
Each company also has a Container to store inventory so they can sell it.
The planet itself can also write out orders so I have created an ICompany interface for that purpose:

Planet : MonoBehavior : ICompany
- List<Company> companies
- List<Order> orders;
- BankAccount account;
- Container inventory

ICompany (interface)
getContainer();
getBankAccount();

Company : ICompany (normal class)
- List<Order> orders
- BankAccount account
- Container inventory

Order (normal class)
- Company issuer
- Company fulfiller

Transaction (normal class)
- Order order
- Company from
- Company to

BankAccount (normal class)
- List<Transaction> transactions
- Company company

This is not 100% as I implemented it but it conveys the message.
From the above, ES3 will create a recursive flow between Order and Company as they reference each other.

I could fix this by changing Company to be a MonoBehavior as well and create an empty GameObject as child of the planet to attach it to.
I would have to do the same for containers and attach them to a planet or company gameObject.

This is taking the component approach to the extreme imo and overkill.
If that's the only solution I'm probably looking at days of work to refactor all this.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: ES3.Save makes Unity crash

Post by Joel »

Hi there,

Unity's Editor also has the same issue regarding recursive references, and their main suggestion is to create a List of your objects, and then instead of storing the object itself, store the index of this object in the List (this assumes that objects are always in the same order in the List).

You could also achieve the same thing by maintaining a Dictionary of your objects and giving them each a unique ID (as long as the unique ID remains the same between sessions), and then store this ID. Essentially you are creating your own very simple reference manager for your objects.

Unity suggests that you change your fields so that they contain this index/unique ID so that it is this which gets serialised. This works in Easy Save too, but you might find it easier to create an ES3Type for your class and modify the Read and Write calls so that it stores the index/unique ID instead. This means you don't need to make any modifications to your classes.

A quick note, we are currently working on changing the reference manager so that it's possible to reference any reference type. You would simply need to call ES3ReferenceMgr.Add("myUniqueID", myObject) to add it to the reference manager. Ideally we want to get this in the next update, but we're currently waiting to hear back from Unity on some workarounds to issues at their end.

Another quick note, the performance impact of detecting and stopping cyclic references is much too high at runtime, which is why we don't currently do so.

Please let me know if you require any further clarification.

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