Function In Global Script Used Simultaneously

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tiernich
:warning: Old Version Published before Godot 3 was released.


One question…

I have a Global Script and one function similar to this:

func say( phrase ):
    print( phrase )

This function is used simultaneously by many objects in my game, and each give a diferent phrase to the function. I run a test thinking (this is gonna be a mess) but no. Every object use the function and print the right phrase. why there is no conflict? i assume that using the same function for many objects at the same time will generate some kind of error. Hope i get clear to explain this.

You might look at classes and functions here to see if you can make the nodes you want to pass the phrase argument to, have a class of the global node so the global variable phrase is not over written by every node.

SupToasty | 2016-04-22 16:34

i am not having trouble, haha, its working without doing nothing, i wanna understand the process. thanks for the reply!

tiernich | 2016-04-22 16:39

Oh, my bad. And your welcome.

SupToasty | 2016-04-22 19:19

:bust_in_silhouette: Reply From: Gokudomatic2

Even though I didn’t check the C code, I can tell that you’re using a stateless method. It is of course compiled in C and executed as a C method.

By the fact it runs without a state, and it’s not only in godot but also in compiled languages, when a method is called with parameters, the application has a stack for this call. If in another thread there’s a concurrent call to the same method, there is another stack for this call. Or, should I say, there’s a big stack for all calls, and the cpu knows where in the stack it needs to seek the values. Therefore there’s no risk of concurrent conflict for local variables or parameters.

By the way, you should check about recursive functions. The concept is a method that call itself until a condition is reached. And each recursive call is put in a stack until it goes out the function and returns to the previous call where it was. It illustrates well how calls are managed and how it can run out of memory if too many calls pile up.

Wow, thanks for this information!

tiernich | 2016-04-26 05:36

Note that GDScript methods do not compile to either C or native code, but they are interpreted at run-time. (though, one day we might have JIT compilation in place)

Bojidar Marinov | 2016-04-26 08:01

Really? I can’t really confirm. I do remember however in the old forum that reduz said that gdscript was fast because it was compiled to c or something like that.
In any case, that doesn’t change much my answer. When I think about it, all languages, no matter if it’s compiled or scripted, use a stack for calls. In ASM we need to do it manually, so I believe this way of doing is almost unavoidable.

Gokudomatic2 | 2016-04-26 08:50