Modding:Block Entity: Difference between revisions

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


== Introduction ==
= Introduction =


A block entity is a construct that you can tack onto an existing block to give it additional functionality. Whenever a block should do something on a regular interval or store extra information, such as the contents of a chest block, you need a block entity. It's highly recommend to have read the tutorial about [[Basic Modding|Basic Blocks]] and [[Advanced Blocks|Block Class]] in order to understand this tutorial properly.
A block entity is a construct that you can tack onto an existing block to give it additional functionality. Whenever a block should do something on a regular interval or store extra information, such as the contents of a chest block, you need a block entity. It's highly recommend to have read the tutorial about [[Basic Modding|Basic Blocks]] and [[Advanced Blocks|Block Class]] in order to understand this tutorial properly.
Line 12: Line 13:
</syntaxhighlight>
</syntaxhighlight>


You can download the assets [http://wiki.vintagestory.at/images/d/d4/Ticking_-_No_CS_File.zip here] and place it in your mods directory.
You can download the assets [https://wiki.vintagestory.at/images/d/d4/Ticking_-_No_CS_File.zip here] and place it in your mods directory.


== The BlockEntity ==
== The BlockEntity ==
Line 49: Line 50:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public void onTick(float par)
         public void OnTick(float par)
         {
         {
          
          
Line 61: Line 62:
         {
         {
             base.Initialize(api);
             base.Initialize(api);
             RegisterGameTickListener(onTick, 20);
             RegisterGameTickListener(OnTick, 20);
         }
         }
</syntaxhighlight>
</syntaxhighlight>
Line 68: Line 69:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public void onTick(float par)
         public void OnTick(float par)
         {
         {
             timer++;
             timer++;
             if(timer > 60)
             if(timer > 60)
             {
             {
                 Block block = api.World.BlockAccessor.GetBlock(pos);
                 Block block = Api.World.BlockAccessor.GetBlock(Pos);
                 if (block.Code.Path.EndsWith("-on"))
                 if (block.Code.Path.EndsWith("-on"))
                     block = api.World.GetBlock(block.CodeWithParts("off"));
                {
                     block = Api.World.GetBlock(block.CodeWithParts("off"));
                }
                 else
                 else
                     block = api.World.GetBlock(block.CodeWithParts("on"));
                {
                 api.World.BlockAccessor.SetBlock(block.BlockId, pos);
                     block = Api.World.GetBlock(block.CodeWithParts("on"));
                 }
 
                Api.World.BlockAccessor.SetBlock(block.BlockId, Pos);
             }
             }
         }
         }
Line 106: Line 112:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
     public class Ticking : ModBase
     public class Ticking : ModSystem
     {
     {


Line 112: Line 118:
         {
         {
             base.Start(api);
             base.Start(api);
             api.RegisterBlockEntity("tickingcounter", typeof(TickingBlockEntity));
             api.RegisterBlockEntityClass("tickingcounter", typeof(TickingBlockEntity));
         }
         }


Line 122: Line 128:
Now everything is ready to run the first test:
Now everything is ready to run the first test:


https://youtu.be/QQUibC4H9TI
<youtube>QQUibC4H9TI</youtube>


== Distribution ==
== Distribution ==


There is only one thing left to do and that is 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 mods 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 mods folder.
 
= Mod Download =
* for VS 1.12 (Source only): [https://github.com/anegostudios/vsmodexamples/tree/ac7eeaed597b8a25dcfc2366b9c51cd92850d2b9/Mods/Ticking GitHub]
* for VS 1.9: [https://wiki.vintagestory.at/images/8/80/Ticking_v1.0.0.zip Ticking_v1.0.0.zip]
* for VS 1.6: [https://wiki.vintagestory.at/images/6/65/Ticking.zip Ticking.zip]
 
 


Here is my version: [http://wiki.vintagestory.at/images/6/65/Ticking.zip Ticking.zip]
{{Navbox/modding|Vintage Story}}
47

edits