Is it better to have checks in process loops before actions?

Godot Version

4.2

Question

I’m developing a game and I wanted to know if having checks before an action in a process/physics_process loop is more “optimized” than just having an action straight away, does it make any difference perfomance-wise and which method would perhaps run better?

Here’s an example of a check before an action (after else:)
image

And heres the same code but without a check for if the timer stopped:
image

This code is running in the physics_process loop and with that in mind, would it be faster to have it constantly check for if the timer is not stopped (and then stop it), or would it be better to just stop the timer constantly every frame?

edit: most of the time the code would return false in the if linear_velocity … , running the “else” code 99% of the time

Since Godot is open source, you can check what is_stopped() and stop() do:
github.com/godotengine/godot/blob/master/scene/main/timer.cpp

Looks like is_stopped() is just checking if the variable time_left is greater than 0:

bool Timer::is_stopped() const {
	return get_time_left() <= 0;
}

double Timer::get_time_left() const {
	return time_left > 0 ? time_left : 0;
}

stop() seems to be calling _set_pocess() and setting it to false.

void Timer::stop() {
	time_left = -1;
	_set_process(false);
	autostart = false;
}

So it doesn’t seem like this will make a lot of difference on performance.

Whenever you have these types of doubts, try both cases and see if the performance improves or is imperceptible. And take a look at the Godot’s source code, where you will be able to investigate further and find what is exactly happening behind the scenes!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.