How to pass a function name as an expression from an array?

0 favourites
  • 9 posts
From the Asset Store
Assets for creating mountains and ravines environments
  • I have created many functions in the event sheet, and I want to call them dynamically by fetching their names from an array. I have a JSON array that is converted to a local array using AJAX, and the array contains the names of the functions that I want to call. For example, one of the functions is called “MyRoseFunction”.

    To call the function by name, I have added a JavaScript script in the action area of the event sheet, and I have used the runtime.callFunction method. When I pass the function name as a string, like this:

    runtime.callFunction("MyRoseFunction");

    It works fine and the function is executed. However, when I try to pass the function name as an expression using the At method of the array object, like this:

    runtime.callFunction(MyArray.At(5,2));

    It doesn’t work. Please help.

  • Maybe try putting the array value inside String(). Also I'm trying to work out why you would need to store function names in an array and call from there because you still need a triggering condition, at which point you might as well just use the name itself.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • var functionName = String(MyArray.At(5, 2));
    runtime.callFunction(functionName);
    

    It's not working; please help.

    I really like using arrays to control different parts of the code because they're so handy. :)

  • Sorry I don't know, you can log the variable to see if it's as you expected. I would not use arrays like this though I would just call the function.

  • Hi 🦁!

    I can make it work with Global Variable,

    on every tick → set TestGV → Value MyArray.At(5,2)

    var valueOfTestGV = runtime.globalVars.TestGV;
    runtime.callFunction(valueOfTestGV);
    

    but it doest work with local variable, like this

    var valueOfTestLV = runtime.localVars.TestLV;
    runtime.callFunction(valueOfTestLV);
    

    (I'm not sure if "runtime.localVars." is valid code.)

    I didn't think passing a function name as an expression from an array could be this complicated.

    If anyone knows, please help.

  • It's not complicated if you use events but you've chosen to use scripting, an additional feature which is programming in javascript. I don't think you can even access the array object in the way that you're attempting but I could be wrong.

    You likely won't be able to use the usual methods like array.at(a,b) so probably it doesn't recognise MyArray at all, thought I've not tried this combination of events and javascript before. I assume you create the array yourself in code if you decide to go down the scripting method, something like const MyArray = [a,b,c]

    If you can use such methods as above in the scripting like array.at(a,b) from the array object then this looks like a separate coding issue to do with scope of variables and accessing the array.

  • 🙄, I use JSON Array, because it is convenient.

  • Your JSON array is loaded into the array object in construct.

    construct.net/en/make-games/manuals/construct-3/plugin-reference/array

    You can manipulate the array directly with construct events and actions, as described in the manual. Using JS is giving you no advantage or anything you can't do directly with the array plugin in this case, besides making things more complicated.

  • 😅 Okay

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