idle_and_physics_processing.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. .. _doc_idle_and_physics_processing:
  2. Idle and Physics Processing
  3. ===========================
  4. Games run in a loop. Each frame, you need to update the state of your game world
  5. before drawing it on screen. Godot provides two virtual methods in the Node
  6. class to do so: :ref:`Node._process() <class_Node_method__process>` and
  7. :ref:`Node._physics_process() <class_Node_method__physics_process>`. If you
  8. define either or both in a script, the engine will call them automatically.
  9. There are two types of processing available to you:
  10. 1. **Idle processing** allows you to run code that updates a node every frame,
  11. as often as possible.
  12. 2. **Physics processing** happens at a fixed rate, 60 times per second by
  13. default. This is independent of your game's actual framerate, and keeps physics
  14. running smoothly. You should use it for anything that involves the physics
  15. engine, like moving a body that collides with the environment.
  16. You can activate idle processing by defining the ``_process()`` method in a
  17. script. You can turn it off and back on by calling :ref:`Node.set_process()
  18. <class_Node_method_set_process>`.
  19. The engine calls this method every time it draws a frame:
  20. .. tabs::
  21. .. code-tab:: gdscript GDScript
  22. func _process(delta):
  23. # Do something...
  24. pass
  25. .. code-tab:: csharp
  26. public override void _Process(double delta)
  27. {
  28. // Do something...
  29. }
  30. Keep in mind that the frequency at which the engine calls ``_process()`` depends
  31. on your application's framerate, which varies over time and across devices.
  32. The function's ``delta`` parameter is the time elapsed in seconds since the
  33. previous call to ``_process()``. Use this parameter to make calculations
  34. independent of the framerate. For example, you should always multiply a speed
  35. value by ``delta`` to animate a moving object.
  36. Physics processing works with a similar virtual function:
  37. ``_physics_process()``. Use it for calculations that must happen before each
  38. physics step, like moving a character that collides with the game world. As
  39. mentioned above, ``_physics_process()`` runs at fixed time intervals as much as
  40. possible to keep the physics interactions stable. You can change the interval
  41. between physics steps in the Project Settings, under Physics -> Common ->
  42. Physics Fps. By default, it's set to run 60 times per second.
  43. The engine calls this method before every physics step:
  44. .. tabs::
  45. .. code-tab:: gdscript GDScript
  46. func _physics_process(delta):
  47. # Do something...
  48. pass
  49. .. code-tab:: csharp
  50. public override void _PhysicsProcess(double delta)
  51. {
  52. // Do something...
  53. }
  54. The function ``_process()`` is not synchronized with physics. Its rate depends on
  55. hardware and game optimization. It also runs after the physics step in
  56. single-threaded games.
  57. You can see the ``_process()`` function at work by creating a scene with a
  58. single Label node, with the following script attached to it:
  59. .. tabs::
  60. .. code-tab:: gdscript GDScript
  61. extends Label
  62. var time = 0
  63. func _process(delta):
  64. time += delta
  65. text = str(time) # 'text' is a built-in Label property.
  66. .. code-tab:: csharp
  67. using Godot;
  68. public partial class CustomLabel : Label
  69. {
  70. private double _time;
  71. public override void _Process(double delta)
  72. {
  73. _time += delta;
  74. Text = _time.ToString(); // 'Text' is a built-in Label property.
  75. }
  76. }
  77. When you run the scene, you should see a counter increasing each frame.