Confirmedusers, editor, Administrators
886
edits
CreativeMD (talk | contribs) No edit summary |
|||
(16 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
<languages /> | |||
<translate> | |||
__FORCETOC__ | __FORCETOC__ | ||
= | == Introduction == | ||
Block Behaviors are useful when you want different blocks to act in the same way, as you can attach one or more block behaviors to an arbitrary number of blocks. | |||
You may want to have a look at the existing [[Block Json Properties#p_behaviors|block behaviors]] before implementing your own. | |||
In this tutorial we'll create a new Behavior that we can attach to blocks to make them movable by right clicking them. | |||
== Setting up == | == Setting up == | ||
A [[Setting up | A [[Setting up your Development Environment|development workspace]] is required. Additionally you will need the assets (blocktype, texture and lang file). You can either create your one owns or use those pre-made ones: [https://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip] | ||
== Creating the behavior == | == Creating the behavior == | ||
So first of all we need to create the behavior itself, which is a class extending BlockBehavior | So first of all we need to create the behavior itself, which is a class extending BlockBehavior | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="csharp"> | ||
class Moving : BlockBehavior | class Moving : BlockBehavior | ||
{ | { | ||
Line 33: | Line 37: | ||
# Check if the block can be placed at this position | # Check if the block can be placed at this position | ||
# Remove the original block | # Remove the original block | ||
# Place the new block using the | # Place the new block using the previously calculated position | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | ||
{ | { | ||
// Find the target position | |||
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite()); | BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite()); | ||
// Can we place the block there? | |||
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | ||
{ | { | ||
// Remove the block at the current position and place it at the target position | |||
world.BlockAccessor.SetBlock(0, blockSel.Position); | world.BlockAccessor.SetBlock(0, blockSel.Position); | ||
world.BlockAccessor.SetBlock(block.BlockId, pos); | world.BlockAccessor.SetBlock(block.BlockId, pos); | ||
} | } | ||
// Notify the game engine other block behaviors that we handled the players interaction with the block. | |||
// If we would not set the handling field the player would still be able to place blocks if he has them in hands. | |||
handling = EnumHandling.PreventDefault; | handling = EnumHandling.PreventDefault; | ||
return true; | return true; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Register == | == Register == | ||
Line 64: | Line 65: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public class MovingBlocks : | public class MovingBlocks : ModSystem | ||
{ | { | ||
Line 70: | Line 71: | ||
{ | { | ||
base.Start(api); | base.Start(api); | ||
api. | api.RegisterBlockBehaviorClass("Moving", typeof(Moving)); | ||
} | } | ||
Line 76: | Line 77: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Distribution == | |||
In order to finish everything, open the modtools and type in <code>pack <your mod id></code>. Now you can take the zip file and share it with other people. | |||
* for VS 1.9: [https://wiki.vintagestory.at/images/2/2a/Moving_v1.0.0.zip Moving_v1.0.0.zip] | |||
* for VS 1.6: [https://wiki.vintagestory.at/images/c/cb/Moving.zip Moving.zip] | |||
== Testing == | == Testing == | ||
Line 173: | Line 176: | ||
The block will be pushed two blocks instead of one and the player can pull it by sneaking while right clicking. | The block will be pushed two blocks instead of one and the player can pull it by sneaking while right clicking. | ||
* for VS 1.9: [https://wiki.vintagestory.at/images/7/7b/Advancedmoving_v1.0.0.zip AdvancedMoving_v1.0.0.zip] | |||
* for VS 1.6: [https://wiki.vintagestory.at/images/7/72/AdvancedMoving.zip AdvancedMoving.zip] | |||
{{Navbox/modding|Vintage Story}} | |||
</translate> |