Dragonfly

Question: When should you use _process and _physics_process in Godot?

Answer

In Godot, both _process(delta) and _physics_process(delta) are callback functions that are called every frame, but they serve different purposes and are synchronized with different parts of the engine's loop cycle.

_process(delta)

Use _process(delta) when you need to update something on every frame regardless of the physics. This is where you put your code for anything that doesn't require precise synchronization with the physics engine, such as:

The delta parameter represents the time elapsed (in seconds) since the last frame. It helps ensure that updates occur at a consistent rate, regardless of frame rate variations.

Here's an example of using _process(delta):

func _process(delta):
    # Rotate a node at a constant rate
    rotation_degrees += 90 * delta

_physics_process(delta)

On the other hand, _physics_process(delta) is specifically designed to run in sync with the physics engine. This means it's called at a fixed interval, ensuring consistent physics simulation. Use _physics_process(delta) for:

Similar to _process, the delta parameter here indicates the fixed amount of time between physics frames, ensuring consistent physics behavior.

Here's how you might use _physics_process(delta):

func _physics_process(delta):
    # Apply a constant force to a RigidBody
    if Input.is_action_pressed("ui_up"):
        $RigidBody.apply_central_impulse(Vector3(0, -10, 0) * delta)

Key Differences

It's important to choose the right function based on what you need to achieve in your game for smooth gameplay and accurate physics simulation.

Was this content helpful?

Other Common Game Engines Questions (and Answers)

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost