This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes


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.

in Engine by (429 points)
recategorized by

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.

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

Oh, my bad. And your welcome.

1 Answer

+1 vote
Best answer

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.

by (703 points)
selected by

Wow, thanks for this information!

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)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.