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

Jump to navigation Jump to search
(→‎What is Serialization?: significant rewrite and editing)
(→‎How Serialization Works for Qud: add using directives)
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 UnityEngine;
</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 UnityEngine;
 
namespace XRL.World.Parts
namespace XRL.World.Parts
{
{

Navigation menu