Serialisation – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<syntaxhighlight lang="c#">" to "<enforce>") |
Lou Montana (talk | contribs) m (Text replacement - "</syntaxhighlight>" to "</enforce>") |
||
Line 18: | Line 18: | ||
// process saved data (export, send, save...), in this case data are exported as json string | // process saved data (export, send, save...), in this case data are exported as json string | ||
dataString = context.ExportToString(); | dataString = context.ExportToString(); | ||
</ | </enforce> | ||
=== Deserialisation === | === Deserialisation === | ||
Line 32: | Line 32: | ||
context.ReadValue("key2", integerValue); | context.ReadValue("key2", integerValue); | ||
context.ReadValue("key1", stringValue); | context.ReadValue("key1", stringValue); | ||
</ | </enforce> | ||
Line 50: | Line 50: | ||
// process saved data (export, send, save...), in this case data are saved to "file.bin" | // process saved data (export, send, save...), in this case data are saved to "file.bin" | ||
context.SaveToFile("file.bin"); | context.SaveToFile("file.bin"); | ||
</ | </enforce> | ||
=== Deserialisation === | === Deserialisation === | ||
Line 64: | Line 64: | ||
context.ReadValue("key1", stringValue); | context.ReadValue("key1", stringValue); | ||
context.ReadValue("key2", integerValue); | context.ReadValue("key2", integerValue); | ||
</ | </enforce> | ||
Line 80: | Line 80: | ||
protected float m_fVariable = 33.3; | protected float m_fVariable = 33.3; | ||
}; | }; | ||
</ | </enforce> | ||
==== NonSerialized ==== | ==== NonSerialized ==== | ||
Line 93: | Line 93: | ||
protected float m_fVariable = 33.3; | protected float m_fVariable = 33.3; | ||
}; | }; | ||
</ | </enforce> | ||
=== Advanced === | === Advanced === | ||
Line 122: | Line 122: | ||
} | } | ||
}; | }; | ||
</ | </enforce> | ||
==== SerializationLoad ==== | ==== SerializationLoad ==== | ||
Line 146: | Line 146: | ||
} | } | ||
}; | }; | ||
</ | </enforce> | ||
{{GameCategory|armaR|Modding|Tutorials|Scripting}} | {{GameCategory|armaR|Modding|Tutorials|Scripting}} |
Revision as of 19:18, 30 July 2022
This page talks about SCR_JsonSaveContext/SCR_JsonLoadContext and SCR_BinSaveContext/SCR_BinLoadContext.
JSON
Serialisation
SCR_JsonSaveContext context = new SCR_JsonSaveContext();
string stringValue = "data";
int integerValue = 123;
context.WriteValue("key1", stringValue);
context.WriteValue("key2", integerValue);
// process saved data (export, send, save...), in this case data are exported as json string
dataString = context.ExportToString();
Deserialisation
SCR_JsonLoadContext context = new SCR_JsonLoadContext();
context.ImportFromString(dataString);
string stringValue;
int integerValue;
// order does not matter for JSON as it uses key names
context.ReadValue("key2", integerValue);
context.ReadValue("key1", stringValue);
Binary
Serialisation
SCR_BinSaveContext context = new SCR_BinSaveContext();
string stringValue = "data";
int integerValue = 123;
context.WriteValue("key1", stringValue);
context.WriteValue("key2", integerValue);
// process saved data (export, send, save...), in this case data are saved to "file.bin"
context.SaveToFile("file.bin");
Deserialisation
SCR_BinLoadContext context = new SCR_BinLoadContext();
context.LoadFromFile("file.bin");
string stringValue;
int integerValue;
// order matters for Binary serialisation, as Binary ignores names
context.ReadValue("key1", stringValue);
context.ReadValue("key2", integerValue);
Object Serialisation
Simple
The following class set to serialise will serialise all its properties.
NonSerialized
Adding the NonSerialized() decorator to a field will make the serialisation ignore it.
Advanced
The following methods allow to define a custom serialisation per class. This is useful to avoid saving lengthy yet useless information for loading as well as load values in a certain order.
SerializationSave
If an object has the SerializationSave method defined, the SaveContext will use it and not process object's properties automatically at all.
class MyClass : Managed
{
protected int m_iVariable = 42;
protected string m_sVariable = "Hello there";
protected float m_fVariable = 33.3;
bool SerializationSave(SerializationSaveContext context)
{
if (!context.IsValid())
return false;
context.WriteValue("theString", m_sVariable);
context.WriteValue("integer", m_iVariable);
context.WriteValue("floatingpoint", m_fVariable);
return true;
}
};
SerializationLoad
If an object has the SerializationLoad method defined, the SaveContext will use it and not process object's properties automatically at all.
class MyClass : Managed
{
protected int m_iVariable = 42;
protected string m_sVariable = "Hello there";
protected float m_fVariable = 33.3;
bool SerializationLoad(SerializationLoadContext context)
{
if (!context.IsValid())
return false;
context.ReadValue("theString", m_sVariable);
context.ReadValue("integer", m_iVariable);
context.ReadValue("floatingpoint", m_fVariable);
return true;
}
};