How do I call a function by name?

0 favourites
  • 4 posts
From the Asset Store
Medeival & Viking War Horns Sounds / 22 Unique Calls / 2:40 minutes of audio
  • hello, I would like to be able to call a function by its name which is stored in a variable. Let me explain: an instance variable contains the name of a function. when selecting this instance (for example by clicking on it) I would like to call the function corresponding to the value of the variable (for example if the variable contains "closegame", the game will have to call the function called "closegame")

    It is possible to proceed in this way? and if yes how?

    thank you

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Two ways -

    1. Use function maps, there is a template in C3 explaining how they work.

    2. Call the function from a script. For example - runtime.callFunction(runtime.globalVars.funcName)

    The second method if much easier.

  • ok, i think i will use the first method because i am not very familiar with js.

    thx for ur help

  • You don't need to know JS. Simply add a script in your event with one line of code:

    runtime.callFunction(runtime.globalVars.funcName)

    , where funcName is the global variable containing the name of a function you want to call.

    One problem with this method is that it may not work if you choose advanced minification when exporting.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)