How do I handle multiple parameters in a function?

Get help using Construct 2

Post » Mon Nov 17, 2014 2:00 pm

Functions mystify me. I get the bare basic functionality of a function, but how would I properly use a function with multiple parameters? Does anyone have any samples I could look at?
B
6
S
2
Posts: 21
Reputation: 494

Post » Mon Nov 17, 2014 2:54 pm

I'd suggest taking a look at the manual page for the function object. In the dialog box that appears when you add the Call function action there's a button labeled Add parameter that will add a field to pass a parameter. It appears that you can add as many as you want. You can then access these parameters by getting the param expression while passing in the zero-based index of the parameter you want.
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,126

Post » Mon Nov 17, 2014 6:37 pm

I saw the manual entry for it, but I guess the part I'm stuck on is how those parameters actually get passed around. Like what does it actually look like to assign something to param 0, param 1, etc. To utilize it, it'd look something like function(x,y,z), but how do I tie x, y, and z to the contents of the function? Via local variable?
B
6
S
2
Posts: 21
Reputation: 494

Post » Mon Nov 17, 2014 6:44 pm

Use Function.Param(0), Function.Param(1), etc. I tend to pop all params into local variable on the first action to get them named.
ImageImageImage
B
71
S
22
G
247
Posts: 3,753
Reputation: 137,552

Post » Mon Nov 17, 2014 8:37 pm

Dave LeDev wrote:I saw the manual entry for it, but I guess the part I'm stuck on is how those parameters actually get passed around. Like what does it actually look like to assign something to param 0, param 1, etc. To utilize it, it'd look something like function(x,y,z), but how do I tie x, y, and z to the contents of the function? Via local variable?


Here's an example of calling a function with params, using them in the function, and using the returned value (if any).
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Nov 18, 2014 3:34 pm

Very nice! I'll play with this info when I get to my PC later today. I greatly appreciate it.

By the way, do functions need to be recursive to repeat/maintain variable changes, or would the following work:
If key_pressed then do function()
Where function can control player speed, direction, movement, sprite for walking, etc.
B
6
S
2
Posts: 21
Reputation: 494

Post » Wed Nov 19, 2014 1:14 am

Global variables maintain their value. Local variables don't, by default, so you have to make them 'static'. Hope that's what you're after. You can make functions recursive but I wouldn't unless you know what you're doing :)
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests