How to create a type for a constructor with a parameter?

Discussion and help for Easy Save 3
Post Reply
RedHairring
Posts: 2
Joined: Tue Jul 23, 2019 2:33 am

How to create a type for a constructor with a parameter?

Post by RedHairring »

Hello, I saw in the documentation that users will need to manually modify the ES3TYPE file if you are attempting to create a type from a class that does not have a parameterless constructor. I read the part where it mentions modifying the read and write portions of the type file, but I'm afraid my C# knowledge isn't at the point where I can do this on my own.

I'm attempting to make a type for this class:

Code: Select all

[System.Serializable]
public class CharacterData
{
    public string characterName;

    public string currentArmor;

    public string currentWeapon;

    public string id;


    public CharacterData (CharacterManager characterManager)
    {
        characterName = characterManager.characterName;
        currentArmor = characterManager.currentArmor;
        currentWeapon = characterManager.currentWeapon;

    }
}
My constructor needs to take in another class as the parameter to read the variables off of the current character game object with the CharacterManager script attached to it. Could I get a little help with what I specifically need to modify in order for the CharacterData type to work?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to create a type for a constructor with a parameter?

Post by Joel »

Hi there,

The simplest way is to add a parameterless constructor to your class so it will be automatically supported. However, if this isn't possible, this is covered in the Manually Modifying an ES3Type section here. However, I'll provide a bit more detail for you.

The template ES3Type file can be generated by going to Window > Easy Save 3 > Types, typing the name of the type into the search field (this is case sensitive), and then selecting the fields that you want to be saved. The ES3Type can then be found in Assets/Easy Save 3/Types/ES3Type_<YOUR TYPE NAME>.cs

Once you open this, you will want to modify the ReadObject<T>(ES3Reader reader) method so that it creates the instance of your class. This method shouldn't be mistaken for the ReadObject<T>(ES3Reader reader, object obj) method. So let's say that we're adding support for ClassA, and it has a constructor which accepts ClassB as a parameter. This might look like this:
protected override object ReadObject<T>(ES3Reader reader)
{
        var classB = new ClassB()
	var instance = new ClassA( classB );
	ReadObject<T>(reader, instance);
	return instance;
}
Or if you need to load ClassB before loading ClassA, you can simply save ClassB separately and load it so that we can use it as the parameter in the constructor. E.g.
protected override object ReadObject<T>(ES3Reader reader)
{
        var classB = ES3.Load<ClassB>("classBKey");
	var instance = new ClassA( classB );
	ReadObject<T>(reader, instance);
	return instance;
}
Easy Save will automatically detect the ES3Type class, so then you can save and load your class the same way as you would with any other data. i.e.
ES3.Save<ClassB>("classBKey", classB);
ES3.Save<ClassA>("classAKey", classA);
classA = ES3.Load<ClassA>("classAKey");
Hope this helps.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
RedHairring
Posts: 2
Joined: Tue Jul 23, 2019 2:33 am

Re: How to create a type for a constructor with a parameter?

Post by RedHairring »

This is exactly the bit of help I needed. Thanks for the example!
Post Reply