Call GDevelop Function from JS Code Event? & If Else Events

I have a question, and couldn’t find an answer or example. But I was looking to see if there is a way to call a GDevelop function directly from javascript code.

I’m referring to the custom functions you create, are they accessible via Javascript?

example:

runtimeScene.callFunction(‘myCustomFunction’, Param_1, Param_2);

Also I was wondering how to do a “If Else” condition with the visual editor.

I can do this with the Javascript events, but the visual conditions dpn’t behave like I anticipate.

I tried doing a simple switch where a variable is either “On” or “Off” and clicking a text object, switches between states.

But for whatever reason both “On” and “Off” conditions run at the sametime, even after having one of the events as “Inverted”.

I’m wondering is there something specific that needs to be done to have an “If Else” condtion with the visual editor.

Any help would be appreciated, thanks!

You can use functions from code, but it’s very cumbersome, they’re not meant to be used from JS… after all if you know Javascript you can use Javascript functions which are more powerful :wink:

For the if/else you need an extra variable, check this out: [url]on/off Button for Background music]

Thanks for your help on the “If Else” issue.

How do I call a GDevelop function from Javascript?

Are they exposed on the runtimeScene object? I also saw a GDJS object, but can’t figure out where I would access the functions.

I already tried calling them like any other normal JS function:

Example:

myFunction();

But this only works for functions created in the Javascript code event.

My guess is that they are chained to one of the main game objects, and there is some type of method to call them, but I haven’t been able to find it.

Functions are generated like events in JS functions with a “mangled” name.

Is there any reason why you would call a GDevelop function from a JS function? Any particular reasons?
Because it would be possible for you to find the function name and call it - you can open the debugger and inspect the generated JS files :slight_smile:
But this can break if at some point some refactoring is done in the way the function name is generated. It’s not meant to be “public”, at least for now. (hence this is why I’m asking what’s your precise need behind this, to better understand what you’re trying to achieve :slight_smile: )

Thanks for your reply and also providing us with an awesome game engine.

I was building a simple Tic-Tac-Toe game and realized building the logic would be easier in Javascript.
But I also wanted the flexibility of configuring a few GDevelop functions, taking advantage of the actions/features available “out of the box”.

I figured it would be quicker to build certain features visually, eliminating the need to search though runtime documentation to do certain things, and also having to rebuild functionality that already exists.

I would then build most of the logic (if-else, switch-case blocks, arrays etc…) in Javascript and call certain GDevelop functions when necessary, via the JS logic I configure.

I know I could do everything in Javascript, but I think it would be faster to build certain parts with the visual editor and the rest with Javascript. This provides the benefit of only having to dig into the runtime/api docs when necessary.

Thanks again for your help and your game engine!

I’ll look into the debugger to see what I can find for my project.