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?
Do I need to create a parameterless constructor?
Re: Do I need to create a parameterless constructor?
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
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
Re: Do I need to create a parameterless constructor?
Hi,
If i didn't want to add a blank constructor, what would I need to do to get this working?
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;
}
Re: Do I need to create a parameterless constructor?
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
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