diff --git a/getting_started/step_by_step/scripting_continued.rst b/getting_started/step_by_step/scripting_continued.rst index 34aee5740..4b3432f30 100644 --- a/getting_started/step_by_step/scripting_continued.rst +++ b/getting_started/step_by_step/scripting_continued.rst @@ -3,87 +3,6 @@ Scripting (continued) ===================== -Processing ----------- - -Several actions in Godot are triggered by callbacks or virtual functions, -so there is no need to write code that runs all the time. - -However, it is still common to need a script to be processed on every -frame. There are two types of processing: idle processing and physics -processing. - -Idle processing is activated when the method :ref:`Node._process() ` -is found in a script. It can be turned off and on with the -:ref:`Node.set_process() ` function. - -This method will be called every time a frame is drawn: - -.. tabs:: - .. code-tab:: gdscript GDScript - - func _process(delta): - # Do something... - pass - - .. code-tab:: csharp - - public override void _Process(float delta) - { - // Do something... - } - -It's important to bear in mind that the frequency with which ``_process()`` -will be called depends on how many frames per second (FPS) your application -is running at. This rate can vary over time and devices. - -To help manage this variability, the ``delta`` parameter contains the time -elapsed in seconds as a floating-point number since the previous call to ``_process()``. - -This parameter can be used to make sure things always take the same -amount of time, regardless of the game's FPS. - -For example, movement is often multiplied with a time delta to make movement -speed both constant and independent of the frame rate. - -Physics processing with ``_physics_process()`` is similar, but it should be used for processes that -must happen before each physics step, such as controlling a character. -It always runs before a physics step and it is called at fixed time intervals: -60 times per second by default. You can change the interval from the Project Settings, under -Physics -> Common -> Physics Fps. - -The function ``_process()``, however, is not synced with physics. Its frame rate is not constant and is dependent -on hardware and game optimization. Its execution is done after the physics step on single-threaded games. - -A simple way to see the ``_process()`` function at work is to create a scene with a single Label node, -with the following script: - -.. tabs:: - .. code-tab:: gdscript GDScript - - extends Label - - var accum = 0 - - func _process(delta): - accum += delta - text = str(accum) # 'text' is a built-in label property. - - .. code-tab:: csharp - - public class CustomLabel : Label - { - private float _accum; - - public override void _Process(float delta) - { - _accum += delta; - Text = _accum.ToString(); // 'Text' is a built-in label property. - } - } - -Which will show a counter increasing each frame. - Groups ------ diff --git a/tutorials/scripting/idle_and_physics_processing.rst b/tutorials/scripting/idle_and_physics_processing.rst new file mode 100644 index 000000000..a86520d29 --- /dev/null +++ b/tutorials/scripting/idle_and_physics_processing.rst @@ -0,0 +1,104 @@ +.. _doc_idle_and_physics_processing: + +Idle and Physics Processing +=========================== + +Games run in a loop. Each frame, you need to update the state of your game world +before drawing it on screen. Godot provides two virtual methods on the Node +class to do so: :ref:`Node._process() ` and +:ref:`Node._physics_process() `. If you +define either or both in a script, the engine will call them automatically. + +There are two types of processing available to you: + +1. **Idle processing** allows you to run code that updates a node every frame, + as often as possible. +2. **Physics processing** happens at a fixed rate, 60 times per second by + default, independently of your game's actual framerate, and keeps physics + running smoothly. You should use it for anything that involves the physics + engine, like moving a body that collides with the environment. + +You can activate idle processing by defining the ``_process()`` method in a +script. You can turn it off and back on by calling :ref:`Node.set_process() +`. + +The engine calls this method every time it draws a frame: + +.. tabs:: + .. code-tab:: gdscript GDScript + + func _process(delta): + # Do something... + pass + + .. code-tab:: csharp + + public override void _Process(float delta) + { + // Do something... + } + +Keep in mind that the frequency at which the engine calls ``_process()`` depends +on your application's framerate, which varies over time and across devices. + +The function's ``delta`` parameter is the time elapsed in seconds since the +previous call to ``_process()``. Use this parameter to make calculations +independent of the framerate. For example, you should always multiply a speed +value by ``delta`` to animate a moving object. + +Physics processing works with a similar virtual function: +``_physics_process()``. Use it for calculations that must happen before each +physics step, like moving a character that collides with the game world. As +mentioned above, ``_physics_process()`` runs at fixed time intervals as much as +possible to keep the physics interactions stable. You can change the interval +between physics steps in the Project Settings, under Physics -> Common -> +Physics Fps. By default, it's set to run 60 times per second. + +The engine calls this method every time it draws a frame: + +.. tabs:: + .. code-tab:: gdscript GDScript + + func _physics_process(delta): + # Do something... + pass + + .. code-tab:: csharp + + public override void _PhysicsProcess(float delta) + { + // Do something... + } + +The function ``_process()`` is not synced with physics. Its rate depends on +hardware and game optimization. It also runs after the physics step on +single-threaded games. + +You can see the ``_process()`` function at work by creating a scene with a +single Label node, with the following script: + +.. tabs:: + .. code-tab:: gdscript GDScript + + extends Label + + var time = 0 + + func _process(delta): + time += delta + text = str(time) # 'text' is a built-in Label property. + + .. code-tab:: csharp + + public class CustomLabel : Label + { + private float _time; + + public override void _Process(float delta) + { + _time += delta; + Text = _time.ToString(); // 'Text' is a built-in Label property. + } + } + +When you run the scene, you should see a counter increasing each frame. diff --git a/tutorials/scripting/index.rst b/tutorials/scripting/index.rst index 1ef1e2843..7713b5682 100644 --- a/tutorials/scripting/index.rst +++ b/tutorials/scripting/index.rst @@ -1,6 +1,19 @@ Scripting ========= +This section covers programming languages and core features to code your games +in Godot. + +Here, you will find information that is not already covered in more specific +sections. For instance, to learn about inputs, we recommend you to read +:ref:`Inputs `. + +Programming languages +--------------------- + +The sections below each focus on a given programming language or, in GDNative's +case, an interface that works with multiple languages. + .. toctree:: :maxdepth: 1 :name: toc-learn-scripting @@ -9,7 +22,20 @@ Scripting visual_script/index c_sharp/index gdnative/index + +Core features +------------- + +Some features are specific to the engine and available in all supported +languages. Whether you code in GDScript, C#, or another language, the pages +below will help you make the most of Godot. + +.. toctree:: + :maxdepth: 1 + :name: toc-scripting-core-features + debug/index + idle_and_physics_processing cross_language_scripting creating_script_templates change_scenes_manually