Modding:Block Entity: Difference between revisions

From Vintage Story Wiki
Replaced deprecated (1.9?) tick listener definition
m (Double checked information and updated version)
(Replaced deprecated (1.9?) tick listener definition)
Tags: Mobile edit Mobile web edit
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
{{GameVersion|1.15}}
{{GameVersion|1.16}}


= Introduction =
= Introduction =
Line 44: Line 44:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public int timer;
         public float timer;
</syntaxhighlight>
</syntaxhighlight>


Line 50: Line 50:


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


The timer itself should increase per tick. It ticks 20 times per seconds, meaning 3 seconds have passed after the timer has reached 60. So if it's greater than 60 it should replace the block:
The timer itself should increment by dt, the time difference in seconds between the current tick and the previous tick. It ticks about every 50ms or less often if the game is slow. So if the timer is greater than 3, it should replace the block:


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public void OnTick(float par)
         public void OnGameTick(float dt)
         {
         {
             timer++;
             timer += dt;
             if(timer > 60)
             if(timer >= 3)
             {
             {
                 Block block = Api.World.BlockAccessor.GetBlock(Pos);
                 Block block = Api.World.BlockAccessor.GetBlock(Pos);
Line 97: Line 97:
         {
         {
             base.ToTreeAttributes(tree);
             base.ToTreeAttributes(tree);
             tree.SetInt("timer", timer);
             tree.SetFloat("timer", timer);
         }
         }


Line 103: Line 103:
         {
         {
             base.FromTreeAttributes(tree, worldForResolving);
             base.FromTreeAttributes(tree, worldForResolving);
             timer = tree.GetInt("timer");
             timer = tree.GetFloat("timer");
         }
         }


Confirmedusers, editor
749

edits