Custom type constructor confusion

Discussion and help for Easy Save 3
Post Reply
caleidon
Posts: 16
Joined: Mon Dec 28, 2020 3:02 pm

Custom type constructor confusion

Post by caleidon »

Hello!

I am confused as to what the

Code: Select all

protected override object ReadObject<T>
is supposed to do and how it is used when creating custom supported types through the Types menu in the Editor.

As an example, I have a Region class, and I am only interested in saving the data it contains: RegionID, RegionRect and LocationData.

When adding it as a supported type, this code snippet gets generated:

Code: Select all

protected override object ReadObject<T>(ES3Reader reader)
		{
			var instance = new Region();
			ReadObject<T>(reader, instance);
			return instance;
		}
Since my class has a constructor, it throws an error and requires that I add the parameters required by the Region(int regionID, Rect regionRect) constructor. However, why would I need to do that since I only need to the class data?

What I have been doing, and works, but very much feels like a workaround because of my lack of understanding, is creating a new, empty constructor,

Code: Select all

public Region()
    {
    }
, and then it no longer throws an error, and everything works as expected.

How is this supposed to be used, could you give me some concrete example? Thank you!
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom type constructor confusion

Post by Joel »

Hi there,
Since my class has a constructor, it throws an error and requires that I add the parameters required by the Region(int regionID, Rect regionRect) constructor. However, why would I need to do that since I only need to the class data?
Because your class doesn't have a parameterless constructor, and it would be impossible for Easy Save to know what data to pass to the constructor.

By providing a parameterless constructor you provide a way for Easy Save to create an instance of your class without needing to pass parameters to a constructor.

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