Do I need to create a parameterless constructor?

Discussion and help for Easy Save 3
Post Reply
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Do I need to create a parameterless constructor?

Post by Keeko »

I was creating a custom ES3Type for a list of objects I had called inventoryItem.

This inventory item had 1 single constructor that took an int and a serializable object. When I generated the script for it, the read first read method was attempting to create an instance of the InventoryItem with no constructor parameters. This was causing an error. I went and added a constructor to my inventoryItem with nothing in it and now everything works.

Do I have to do this for every class I want to serialise?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Do I need to create a parameterless constructor?

Post by Joel »

Hi there,

As mentioned in the Supported Types guide, a parameterless constructor is required to automatically serialise a class. Alternatively you could modify the ES3Type file to use your constructor, but adding a parameterless constructor tends to be easier.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Re: Do I need to create a parameterless constructor?

Post by Keeko »

Hi,

If i didn't want to add a blank constructor, what would I need to do to get this working?

Code: Select all

		protected override void ReadObject<T>(ES3Reader reader, object obj)
		{
			var instance = (InventoryItem)obj;
			foreach(string propertyName in reader.Properties)
			{
				switch(propertyName)
				{
					
					case "quantity":
						instance.quantity = reader.Read<System.Int32>(ES3Type_int.Instance);
						break;
					case "itemReference":
						instance.itemReference = reader.Read<Item>();
						break;
					default:
						reader.Skip();
						break;
				}
			}
		}

		protected override object ReadObject<T>(ES3Reader reader)
		{
			var instance = new InventoryItem();
			ReadObject<T>(reader, instance);
			return instance;
		}
		
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Do I need to create a parameterless constructor?

Post by Joel »

Hi there,

You would need to modify the ReadObject<T>(ES3Reader reader) method so that it uses your constructor with suitable default parameters instead of using the parameterless constructor.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Keeko
Posts: 17
Joined: Sun Nov 15, 2020 9:18 pm

Re: Do I need to create a parameterless constructor?

Post by Keeko »

OK, thanks :)
Post Reply