Modding:Adding Block Behavior: Difference between revisions
From Vintage Story Wiki
CreativeMD (talk | contribs) No edit summary |
CreativeMD (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
= | = 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? | ||
# Calculate the new position of the block depending on the face the player is looking at | |||
# Check if the block can be placed at this position | |||
# Remove the original block | |||
# 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 | public class MovingBlocks : ModBase | ||
{ | { | ||
public override void Start(ICoreAPI api) | |||
{ | { | ||
base.Start(api); | |||
api.RegisterBlockBehavior("Moving", typeof(Moving)); | |||
} | } | ||
} | } | ||
</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. | |||
You can download the example mod [http://wiki.vintagestory.at/images/c/cb/Moving.zip here]. | |||
== Testing == | == Testing == | ||
= | <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. | |||
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 == |