Advertisement
Guest User

Untitled

a guest
Feb 6th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. using Godot;
  2. using System;
  3. using System.Diagnostics;
  4.  
  5. public class TickMaster : Godot.Node
  6. {
  7. int CurrTick = 0;
  8.  
  9. int SupposedTickInt = 0;
  10. double SupposedTickDecims = 0f;
  11.  
  12. bool NextPause = false;
  13.  
  14. ulong WatchLastTime = 0;
  15.  
  16. bool First = true;
  17.  
  18. public override void _PhysicsProcess(float delta)
  19. {
  20.  
  21. ++CurrTick;
  22.  
  23. if(NextPause)
  24. {
  25. Wait(1000);
  26. NextPause = false;
  27.  
  28. GD.Print("Paused.");
  29. }
  30.  
  31. if(Input.IsActionJustPressed("Test_Key_2"))
  32. {
  33. NextPause = true;
  34. GD.Print("Pausing!");
  35. }
  36.  
  37. if(CurrTick % 60 != 0) return;
  38.  
  39. if(First)
  40. {
  41. First = false;
  42.  
  43. SupposedTickInt = CurrTick;
  44. }
  45.  
  46. GD.Print($"Current tick is {CurrTick}, based on time, it's supposed to be {SupposedTickInt}.{(int)(SupposedTickDecims * 1000)}! {SupposedTickInt - CurrTick} difference!");
  47. //GD.Print($"Current tick is {SupposedTick}, it's supposed to be {SupposedTickDecims}!");
  48. }
  49.  
  50. long SupposedThreasold = 0;
  51.  
  52. public override void _Process(float delta)
  53. {
  54. ulong now = OS.GetTicksUsec();
  55.  
  56. SupposedTickDecims += ((double)(now - WatchLastTime) / 1000000) * 60;
  57.  
  58. WatchLastTime = now;
  59.  
  60. while(SupposedTickDecims >= 1f)
  61. {
  62. SupposedTickDecims -= 1f;
  63. ++SupposedTickInt;
  64. }
  65.  
  66. /*if(SupposedThreasold <= SupposedTickInt)
  67. {
  68. GD.Print($"Current tick should be {SupposedTickInt}.{(int)(SupposedTickDecims * 1000)}!");
  69. SupposedThreasold += 60;
  70. }*/
  71. }
  72.  
  73. void Wait(uint millis)
  74. {
  75. ulong now = OS.GetTicksUsec();
  76.  
  77. ulong then = now + millis * 1000;
  78.  
  79. while(now < then)
  80. {
  81. now = OS.GetTicksUsec();
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement