How do I call a function using its name?

Not favoritedFavorited Favorited 0 favourites
  • 8 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • I have a character that has certain bonuses - like gain yourself 10 life or heal 5 life after you attack something or double jump and such

    My character gets these bonuses by wearing different hats.

    When she wears a green hat for example there is the function "greenhat_ADD" which gives her the bonus

    When she wears a different hat it Take away the bonus of the hat she was already wearing. for example If she replaces the green hat with a red hat it call the function "greenhat_SUBSTREACT" and then call the function "redhat_ADD"

    It's all well and good if you have 2 hats. What if you have 20?

    Is there no way to create a function that says:

    "Call the function whose name is HAT&"_ADD

    "Call the function whose name is HAT&"_SUBTRACT"

    ?

    I tried using call mapped function via function and it just doesn't work.

    A construct simply won't accept function names in any way I've tried to put into it

    It's really driving me crazy.

    Can you please help me?

    Tagged:

  • Could you not have a single function with paraeters for "add" or "subtract" and the "amount" and in the function logic you use that to determine if its adding or subtracting and how by how much?

  • If I understand correctly you should have a global or instance string variable "HAT" which during the game can have values ​​such as "red", "green", yellow" etc...

    If you want to know if you can call a function by name, I know of 2 possible solutions. The first is mapping functions. The second is to use a script.

    But keep in mind that both could be less useful solutions, I prefer to use a single function and parameters, (using scripts complicates things a lot). I would prefer to use a "(everycolor)hat_ADD function with a string parameter "color" and call the function using the value of the HAT variable for the "color" parameter. Then the function would have a condition for each color e.g. "color=red" and its events. You can use "b" to create a subgroup of events. I have been using this method for a while. But if you have already thought about it and think it is really necessary to call the function by name you will have to resort to either function maps or scripting.

    If you search the forum there are questions and answers of all kinds. I asked for help in this discussion:

    https://www.construct.net/en/forum/construct-3/scripting-51/call-function-name-176356

    (in any case I never put scripting into practice because I didn't consider it necessary).

  • Rnamon

    I am not 100% sure on this, but I don't think C3 functions support the kind of meta programming your are describing.

    My suggestion would be to have functions that ADD and SUBSTRACT (possibly more operations) that receive two parameters, the attribute of your character they are modifying and the value.

    You would then make as many function calls as you have hats. Each hat would also need to have an instance variable indicating what attribute they modify and another saying by how much.

    The body of each function can then modify the corresponding attribute of your character and use the value accordingly.

    This would make it easy to later add more functions and different hats with different properties.

  • I find it easier to call functions by name in scripting:

    runtime.callFunction(localVars.fName);

    or

    runtime.callFunction(localVars.fName, localVars.parameter1, localVars.parameter2,...);

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can try this:

    runtime.callFunction(...localVars["command"]
    .split(",").map(e => e.trim())
    .map(e => (isNaN(e) ? e : Number(e))));

    Add it to a Function and call it with a string parameter.

    {"is-c3-clipboard-data":true,"type":"events","items":[{"functionName":"Call","functionDescription":"","functionCategory":"","functionReturnType":"none","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"command","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[{"type":"script","script":"runtime.callFunction(...localVars[\"command\"]\n.split(\",\").map(e => e.trim())\n.map(e => (isNaN(e) ? e : Number(e))));"}]}]}
  • The Function maps example shows how to use function maps to call a function by a string.

  • I don't think you actually need mapped functions in this case and others above have suggested valid alternatives. However, you said you couldn't get mapped functions to work so I made an example in case you ever need to use them in the future, or your hat system gets so complex that you DO need them ( I could maybe see this being the case if the differences between hats becomes something beyond just additions to attributes).

    In any case, my example also shows how I would do it, by making the hats objects each with their own attribute alteration variables (you could add booleans too). I'm assuming that even if the different hats are included in your player animations that they will still appear on the ground or in menu's, HUD, etc so that they will still be objects within the scene.

    I created a family for hats and created family instance variables for all possible attribute adjustments. That way my functions can be completely generic to the specific hat type and just add all attribute values or subtract them. Since its adding and subtracting from the existing variable values you don't have to worry about any fHat vars with "0". So from my example you could just remove the function maps and call "add_Hat" and "subtract_Hat" directly. You are picking the hat to add by comparing a chosen string to the object type name of all fHats in the layout, and the subtract function picks similarly based on the vCurrentHat value.

    If you didn't want a ton of separate object types for whatever reason, you could have one hat object and create all your hat types as templates in a repo and then just load them into the layout off screen somewhere. In that case you would want to identify each object instance with a hat type identifier variable, e.g. "vHatType" = "Blue" in the templates. Or, as I said above, use some of the other suggestions in the thread.

    I'll be curious which way you choose to go if you want to post a final update in the future.

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