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?

2 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.

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.

1 Like

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.