Modding:Advanced Blocks: Difference between revisions

From Vintage Story Wiki
No edit summary
(20 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
{{GameVersion|1.9}}


It's highly recommended to read [[Basic Block]] first. Additionally this tutorial requires a development environment. If you don't have one already you should read the tutorial [[Setting up a dev environment]].
It's highly recommended to read [[Basic Block]] first. Additionally this tutorial requires a development environment. If you don't have one already you should read the tutorial [[Setting up your Development Environment]].


= Trampoline =
= Trampoline =
Line 17: Line 18:


We will create this class, to give the block the desired functionality, so make sure if you pick a different name it matches the one below.
We will create this class, to give the block the desired functionality, so make sure if you pick a different name it matches the one below.
You can download the assets of the mod [http://wiki.vintagestory.at/images/b/b9/Trampoline_-_No_CS_FILE.zip here].
You can download the assets of the mod [https://wiki.vintagestory.at/images/b/b9/Trampoline_-_No_CS_FILE.zip here].
All you need to do is to place this zip file in your <code>mods</code> directory of your development environment.
All you need to do is to place this zip file in your <code>assets</code> directory in your development project.


== The Block Class ==
== The Block Class ==
Line 24: Line 25:
Now we need to register our class and therefore we need to create a new <code>*.cs</code> file in our project. I'm gonna name it <code>Trampoline.cs</code>.
Now we need to register our class and therefore we need to create a new <code>*.cs</code> file in our project. I'm gonna name it <code>Trampoline.cs</code>.


=== The Mod Base ===
=== The Mod System ===


In order to create a mod your class needs to extend <code>ModBase</code>. This will allow use to register all kinds of stuff, but for now we will stick to our block class example.
In order to create a mod your class needs to extend <code>ModSystem</code>. This will allow use to register all kinds of stuff, but for now we will stick to our block class example.


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
public class TrampolineMod : ModBase
public class TrampolineMod : ModSystem
{
{
      
      
Line 38: Line 39:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
public class TrampolineMod : ModBase
public class TrampolineMod : ModSystem
{
{
     public override void Start(ICoreAPI api)
     public override void Start(ICoreAPI api)
Line 63: Line 64:
This should solve all syntax errors.
This should solve all syntax errors.


So how do we implement a bouncy block? It's pretty helpful to take a look at the [http://apidocs.vintagestory.at/Vintagestory.API/Block.htm api documentation] to find a proper way to implement it.
So how do we implement a bouncy block? It's pretty helpful to take a look at the [https://apidocs.vintagestory.at/api/Vintagestory.API.Common.Block.html api documentation] to find a proper way to implement it.


The method <code>void onEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact)</code> seems to be a good way to implement a bouncy functionality.
The method <code>void onEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact)</code> seems to be a good way to implement a bouncy functionality.
Line 84: Line 85:
public class TrampolineBlock : Block
public class TrampolineBlock : Block
{
{
     public override void onEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact)
     public override void OnEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, Vec3d collideSpeed, bool isImpact)
     {
     {
         if (isImpact && facing.Axis == EnumAxis.Y)
         if (isImpact && facing.Axis == EnumAxis.Y)
         {
         {
            world.PlaySoundAt(tickSound, entity.Pos.X, entity.Pos.Y, entity.Pos.Z);
             entity.Pos.Motion.Y *= -0.8;
             entity.Pos.Motion.Y *= -0.8;
         }
         }
Line 94: Line 96:
</syntaxhighlight>
</syntaxhighlight>


Although this code works already, some sound effects would be rather nice. This will played every time an entity bounces:
Although this code works already, some sound effects would be rather nice. We can implement it by adding a sound link field to our block, which can use to play the <code>game:tick</code> sound.


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
entity.PlayEntitySound("tick", EnumAppSide.Server);
public AssetLocation tickSound = new AssetLocation("game", "tick");
</syntaxhighlight>
 
This <code>tickSound</code> will played every time an entity bounces:
 
<syntaxhighlight lang="c#">
world.PlaySoundAt(tickSound, entity.Pos.X, entity.Pos.Y, entity.Pos.Z);
</syntaxhighlight>
</syntaxhighlight>


Line 110: Line 118:
namespace VSExampleMods
namespace VSExampleMods
{
{
     public class TrampolineMod : ModBase
     public class TrampolineMod : ModSystem
     {
     {
         public override void Start(ICoreAPI api)
         public override void Start(ICoreAPI api)
         {
         {
Line 121: Line 130:
     public class TrampolineBlock : Block
     public class TrampolineBlock : Block
     {
     {
         public override void onEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact)
        public AssetLocation tickSound = new AssetLocation("game", "tick");
 
         public override void OnEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, Vec3d collideSpeed, bool isImpact)
         {
         {
             if (isImpact && facing.Axis == EnumAxis.Y)
             if (isImpact && facing.Axis == EnumAxis.Y)
             {
             {
                 entity.PlayEntitySound("tick", EnumAppSide.Server);
                 world.PlaySoundAt(tickSound, entity.Pos.X, entity.Pos.Y, entity.Pos.Z);
                 entity.Pos.Motion.Y *= -0.8;
                 entity.Pos.Motion.Y *= -0.8;
             }
             }
Line 133: Line 144:
</syntaxhighlight>
</syntaxhighlight>


Of course you can download the file directly [http://wiki.vintagestory.at/images/8/8a/Trampoline.cs Trampoline.cs].
Of course you can download the file directly [https://wiki.vintagestory.at/images/8/8a/Trampoline.cs Trampoline.cs].


== Testing ==
== Testing ==
Line 145: Line 156:
== Distribution ==
== Distribution ==


In order to finish everything, you need to place your *.cs file in the zip archive. Once that is done you can share it with other people. It will work in the same way as ordinary mods, you can install it by copying it into the <code>mods</code> folder.
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. It will work in the same way as ordinary mods, you can install it by copying it into the <code>mods</code> folder.
 
= Mod Download =
 
Here is my version:
* for VS v1.9: [https://wiki.vintagestory.at/images/2/24/Trampoline_vs1.9_v1.0.0.zip Trampoline_vs1.9_v1.0.0.zip]
* for VS v1.5: [https://wiki.vintagestory.at/images/c/ce/Trampoline.zip Trampoline.zip]
 
= Moving Forward =


Here is my version: [http://wiki.vintagestory.at/images/c/ce/Trampoline.zip Trampoline.zip]
Now that you've successfully made an advanced block you can go even further by learning how to utilize '''[[Modding:Block Entity| Block Entities]]''' and how to create your own '''[[Modding:Adding Block Behavior | Block Behaviors]]'''. Both of these tutorials will teach you how to add even more mechanics to your custom blocks.


Or, you can try out making an '''[[Modding:Advanced Items | Advanced Item]]''' if you haven't already.


{{Navbox/modding|Vintage Story}}
{{Navbox/modding|Vintage Story}}
219

edits