The issue with GDevelop functions is that the variable has to exist IN the function too. You can’t just access whatever variable you want unless the function knows that it exists
IMO, this is very unintuitive, especially compared to Construct 3’s functions, which just let you use any variable anywhere
Scene variables have to be declared before they can be used. Fair enough I guess? But why global variables? Aren’t they available for the WHOLE project? Why not my functions too?
Also, it’s inconsistent. Or at least it feels like it
For example: Change the variable state of player = "fall"
This object variable is not declared in the function, and it still works
Change the gravity of player = 1200
This IS declared in the function:
Now, I get why it’s designed this way for custom behaviors and prefabs, since they’re meant to be shareable, but functions are typically used exclusively in the project they’re built in, so like, I don’t understand why it has the same limitations
Is it possible to make the functions a little more flexible and intuitive?
When it comes to variables though. I think the preferred method is to pass the variable to the function as a parameter (when possible). You can then return values back to the variable or create an expression instead and return values through it.
Edit: Just to clarify, Davy is correct that the original post is about Object variables, which the below won’t assist with.
As of this post, you can still access variables external to the functions. It’s literally in the event conditions and actions list as ‘external variables’
I have done my best to explain why global variables should be global including for project-specific functions and extensions (they should never be acceptable for submitted extensions/objects as they can’t maintain that structure, but that’s an entirely separate conversation than internal project-specific items), including that is how global variables work in most programming languages (including javascript). The dialogue is ongoing, but in the interim you should still be able to use these conditions and expressions as needed.
Trigger once doesn’t always work inside a function as expected and usually isn’t needed. It’s better to control when a function is called instead of what happens inside the function even if that means splitting the function into multiple functions. In your case, it might be easier to have a seperate function to reset an object and only call that when needed. Maybe with an at the beginning of the scene. Or maybe it needs a different condition instead of the trigger once.
The thing with using trigger once inside a function is that it takes the conditions that call it into account.
As I test, I created an action that rotated an object by plus 10 degrees. I triggered the function from the scene with a condition of mouse button is pressed. Even though the rotate object action inside the function had a trigger once, the object was still rotated a little everytime the mouse was pressed because the trigger once was taking the mouse condition into consideration.
Honestly, functions should work the same each time they’re called. If trigger once inside a function meant that the event would only ever happen once then how could you ever reuse the function.
Another option with functions is to use conditions or a boolean variable.
If initialized is false
Set initialized to true initialization events
You could use a scene variable or object variable or even a parameter.