Tuto for creating a function in GDevelop

Hello all

I’d like to talk about creating functions in GD.

Are there any (updated) tutorials that explain step-by-step how to create functions, because it doesn’t look that easy?

Thanks in advance.

A+
Xierra

If you haven’t already. Then start with the wiki.

https://wiki.gdevelop.io/gdevelop5/events/functions/

What part do you need help with?

The worst part for me is setting up the function. It’s not hard once you get familiar with it but setting up parameters and settings can be tedious but if you need events that are reusable or you want to move events out of the main event sheet then it’s worth it.

I can post more info when I have more time.

You can extract or move events into a new function. This works but sometimes it uses an object parameter when you only need a number but it’s a start.

https://wiki.gdevelop.io/gdevelop5/events/functions/extract-events/

You can also study the way other extensions and functions are written.

I think they could keep the current method and create a simpler one that doesn’t even depend on extensions.

Hello @Keith_1357

Thanks a lot. I’m going to test your Wiki examples because the documentation is poor in explications and this last is not a tutorial.

A+
Xierra

Hi @superely

I am agree with you at 100%.
In traditional programmation, there is no link between function and extension.

This complicates things unnecessarily.

Moreover, parameters switching is poorly explained, if at all.

Thanks

A+
Xierra

I almost switched to Construct because of that. Creating functions there is way simpler and more efficient. There’s just no comparison.

literally just yesterday i was thinking “man, i should make this code to initialize the keyboard a function…oh right, extensions cant do that”

its not that they cant either. itd be the same as external layouts or events where you choose a scene and then just put in code. let me put it exactly like this to some who might understand…

External events is like importing another python file in the beginning of a python script.

Extensions are like imports that actually expand on the capabilities of python.

…yeah, none of what i just said replaces functions.
this is most certainly a compromise that doesnt need to be there. hell, id rather an export function to extension button rather than a export events to extension. that option is rarely ever useful, the code just breaks once its an extension lol.

External events replace like 5% of functions. mostly just “init” functions.

Something really weird about extensions is that unlike other things, leaving the parameter invalid or hardcoded does not fix it in a scene where its actually valid. i (think) this works in other things like external events

i dont really have my hopes up…how many decades did it take to add Else?

Hi all!

I know that GD is presented as a “no code” engine but that does not justify the lack of explanations for creating functions or extensions.

I finally understand that an extension can group together several functions but we must also have the possibility of creating a function without having to use an extension.

I’m still waiting for detailed explanations on creating functions in GD.

PS: a simple function like Choose(1,4,7,15,…) does not exist in GD. Of course, I can get around this by writing event lines but that clutters up the events.

A+
Xierra

What is Choose()? I’m aware of several similar functions but idk your context.

Maybe not related, but I wish GD had optional parameters that would be created as an array. The closest thing to infinite parameters would be a string that gets converted by something like the Split() function in the array tools extension.

Hang on…do you mean, expressions? what you said (Choose()) is an example of an expression. and surprisingly or not, that actually used to be an expression from an extension that was taken off the extension list.

anyway, you cannot create functions in gdevelop in the traditional programming sense. However, i can tell you how youre supposed to make one with an extension.


First, open the Project Manager, and press the plus next to the extensions category.


Press “Create a new extension” at the bottom of the screen.
Name it something that makes sense.


To your left, you will see this menu. Press the plus next to “Functions”.


Judging by what you said, the only thing that would make sense is an expression, so click that. Name it something, (e.g. “Choose”)


To your right, you will see the function configuration. You can edit the name it will display as in the event sheet here, as well as specify parameters.
The type is the type of data this expression will expect. For this, i would suggest string, even though were working with numbers.

From now on, you’d add either events or JavaScript to return the value you want this expression to return. thats pretty specific, so i dont think ill get there just yet.

To see this expression in action, go to your event sheet in a scene for example, create any action that can take a string parameter, and then in that parameter you can put in your expression.

Yeah, I remember the choose extension. I have it in older projects, but I couldn’t find it on the store. I could export it and send though. What it does is:
Choose(Comma separated string here of a list of text or number) thereby allowing multiple output.

…why was it removed? perfectly reasonable, worked very well.

i use to use it just as often “repeat every x seconds” because i dont wanna bother with timer setup

Thanks, @DesmonDev

I arrived upto what you showed.
The Choose function existed in the past but was not official, it seems.
It was Ulisses Freitas who wrote an extension with this function.

The difficulty of the function Choose is in the number of parameters accepted by the function.
By example, you can write “Choose(2,15,3)” and after, have the need to call the same function like that: Choose(4,10,66,8,99)
It’s necessary to have the possibility to know how many parameters there are in the call of the function. It’s the principal difficulty.

In the same order, the function Choose can also be used to return a string choosen among the list of strings, as:
Choose(“one”,“two”,“three”)

Or more simple, write 2 functions:
ChooseNumber(…) and ChooseString(…)

And thanks again

A+
Xierra

What the previous choose extension did was different:
Choose(“1,2,3,4,5,6,7,8”) - A string of comma seperated values
Then use JS to split these into and array and then randomly select one of them. If it for the ChooseNumber() you can then output a number. This allows multiple parameters.

Ok, thanks for the trick of the string with commas.
The problem is i don’t know at all JS.

I am going to see if i can do the same with events but i am not sure it’s possible.

A+
Xierra

It was removed here:

Gdevelop decided it was low quality.

This is the part on the PR:

  • Choose value
    • Comma separated value is not user friendly
    • It’s better to use the array tool extension