Modding:Adding Block Behavior: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
__FORCETOC__
__FORCETOC__


= Moving =
= Basic Behavior =


Let's create a behavior which makes a block move on right click.
Let's create a behavior which makes a block move on right click.
Line 30: Line 30:
What should it do?
What should it do?


1. Calculate the new position of the block depending on the face the player is looking at
# Calculate the new position of the block depending on the face the player is looking at
2. Check if the block can be placed at this position
# Check if the block can be placed at this position
3. Remove the original block
# Remove the original block
4. Place the new block using the previous calculate position
# Place the new block using the previous calculate position


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
Line 49: Line 49:
</syntaxhighlight>
</syntaxhighlight>


----
<syntaxhighlight lang="c#">
handling = EnumHandling.PreventDefault;
</syntaxhighlight>
This is used to prevent all further actions. For example if you hold a block in hand it will no longer place it.
== Register ==


In order the register the BlockBehavior we need to create a mod class, override <code>Start(ICoreAPI)</code> and register it with the given name:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
     public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)
     public class MovingBlocks : ModBase
     {
     {
        BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite());
 
         if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block))
         public override void Start(ICoreAPI api)
         {
         {
             world.BlockAccessor.SetBlock(0, blockSel.Position);
             base.Start(api);
             world.BlockAccessor.SetBlock(block.BlockId, pos);
             api.RegisterBlockBehavior("Moving", typeof(Moving));
         }
         }
        handling = EnumHandling.PreventDefault;
 
        return true;
     }
     }
</syntaxhighlight>
</syntaxhighlight>


Now all you need to do is to put the *.cs file in the zip file as well and you are ready to test/ distribute your mod.


== Register ==
You can download the example mod [http://wiki.vintagestory.at/images/c/cb/Moving.zip here].


== Testing ==
== Testing ==


= Second Behavior (using custom properties) =
<youtube>8eVG0uQF2xs</youtube>
 
= Advanced Behavior =
 
Our behavior is still rather simple, but there are a lot more possibilities. A behavior can have special properties, which can be defined by the blocktype itself.
 
== Example ==
 
The behavior liquid supports some special properties as shown in this example of the water blocktype:
 
<syntaxhighlight lang="json">
behaviors: [
{
name: "FiniteSpreadingLiquid",
properties:
{
spreadDelay: 150,
liquidCollisionSound: "hotmetal",
sourceReplacementCode: "obsidian",
flowingReplacementCode: "basalt"
}
}
],
</syntaxhighlight>
 
== Parsing properties ==
 
In order to take care of special properties there is a method called <code>Initialize(JsonObject)</code>. Each blocktype creates a new instance of the behavior, so the method can be used to parse the properties.


Still trying to find an idea.
So what kind of properties could we add?
* push distance
* pull block if player is sneaking
 
First of all, we need to override the method in our block behavior class ...
 
<syntaxhighlight lang="c#">
        public override void Initialize(JsonObject properties)
        {
            base.Initialize(properties);
        }
</syntaxhighlight>
 
Additionally we need to add two fields, one for the distance and another one if the player should pull the block while sneaking ...
 
<syntaxhighlight lang="c#">
        public int distance = 1;
        public bool pull = false;
</syntaxhighlight>
 
== Adding another block ==
 
== Testing ==
Confirmedusers, editor, Administrators
886

edits