How to instantiate the game state?

Discussion and help for Easy Save 3
Post Reply
PoleaxeGames
Posts: 1
Joined: Thu Sep 22, 2022 11:24 am

How to instantiate the game state?

Post by PoleaxeGames »

Suppose I have the following:

Code: Select all

public class EntityManager
{
 static EntityManager globalEntityManagerInstance;
 public List<Entity> entities = new List<Entity>();
}

public class Entity
{
public int health;
public void OnLoad {Debug.Log("Loaded");}
}

EntityManager.globalEntityManagerInstance = new EntityManager();
Entity e = new Entity();
e.health = 1234;
EntityManager.globalEntityManagerInstance.entities.Add(e);

QUESTIONS:
1. To save this non-Monobehavior class, it is correct to write ES3.Save("EntityManager", EntityManager.globalEntityManagerInstance); ?
2. When loading, EntityManager.globalEntityManagerInstance may or may not already exist. How do I ensure there is exactly one instance of this class?
3. If I use ES3.LoadInto("EntityManager", EntityManager.globalEntityManagerInstance); is that going to overwrite what is already there ?
4. A lot of the times after loading I want to perform some kind of operation - in the above example it would be Entity.OnLoad(). How do I do that?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to instantiate the game state?

Post by Joel »

Hi there,
1. To save this non-Monobehavior class, it is correct to write ES3.Save("EntityManager", EntityManager.globalEntityManagerInstance); ?
That's correct.
When loading, EntityManager.globalEntityManagerInstance may or may not already exist. How do I ensure there is exactly one instance of this class?
This can be achieved with an if statement:

Code: Select all

if(EntityManager.globalEntityManagerInstance == null)
    ES3.Save("EntityManager", EntityManager.globalEntityManagerInstance);
3. If I use ES3.LoadInto("EntityManager", EntityManager.globalEntityManagerInstance); is that going to overwrite what is already there ?
This will overwrite the fields, but not create a new instance. For more information please see the API reference:
https://docs.moodkie.com/easy-save-3/es ... -loadinto/
4. A lot of the times after loading I want to perform some kind of operation - in the above example it would be Entity.OnLoad(). How do I do that?
Easy Save is syncrhonous, so just call Entity.OnLoad() immediately after your ES3.Load/LoadInto call.

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