Can i save the abastract class with ES2_Type?

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
popeyaho
Posts: 2
Joined: Tue Aug 07, 2018 10:21 am

Can i save the abastract class with ES2_Type?

Post by popeyaho »

Hi there, as the title, can I save the class like them to ES2 and store to file *.txt, I using the manager types but I can not found the class HitObject, I'm newer, s.o can help! <3

public abstract class HitObject
    {
        public Vector3 Position;
        public int StartTime;
        public int EndTime;
        public int HitSound;
        //public List<int> Addition { get; set; } = new List<int>();
        public int MaxCombo = 1;

        public HitObject(Vector3 position, int startTime, int endTime, int hitSound)
        {
            Position = position;
            StartTime = startTime;
            EndTime = endTime;
            HitSound = hitSound;
        }
    }
public abstract class StandardHitObject : HitObject
    {
        public bool IsNewCombo;

        public StandardHitObject(Vector3 position, int startTime, int endTime, int hitSound, bool isNewCombo)
            : base(position, startTime, endTime, hitSound)
        {
            IsNewCombo = isNewCombo;
        }
    }
public class StandardSpinner : StandardHitObject
    {
        public StandardSpinner(Vector2 position, int startTime, int endTime, int hitSound, bool isNewCombo)
            : base(position, startTime, endTime, hitSound, isNewCombo)
        {
        }
    }
public class StandardSlider : StandardHitObject
    {
        public CurveType CurveType;
        public List<Vector3> SliderPoints = new List<Vector3>();
        public int Repeats;
        public float PixelLength;

        //public int EdgeHitSound { get; set; }
        //public List<int> EdgeAddition { get; set; }

        public StandardSlider(Vector3 position, int startTime, int endTime, int hitSound, bool isNewCombo, CurveType type,
            List<Vector3> points, int repeats, float pixelLength) : base(position, startTime, endTime, hitSound, isNewCombo)
        {
            CurveType = type;
            SliderPoints = points;
            Repeats = repeats;
            PixelLength = pixelLength;
        }
    }

User avatar
Joel
Moodkie Staff
Posts: 3462
Joined: Wed Nov 07, 2012 10:32 pm

Re: Can i save the abastract class with ES2_Type?

Post by Joel »

Hi there,

Easy Save 2 does not natively support abstract classes (though Easy Save 3 does), so you will need to create a custom ES2_Type.

I'll give you an example of how you would do this. Let's say we have an abstract and two concrete classes called AbstractClass and ConcreteClass1 and ConcreteClass2 which look like this:
public abstract class AbstractClass
{
	public int abstractField = 1;
}

public class ConcreteClass1 : AbstractClass
{
	public int concrete1Field = 1;
}

public class ConcreteClass2 : AbstractClass
{
	public float concrete2Field = 1;
}
First, you should add support for your ConcreteClass1 and ConcreteClass2 in Assets > Easy Save 2 > Manage Types (see Automatically Adding Support for a Type here).

Then, you would need to manually create a script called ES2Type_AbstractClass.cs in Assets/Easy Save 2/Types/ to manage these classes. This would look something like this:
public class ES2UserType_AbstractClass : ES2Type
{
	public override void Write(object obj, ES2Writer writer)
	{
		AbstractClass data = (AbstractClass)obj;

		// Get the correct ES2Type.
		var es2Type = ES2TypeManager.GetES2Type(data.GetType());
		// Write the ID of the ES2Type so we know which one to use to load.
		writer.Write(es2Type.hash);
		// Use the ES2Type to write the data for the concrete class.
		es2Type.Write (data, writer);
	}
	
	public override object Read(ES2Reader reader)
	{
		var es2Type = ES2TypeManager.GetES2Type(reader.Read<int>());
		return es2Type.Read(reader);
	}

	public override void Read(ES2Reader reader, object c)
	{
		var es2Type = ES2TypeManager.GetES2Type(reader.Read<int>());
		es2Type.Read(reader, c);
	}
	
	public ES2UserType_AbstractClass():base(typeof(AbstractClass)){}
}
Finally, you will need to go to Assets > Easy Save 2 > Manage Types and press Refresh ES2Init to register your new type.

Once you've done this, you can save and load an array of AbstractClass containing both ConcreteClass1 and ConcreteClass2 like any other array:
ES2.Save(abstractArray, "abstractArray");
abstractArray = ES2.LoadList<AbstractClass>("abstractArray");
All the best,
Joel
Joel @ Moodkie Interactive
Buy Easy Save | Contact | Guides | Docs | Getting started

popeyaho
Posts: 2
Joined: Tue Aug 07, 2018 10:21 am

Re: Can i save the abastract class with ES2_Type?

Post by popeyaho »

Thank you for the help, seem ES2 and ES3 cannot search the abstract class in manager types. I'll try

User avatar
Joel
Moodkie Staff
Posts: 3462
Joined: Wed Nov 07, 2012 10:32 pm

Re: Can i save the abastract class with ES2_Type?

Post by Joel »

Hi there,

Automatic types cannot be generated for abstract types, so you will need to create the file manually.

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

Locked