2023-01-12 20:49:14 +01:00
|
|
|
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
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 in the Node
|
2023-01-12 19:30:47 +01:00
|
|
|
class to do so: `Node._process()` and
|
|
|
|
`Node._physics_process()`. If you
|
2022-03-18 17:46:08 +01:00
|
|
|
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. This is independent 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.
|
|
|
|
|
2023-01-12 20:57:31 +01:00
|
|
|
You can activate idle processing by defining the `process()` method in a
|
2023-01-12 19:29:11 +01:00
|
|
|
script. You can turn it off and back on by calling `Node.set_process()
|
2023-01-12 20:47:54 +01:00
|
|
|
( Node_method_set_process )`.
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
The engine calls this method every time it draws a frame:
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
func _process(delta):
|
|
|
|
# Do something...
|
|
|
|
pass
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 20:57:31 +01:00
|
|
|
Keep in mind that the frequency at which the engine calls `process()` depends
|
2022-03-18 17:46:08 +01:00
|
|
|
on your application's framerate, which varies over time and across devices.
|
|
|
|
|
2023-01-12 19:43:03 +01:00
|
|
|
The function's `delta` parameter is the time elapsed in seconds since the
|
2023-01-12 20:57:31 +01:00
|
|
|
previous call to `process()`. Use this parameter to make calculations
|
2022-03-18 17:46:08 +01:00
|
|
|
independent of the framerate. For example, you should always multiply a speed
|
2023-01-12 19:43:03 +01:00
|
|
|
value by `delta` to animate a moving object.
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
Physics processing works with a similar virtual function:
|
2023-01-12 20:57:31 +01:00
|
|
|
`physics_process()`. Use it for calculations that must happen before each
|
2022-03-18 17:46:08 +01:00
|
|
|
physics step, like moving a character that collides with the game world. As
|
2023-01-12 20:57:31 +01:00
|
|
|
mentioned above, `physics_process()` runs at fixed time intervals as much as
|
2022-03-18 17:46:08 +01:00
|
|
|
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:
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
func _physics_process(delta):
|
|
|
|
# Do something...
|
|
|
|
pass
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 20:57:31 +01:00
|
|
|
The function `process()` is not synchronized with physics. Its rate depends on
|
2022-03-18 17:46:08 +01:00
|
|
|
hardware and game optimization. It also runs after the physics step in
|
|
|
|
single-threaded games.
|
|
|
|
|
2023-01-12 20:57:31 +01:00
|
|
|
You can see the `process()` function at work by creating a scene with a
|
2022-03-18 17:46:08 +01:00
|
|
|
single Label node, with the following script attached to it:
|
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
gdscript GDScript
|
2022-03-18 17:46:08 +01:00
|
|
|
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
extends Label
|
|
|
|
|
|
|
|
var time = 0
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
time += delta
|
|
|
|
text = str(time) # 'text' is a built-in Label property.
|
2023-01-12 18:31:02 +01:00
|
|
|
```
|
2022-03-18 17:46:08 +01:00
|
|
|
|
|
|
|
When you run the scene, you should see a counter increasing each frame.
|