Are GDScript functions atomic?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By avnih

For example, say I have this function:

var event_x_happened = false

func proceed_only_if_event_x_already_happened()
    if not event_x_happened:
        print("waiting for it...")
        yield(self, "event_x")
        event_x_happened = true
    print("now it happened for sure - continue")
    do_some_logic()

Is it guaranteed that the signal event_x will not be emitted on the line
print("waiting for it...")
and hence will get missed out by the line
yield(self, "event_x")
thus causing a deadlock?

If your code isn’t multithreaded, while the GDScript VM is processing proceed_only_if_event_x_already_happened() it can’t emit the signal the yield is waiting for, because it will only return from the function when reaching the yield (and eventually call your emit_signal() after that).

Hoimar | 2021-04-12 20:17

Thank you. That’s what I wanted to know - that once a func starts executing, no other code will be executed before it finishes (other than the cases of yield or multithreading)

avnih | 2021-04-13 12:46

:bust_in_silhouette: Reply From: Inces

It depends only how You are planning on checking this event.
signal will never be missed because of other lines, it is independent from lenght of code, it all happens too fast.

But if You somehow emit “event” before whole function is started, then it will be deadlocked. You have to time it properly.