Modding:Asset Patching in Code: Difference between revisions

From Vintage Story Wiki
(explain better what type of attributes is modified)
(added subtitles and better explanation)
Line 10: Line 10:
Collectible attributes (not to confuse with itemstack attributes) can be modified using examples below:
Collectible attributes (not to confuse with itemstack attributes) can be modified using examples below:


Patch '''bool''' attribute:
==== Primitive ==== <!--T:4-->
In this example we add new boolean attribute:


<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
Line 41: Line 42:
</syntaxhighlight>
</syntaxhighlight>


You can also retrieve '''list''' or '''dictionary''', modify it, then put it back:
==== Complex ==== <!--T:4-->
In this example we retrieve integer list, modify it, then put it back:


<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">

Revision as of 11:41, 25 October 2024

Patching through code is considered more reliable and flexible than JSON_Patching if written correctly. It is more suitable if you want to patch hundreds or thousands of objects at once. Note that most of these examples work only when world is not fully loaded yet.

Attributes

Collectible attributes (not to confuse with itemstack attributes) can be modified using examples below:

Primitive

In this example we add new boolean attribute:

using Newtonsoft.Json.Linq;
using Vintagestory.API.Common;
using Vintagestory.API.Datastructures;

public class WikiExamples : ModSystem
{
    public override void AssetsFinalize(ICoreAPI api)
    {
        foreach (CollectibleObject obj in api.World.Collectibles)
        {
            // Make sure collectible or its code is not null
            if (obj == null || obj.Code == null)
            {
                continue;
            }

            // Make sure attributes are not null
            obj.Attributes ??= new JsonObject(new JObject());
            
            // Make collectible storable on shelf
            obj.Attributes.Token["shelvable"] = JToken.FromObject(true);
        }
    }
}

Complex

In this example we retrieve integer list, modify it, then put it back:

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Vintagestory.API.Common;

public class WikiExamples : ModSystem
{
    public override void AssetsFinalize(ICoreAPI api)
    {
        foreach (CollectibleObject obj in api.World.Collectibles)
        {
            // Make sure collectible or its code is not null
            if (obj == null || obj.Code == null)
            {
                continue;
            }

            // Make sure attribute exists
            if (obj.Attributes != null && obj.Attributes.KeyExists("somelist"))
            {
                // Retrieve list
                List<int> list = obj.Attributes["somelist"].AsObject<List<int>>();
                
                // Add new value
                list.Add(1);

                // Put it back
                obj.Attributes.Token["somelist"] = JToken.FromObject(list);
            }
        }
    }
}