Modding:TreeAttribute

From Vintage Story Wiki
This page contains changes which are not marked for translation.

This page was last verified for Vintage Story version 1.19.8.

Other languages:

A TreeAttribute is a nested data structure whose primary purpose is to represent the tree attribute serialization format in memory. It can hold generic data for most primitives, such as int, string, float, as well as more complex types like ItemStacks and other TreeAttributes (hence a nested structure).

Additionally you can store data as byte[] if it is not supported by the TreeAttribute itself (for this you could use Vintagestory.API.Util.SerializerUtil). Attributes are written and read via keys.

To use TreeAttribute, add the using statement for Vintagestory.API.Datastructures.

Use in API

Within the API, TreeAttribute is often employed to store persistent entity data (items a basket contains, content of a barrel/bucket) and to access data extracted from JSON files. TreeAttribute is often supplied as the interface ITreeAttribute.

Example in code

using Vintagestory.API.Datastructures;

public class TreeAttributeMod : ModSystem
    {
        public override void StartServerSide(ICoreServerAPI api)
        {
            // Setting and getting values
            TreeAttribute tree;
            tree = new TreeAttribute();

            float someValue = 0.35f;
            string someKey = "valueKey";

            // Set the value
            tree.SetFloat(someKey, someValue);
            // Retrieve the value
            tree.GetFloat(someKey);
        }
    }

For documentation, see TreeAttribute and ITreeAttribute.