Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

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 class to do so: Node._process() and Node._physics_process(). If you define either or both in a script, the engine will call them automatically.

Существует два типа обработки, которые доступны вам:

  1. Idle processing позволяет вам запускать код, который обновляет узел каждый кадр, так часто, как это возможно.

  2. Physics processing происходит с фиксированной скоростью, 60 раз в секунду по умолчанию. Это не зависит от фактической частоты кадров в вашей игре и обеспечивает бесперебойную обработку физики. Вы должны использовать это для всего, что предполагает использование физического движка, как например движение тел, которые сталкиваются с окружением.

You can activate idle processing by defining the _process() method in a script. You can turn it off and back on by calling Node.set_process().

The engine calls this method every time it draws a frame:

func _process(delta):
    # Do something...
    pass

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_process(). Используйте её для вычислений, которые должны происходить перед каждым шагом физики, например, для перемещения персонажа, который сталкивается с игровым миром. Как упоминалось выше, _physics_process() выполняется с фиксированными временными интервалами, насколько это возможно, чтобы сохранить стабильность физических взаимодействий. Вы можете изменить интервал между шагами физики в Настройках проекта, в разделе Physics -> Common -> Physics Fps По умолчанию он установлен на 60 раз в секунду.

The engine calls this method before every physics step:

func _physics_process(delta):
    # Do something...
    pass

The function _process() is not synchronized with physics. Its rate depends on hardware and game optimization. It also runs after the physics step in single-threaded games.

You can see the _process() function at work by creating a scene with a single Label node, with the following script attached to it:

extends Label

var time = 0

func _process(delta):
    time += delta
    text = str(time) # 'text' is a built-in Label property.

When you run the scene, you should see a counter increasing each frame.