Modding:Serialization (Saving/Loading): Difference between revisions

Jump to navigation Jump to search
m
Woopsed namespace end brace
(→‎What is Serialization?: significant rewrite and editing)
m (Woopsed namespace end brace)
 
(3 intermediate revisions by one other user not shown)
Line 12: Line 12:


== How Serialization Works for Qud ==
== How Serialization Works for Qud ==
This section assumes you have the following <code>using</code> directives at the top of your code, which tell C# where to find the attributes you will be using:
<syntaxhighlight lang="C#">
using System;
using SerializeField = UnityEngine.SerializeField;
</syntaxhighlight>
In many cases, you can simply include the the <code>[Serializable]</code> attribute at the top of your class definition, and Qud's game engine will take care of serializing all of your <code>public</code> class fields for you. Specifically, Qud is capable of correctly serializing all intrinsic types (such as <code>string</code>, <code>int</code>, etc.) as well as containers of those types (such as <code>List<string></code>). If your class only uses intrinsic types, the <code>[Serializable]</code> attribute should be all that you need. Qud will properly save and load your data without further effort. Note that <code>private</code>, <code>protected</code>, and <code>static</code> class fields are not serialized by default. If you want the values associated with private or protected class fields to be retained through a save and load, you need to mark each individual private or protected field with a <code>[SerializeField]</code> attribute. Static fields cannot be serialized in this manner. See [https://docs.unity3d.com/ScriptReference/SerializeField.html the Unity docs] for more information about serializable fields ''(note that the GameObject discussed on that linked page is not related to the GameObject type commonly used in Qud)''.
In many cases, you can simply include the the <code>[Serializable]</code> attribute at the top of your class definition, and Qud's game engine will take care of serializing all of your <code>public</code> class fields for you. Specifically, Qud is capable of correctly serializing all intrinsic types (such as <code>string</code>, <code>int</code>, etc.) as well as containers of those types (such as <code>List<string></code>). If your class only uses intrinsic types, the <code>[Serializable]</code> attribute should be all that you need. Qud will properly save and load your data without further effort. Note that <code>private</code>, <code>protected</code>, and <code>static</code> class fields are not serialized by default. If you want the values associated with private or protected class fields to be retained through a save and load, you need to mark each individual private or protected field with a <code>[SerializeField]</code> attribute. Static fields cannot be serialized in this manner. See [https://docs.unity3d.com/ScriptReference/SerializeField.html the Unity docs] for more information about serializable fields ''(note that the GameObject discussed on that linked page is not related to the GameObject type commonly used in Qud)''.


Here is an example of the basic concepts discussed above:<syntaxhighlight lang="c#">
Here is an example of the basic concepts discussed above:
 
<syntaxhighlight lang="c#">
using System;
using SerializeField = UnityEngine.SerializeField;
 
namespace XRL.World.Parts
namespace XRL.World.Parts
{
{
Line 31: Line 44:
         }
         }
     }
     }
}
</syntaxhighlight>Object fields are not serialized automatically. If your class introduces a new field for an object type or a container of objects, such as a GameObject field that stores a reference to some particular object, a List<LiquidVolume> that stores a list of liquid volumes, or a completely new type of object introduced by your mod, you must implement custom serialization for those object fields, or your object references will no longer be valid after the game is reloaded.
</syntaxhighlight>Object fields are not serialized automatically. If your class introduces a new field for an object type or a container of objects, such as a GameObject field that stores a reference to some particular object, a List<LiquidVolume> that stores a list of liquid volumes, or a completely new type of object introduced by your mod, you must implement custom serialization for those object fields, or your object references will no longer be valid after the game is reloaded.


Navigation menu