How many _process are too many?

Godot Version

4.5

Question

I am making a 2D turn-based game. I want my game to run on low-end hardware. For this porpouse, I have been avoiding using func _process(), instead calling certain functions only when needed(like updating healthbars only when damage is dealt). I am finding it difficult to time everything correctly, and I am coming to a realisation that just using func _process() would solve my problems. But the question is - how many func _process() will start causing lags on low-end hardware?

How many times you define _process doesn’t change how performant the game is.

why

Internally, each node has a process function, defining it in your own script just overrides it. And as far as I know, Godot calls all of these functions regardless of whether they’re overriden. (Even if it didn’t, calls to empty functions don’t end up causing any lag)

What counts is what you do in the function:

  • Setting simple variables is basically free, this is fine for example:
var time: float = 0.0

func _process(delta: float) -> void:
    time += delta
  • Function calls are fine as long as you know that they’re not really performance-heavy:
var time: float = 0.0

func _process(delta: float) -> void:
    time += delta

    # fine because even though sin is expensive compared to other math functions,
    # it's still very cheap
    print(sin(time))
    # not fine
    get_tree().reload_current_scene()

It should be noted that putting something into _process doesn’t make it laggier, so something like this is still theoretically fine because the expensive function is only called once:

var collision_handled: bool = false

func _process(_delta: float) -> void:
    if not collision_handled and some_area_2d.overlaps_area(some_other_area_2d):
        collision_handled = true
        very_expensive_function()

Of course, it’s good practice to avoid _process as much as possible, but it’s usually not problematic to use it. And you can still optimise your game later on if you notice that it has issues on low-end machines.

1 Like

There will be some performance cost for calling an “empty” function. It wouldn’t be there if the function was not defined. Unless the compiler optimizes away empty functions… I haven’t checked if that’s the case in the source.

In general, you want to do as little as possible in processing script functions and have as few of them as possible. But don’t obsess over it until profiling your project shows that some of those functions are indeed eating up too much of your frame time. Always profile, never guess.

1 Like

Thanks. I can finally not worry about me forgetting to update healthbars and just put it in process

1 Like