Page 1 of 1

Attributes to handle changes in variable names

Posted: Tue May 05, 2020 6:21 am
by Joel
Status

Requested

Complexity

3/10

Description

When the name of a variable in a script changes, this invalidates the save data for it. We could account for changes by adding an attribute which specifies a persistent field name:
[ES3FieldName("myVariable")]
public int myVariable;
If the name of the variable were to change, the value could still be retrievable, as the attribute value would be the same:
[ES3FieldName("myVariable")]
public int myNewVariable;
There would be a performance impact to performing this check for each field, so this will need to be weighed up against the benefits. This would also not be possible for classes for which an explicit ES3Type exists.

Currently this can be achieved (albeit with more effort) by creating an ES3Type for the class containing the variable, and manually modifying the Read and Write of that variable to use the original variable name. I.e.
// Inside WriteComponent(obj,writer)
writer.WriteProperty("oldVariableName", instance.newVariableName, ES3Type_int.Instance);

// Inside ReadComponent<T>(reader, obj)
case "oldVariableName":
	instance.newVariableName = reader.Read<System.Single>(ES3Type_float.Instance);
	break;
For more information on creating ES3Types, see Creating an ES3Type.