Confirmedusers
261
edits
Mirotworez (talk | contribs) (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 | 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--> | ||
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--> | ||
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--> | ||
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--> |