Modding:Chunk Moddata: Difference between revisions

From Vintage Story Wiki
m
Bluelightning32 moved page Modding:Chunk Data Storage to Modding:Chunk Moddata without leaving a redirect: Part of translatable page "Modding:Chunk Data Storage"
(Marked this version for translation)
m (Bluelightning32 moved page Modding:Chunk Data Storage to Modding:Chunk Moddata without leaving a redirect: Part of translatable page "Modding:Chunk Data Storage")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
{{GameVersion|?.??}}
{{GameVersion|1.12}}
<languages/><translate>
<languages/><translate>
<!--T:1-->
<!--T:1-->
Line 8: Line 8:


<!--T:3-->
<!--T:3-->
VintageStory allows you to set and retrieve custom data to the world's game save as well as individual chunks. The way it works is actually quite simple!
VintageStory allows mods to set and retrieve custom data for their mod on individual chunks. The way it works is actually quite simple! The custom data is set with [https://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerChunk.html#Vintagestory_API_Server_IServerChunk_SetServerModdata_System_String_System_Byte___ IServerChunk.SetServerModdata] and read with [https://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerChunk.html#Vintagestory_API_Server_IServerChunk_GetServerModdata_System_String_ GetServerModdata]. The first parameter is key of type string to identify the custom data. The intention is for the modid to be used as the key, but really the methods will accept any string as the key.


<!--T:4-->
<!--T:4-->
Custom data is stored as an array of bytes to save space, but we can easily convert any valid C# type to <code>byte[]</code> with a very convenient class in the VintageStory API: [http://apidocs.vintagestory.at/api/Vintagestory.API.Util.SerializerUtil.html <code>SerializerUtil</code>]. This class provides us with two static methods that allow us to quickly <code>Serialize</code> into byte arrays, and <code>Deserialize<T></code> them into other types.
The dictionary values are byte arrays, but there is a utility function to convert any [https://loyc.net/2013/protobuf-net-unofficial-manual.html protobuf annotated] C# type to a <code>byte[]</code> in the VintageStory API: [http://apidocs.vintagestory.at/api/Vintagestory.API.Util.SerializerUtil.html <code>SerializerUtil</code>]. This class provides us with two static methods that allow us to quickly <code>Serialize</code> into byte arrays, and <code>Deserialize<T></code> them into other types.


<!--T:5-->
<!--T:5-->
You can check out the methods for storing and retrieving these arrays of bytes for [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerChunk.html Chunks] and for [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.ISaveGame.html#methods SaveGame]
In addition to attaching custom data to chunks, custom data can be attached to the world. See [https://apidocs.vintagestory.at/api/Vintagestory.API.Server.ISaveGame.html#Vintagestory_API_Server_ISaveGame_GetData_System_String_ ISaveGame.GetData] and [https://apidocs.vintagestory.at/api/Vintagestory.API.Server.ISaveGame.html#Vintagestory_API_Server_ISaveGame_StoreData_System_String_System_Byte___ ISaveGame.StoreData].


==Custom Chunk Data== <!--T:6-->
==Custom Chunk Data== <!--T:6-->
Line 20: Line 20:
<!--T:7-->
<!--T:7-->
In this example mod we'll show you how to store custom data to chunks represented on the server side. We'll create a mod that keeps track of how many times players have died on a given chunk, and this sum of total deaths on the chunk will be used to inflict damage onto players sleeping in it! Spooky!
In this example mod we'll show you how to store custom data to chunks represented on the server side. We'll create a mod that keeps track of how many times players have died on a given chunk, and this sum of total deaths on the chunk will be used to inflict damage onto players sleeping in it! Spooky!
Logically the count of deaths in a chunk is an integer. However, as described earlier, for serialization purposes, that int needs to be stored as a byte array on the chunk. But, an int is more convenient for processing outside serialization purposes. So we'll use a <code>Dictionary<IServerChunk, int></code> to map chunks to the amount of player deaths that occurred on them (which we'll call haunting level). That dictionary will be updated when a player dies, and that dictionary will be read when a player uses a bed.


<!--T:8-->
<!--T:8-->
We'll use a <code>Dictionary<IServerChunk, int></code> to hold a list of chunks and the amount of player deaths that occurred on them (which we'll call haunting level). This list will be updated ingame as players die or use beds, and each chunk's haunting level will fetched from custom data if the chunk ever had such value. When the game saves, we iterate through our <code>Dictionary<IServerChunk, int></code> and store the haunting level for each chunk as persistent custom data!
When the game saves, we iterate through our <code>Dictionary<IServerChunk, int></code>, convert the number of death integers into byte arrays, and attach each byte array to its corresponding chunk. Vintage Story will take care of saving that mod data in the game save file.


==Preparation== <!--T:9-->
==Preparation== <!--T:9-->
Confirmedusers
261

edits