Modding:TreeAttribute: Difference between revisions

From Vintage Story Wiki
mNo edit summary
m (Updated navbox to new code navbox.)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{GameVersion|1.15}}
{{GameVersion|1.19.3}}
<languages/><translate>
<languages/><translate>
A TreeAttribute is a nested data structure whose primary purpose is to store data. 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).  
<!--T:1-->
A TreeAttribute is a nested data structure whose primary purpose is to represent the tree attribute [[Modding:Serialization_Formats|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).  


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


To use TreeAttribute, add the using statement for '''Vintagestory.API.Datastructures'''.<br><br>
To use TreeAttribute, add the using statement for '''Vintagestory.API.Datastructures'''.
== Use in API ==
== Use in API == <!--T:3-->
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'''.<br>
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 ==  
== Example in code == <!--T:4-->
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
using Vintagestory.API.Datastructures;
using Vintagestory.API.Datastructures;


class Example
<!--T:5-->
{
public class TreeAttributeMod : ModSystem
    Example()
     {
     {
         // Setting and getting values
         public override void StartServerSide(ICoreServerAPI api)
        TreeAttribute tree;
        {
        tree = new TreeAttribute();
            // Setting and getting values
            TreeAttribute tree;
            tree = new TreeAttribute();


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


        // Set the value
            // Set the value
        tree.SetFloat(someKey, someValue);
            tree.SetFloat(someKey, someValue);
        // Retrieve the value
            // Retrieve the value
        tree.GetFloat(someKey);
            tree.GetFloat(someKey);
        }
     }
     }
}
</syntaxhighlight>
</syntaxhighlight>
For documentation, see [http://apidocs.vintagestory.at/api/Vintagestory.API.Datastructures.TreeAttribute.html TreeAttribute] and [http://apidocs.vintagestory.at/api/Vintagestory.API.Datastructures.ITreeAttribute.html ITreeAttribute].
For documentation, see [http://apidocs.vintagestory.at/api/Vintagestory.API.Datastructures.TreeAttribute.html TreeAttribute] and [http://apidocs.vintagestory.at/api/Vintagestory.API.Datastructures.ITreeAttribute.html ITreeAttribute].
</translate>
</translate>
{{Navbox/modding|Vintage Story}}
{{Navbox/codemodding}}

Latest revision as of 17:06, 27 March 2024

This page was last verified for Vintage Story version 1.19.3.

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.

Code Modding
Basics Code Mods Preparing For Code Mods Creating A Code Mod
Tutorials
Advanced Server-Client Considerations Setting up your Development Environment Advanced Blocks Advanced Items Block and Item Interactions Block Behavior Block Entity Particle Effects World Access Inventory Handling Commands GUIs Network API Monkey patching (Harmony)
Data Management VCDBS format Savegame Moddata ModConfig File Chunk Moddata Serialization Formats TreeAttribute
Worldgen WorldGen API NatFloat EvolvingNatFloat
Rendering Shaders and Renderers
Icon Sign.png

Wondering where some links have gone?
The modding navbox is going through some changes! Check out Navigation Box Updates for more info and help finding specific pages.

Modding
Modding Introduction Getting Started Theme Pack
Content Modding Content Mods Developing a Content Mod Basic Tutorials Intermediate Tutorials Advanced Tutorials Content Mod Concepts
Code Modding Code Mods Setting up your Development Environment
Property Overview ItemEntityEntity BehaviorsBlockBlock BehaviorsBlock ClassesBlock EntitiesBlock Entity BehaviorsCollectible BehaviorsWorld properties
Workflows & Infrastructure Modding Efficiency TipsMod-engine compatibilityMod ExtensibilityVS Engine
Additional Resources Community Resources Modding API Updates Programming Languages List of server commandsList of client commandsClient startup parametersServer startup parameters
Example ModsAPI DocsGitHub Repository