How do I call function by name?

0 favourites
From the Asset Store
Basic clone of a 80's arcade game called Gyruss that was quite fun to play!
  • Aside from the calling of maps/strings, which is already puzzling, have you considered how strange and limited the forwarding of parameters is?

    I don't understand the point. If you call a function by a variable string, you have to provide the same parameters regardless of the string provided, right? Function maps are no different in that regard.

    I don't think the poll proves anything. It could just mean most people don't have a need for calling a function by a string.

    In Construct's own codebase we use the function maps pattern in several places as it's useful, and one of our goals with event sheets is to design things in a way that mimics how programming languages work. While JavaScript does allow calling a function by a string of its name, it causes maintenance headaches, so we tend to use a Map with string keys and function values - the same approach as used with function maps.

    I think questions like "How do I call a function by name in an event sheet?" are best answered by "Function maps". Otherwise you are telling someone to ignore an existing event sheet feature and use JavaScript coding instead - which is ignoring the preference of the user if they want to stick to event sheets, and was a prospect a lot of people were worried about when we first introduced the JavaScript coding feature - so let's try not to fall in to that trap!

  • But this is the Scripting section, lol

    In any case, I understand, but the majority of people using scripting for dynamic function calls will still continue.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I don't understand the point. If you call a function by a variable string, you have to provide the same parameters regardless of the string provided, right? Function maps are no different in that regard.

    How would you pass parameters of a function that is also calling to another function, and then another function, each passed on function having different parameter values, count, order and types.

    And then, in-between, what if you also need to do some calculations as you pass?

    How would you do such a simple scenario with only Function Maps?

    Please kindly observe my screenshot below, for context. Please pay close attention to how parameters are passed, the number of parameters passed and their types.

  • But this is the Scripting section, lol

    But I think they're only here because they read advice elsewhere to use scripting instead of function maps in the event sheet:

    i found this topic where the answer was given which says to use scripting.

    I'm afraid I still don't understand the example you gave afterwards either. I don't see that it is using calling functions by a string variable at all. They just all look like ordinary function calls.

  • There are many issues with this system:

    1-Passing Parameters is confusing

    2-You have to plan everything in advance

    Just planning everything in advance defeats the purpose of creating any "Function Maps" as for me calling by a string means freedom, as simple as:

    Give the Function Name, Parameters if any and done.

    runtime.callFunction("Function Name", Parameter0, Parameter1);
    

    Super simple.

    That's a true dynamic freedom for me at least. Where you dont have to plan anything in advance as everything comes naturally. Normally on most of the games you have to constantly redo the events to optimize, etc... So having to plan constantly "Function Maps" is just a waste of a lot of time in comparison to just:

    -Create a Function

    -Call: runtime.callFunction("Function Name", Parameter0, Parameter1)

    -Delete the function when not need it.

    Also, Dop Pool from one side and I heard many other users through the years that never use "Function Maps", so in true are more people than you think that dont use that. In the end, most of us will continue to use the Scripting just to cal functions and we will lose the advantage of:

    "function maps, you can rename a function and everything still works."

    Anyway.

    Maybe making some Analitycs to see who uses (Scripting Vs Function Maps) will give more accurate info on that probably.

    Or even an official Pool to see how many people use one or the other.

  • Functions map are tedious to use.

    We need 2 things to solve all issues :

    1. Ability to turn any Function/Custom Action into a Better signal (= Multiple "Listener Block" support) : construct23.ideas.aha.io/ideas/C23-I-165

    It would be the same as calling Multiple Function at once with the same parameters but with an amazing UX, an optional execution order feature and a single thing to "Find All References" for (single name).

    2. Ability to call a Function/Custom Action by its name

    (3. For 2 to be easier to use in some situations, it could be cool to have Function/Custom Actions Names autocomplete and autoupdate everywhere thanks to new System Expressions such as EnumFunctions.MyFunctionName and EnumCustomActions.MyObjectType.MyCustomeAction as in this suggestion : construct23.ideas.aha.io/ideas/C23-I-69 )

    I detailed those 3 points extensively in this thread : construct.net/en/forum/construct-3/general-discussion-7/custom-actionsfunctions-175063

  • Here is my usage case - I have a dialogue system based on JSON files. These dialogues may call various functions with variable number of parameters:

    {"function": "functionName", "params": ["foo", 1, 20, 30, "bar"]}
    

    So there is a single script which calls all these functions:

    if (localVars.params=="") {
     runtime.callFunction(localVars.fName);
    } else {
     var p = JSON.parse(localVars.params);
     runtime.callFunction(localVars.fName, ...p);
    }
    

    I guess this can be re-done with function maps, but it will probably be a huge task, taking hours.

  • Here is my usage case - I have a dialogue system based on JSON files. These dialogues may call various functions with variable number of parameters:

    Construct does something similar when reading project files: depending on strings in the JSON, it calls different functions. For that we use function maps. Otherwise you end up encoding function names in the data format, which causes the maintenance problems I was talking about: you can never rename any functions without breaking compatibility with all your existing data files.

    I guess you can use the scripting feature if you want to anyway - after all part of the reason it's there is for more advanced use cases like processing complex JSON data structures. It's just if someone prefers to use event sheets and wants to call a function by a string, then I think the first suggestion should be to use function maps. Alternatively for small cases you can use other simple solutions like a string parameter and if-else-if style sub-events doing something different depending on the parameter. Only then if these solutions don't meet the demands of the particular use case would it then be appropriate to talk about scripting. There's no need to use a sledgehammer to crack a nut, as they say.

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