How do I call function by name?

0 favourites
From the Asset Store
Medeival & Viking War Horns Sounds / 22 Unique Calls / 2:40 minutes of audio
  • Hi,

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

    But I tried and it doesn't work, I'm not very experienced in scripting so can you explain me how to do it?

    I'm not clear if I also have to write my function via script or it's fine with the construct editor too (it would be easier that way).

    construct.net/en/forum/construct-3/how-do-i-8/call-function-name-146123

  • This is the correct syntax:

    runtime.callFunction("prova");
    
  • Thanks for the replies.

    Could you also tell me what is the correct syntax for the parameters?

    I still don't know javascipt very well and I don't understand what "e" means ( (...command.split(",").map(e => e.trim())) )

    Shouldn't the syntax for entering the parameters also be the one in this image? I tried but it doesn't work. Instead your example does.

  • If you know the exact number of parameters, then just list them after the function name, for example with two parameters:

    runtime.callFunction("prova", 10, 20);

    or

    runtime.callFunction("prova", "text", "some other text");

  • The simplest example.

    	runtime.callFunction("myFunction");
    

    with parameters

    	runtime.callFunction("myFunction", Parameter0);
    	runtime.callFunction("myFunction", Parameter0, Parameter1);
    

    with local variable

    	const parm1 = localVars.Variable1;
    	runtime.callFunction("myFunction", parm1);
    

    with global variable

    	const parm1 = runtime.globalVars.Variable1;
    	runtime.callFunction("myFunction", parm1);
    

    with Instance variable

    	const player = runtime.objects.Sprite.getFirstInstance();
    	const playerScore = player.instVars["Score"];
    	runtime.callFunction("myFunction", playerScore);
    

    with Instance behaviors property

    	const player = runtime.objects.Sprite.getFirstInstance();
    	const playerMaxSpeed = player.behaviors.Platform.maxSpeed;
    	runtime.callFunction("myFunction", playerMaxSpeed);
    

    --------------

    Special case

    FYI If you need multiple parameters but are not sure about the number of parameters. can be like this: Note that if you do this all Parameter will be used as strings

    	const command = "myFunction, Parameter0, Parameter1";
    	runtime.callFunction(...command.split(",").map(e => e.trim()));
    
  • Ok.

    I had tried all the syntax except the right one,

    because my function's parameters are strings and I have to put " for each parameter

    runtime.callFunction("test","parameter1", "parameter2");

    Thanks for all the examples.

  • You don't need to use scripting to call a function from a string. The event system allows for this too with the function maps feature.

  • You don't need to use scripting to call a function from a string. The event system allows for this too with the function maps feature.

    Ashley I don't think you can force users to use feature maps because they are inconvenient.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You don't need to use scripting to call a function from a string. The event system allows for this too with the function maps feature.

    The current documentation on function maps is so vague. and the editor '#function-maps' examples also simple. I'm trying to understand, but I still don't know how to use it. Documentation only said, the function maps is very useful. But you didn't explain how it is useful and how it should be used. Can you write more detailed documentation for it? or contact Viridino Studios to show us more examples of how to use it?

  • After you have programmed in a function, you use an event such as start of layout and then use the action under functions called "map function to string" you can add it to a "map" which is like a folder in my mind and then assign the string that you want to call the function. You are then able to use the "call mapped functions" action in combinations with strings, particularly helpful is from variables and arrays.

  • You don't need to use scripting to call a function from a string. The event system allows for this too with the function maps feature.

    It's easier to learn JavaScript than to use that, lol. I bet you wouldn't even use it on an actual project as well.

  • > You don't need to use scripting to call a function from a string. The event system allows for this too with the function maps feature.

    It's easier to learn JavaScript than to use that, lol. I bet you wouldn't even use it on an actual project as well.

    Lol true))

    Since the new "Function Map" was introduced years ago I haven't seen anyone using it as sometimes this question comes randomly (Discord or Forum) and 99.9% say they use that trick using Scripting even if they dont know how to use JS or never use scripting including myself as at the moment I just use scripting when I need to call a Function by string. The few times that someone said they use it was a new user, so I guess because they never used the old Functions therefore they dont know the difference, so they wouldn't mind spending that much time trying to understand how the New "Function Maps" works which is quite complicate.

    The new "Function Map" could have a revisit to make it more useable in my opinion but if not it's fine the scripting trick is super easy, it's just every time that I use it I have to look at my examples as I keep forgetting the Script structure for the Functions but is not a big deal.

  • Function maps are pretty simple. I think people are overreacting. It's pretty much "map function to string", which assigns a string to a function so you can then refer to it by that string. Then you can have different maps with different names, and "call mapped function" calls a function by its assigned string.

    It also has the advantage of separating the string from the function name. With calling directly by a string of the name, if you rename the function, you break your events. So it's a brittle setup. With function maps, you can rename a function and everything still works.

    The existing "Function maps" example shows it pretty much in full. There's not much else to cover.

  • Aside from the calling of maps/strings, which is already puzzling, have you considered how strange and limited the forwarding of parameters is?

    It expects that developers usually don't change parameters as it passes through functions, especially its order, there is so little flexibility, it's shocking it's designed by an adept programmer who takes this feature for granted in typed languages.

    By the time you have finished planning the Functions Maps, you have ended up spending more time changing the events than it would have taken to just write 1 to 3 lines of JavaScript to do the same thing, with better readability and flexibility to adjust with little changes in the event sheet.

    I mean, it's proof enough that people are willing to use JavaScript to call functions with strings rather than use the built-in. People would always find the more convenient option.

  • Here are recent poll results from a local C3 group:

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