More flexible functions

If there’s one thing I dislike about GDevelop, it’s that the functions feel kind of limiting

See here, I’m trying to reset my player’s values if they respawn (Respawning is in a function)

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:


…but it don’t work???

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?

3 Likes

I agree with you. Functions need work.

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.

I think one of the biggest limitations with GD is a lack of documentation.

2 Likes

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’

Expression wise you still have to use the old globalvariable() expression to access global variables.

I say as of this post because there is currently a PR to remove those conditions/actions/expressions entirely from extensions. Deprecate old variable actions and conditions in extensions by D8H · Pull Request #7406 · 4ian/GDevelop · GitHub

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.

2 Likes

The issue here is about object variables which is a different matter.

Object variables in extensions still use the old way (we don’t have any plan to migrate them for now):

The new object variable actions used in scenes should not be copy-pasted in function because they won’t work, these ones must be used:

That said, I recommend to pass objects with custom behaviors in parameter instead of using undeclared variable accesses.

1 Like

I also want to point another thing out:


See here, in the same function (Used to respawn the player), I have the Health behavior, as well as my own custom behavior

The health and custom behaviors work without issue, which is why it’s especially confusing as to why the Platformer behavior actions DON’T work

I’d like to just have the platformer stat changes here since it’s convenient, but the engine just straight up won’t let me

THAT’S why I say inconsistent. Some behaviors work, and some don’t

Or am I missing something?

One thing is different : delete “Trigger once” and check

A+
Xierra54

1 Like

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.

1 Like

I agree with Keith.

Strange the trigger works like that in the function!

I don’t know we can considerate that as 1 bug but we are not so far, isn’t it!

A+
Xierra54