[R109] Functions: No named parameters?

Get help using Construct 2

Post » Fri Nov 09, 2012 4:05 am

One of the best parts of the Functions plugin by rexrainbow was I could name a parameter with a string, like "BulletX" and "BulletY". With the new built in function object/event though it seems we can only access them by numbers.

Is there no way around this? Will this maybe be updated in a future release? It's nice having this built in, but calling and setting parameters by number quickly gets out of control.
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Fri Nov 09, 2012 2:08 pm

The official Function plugin from Classic also only used numbers for parameters, and the C2 Function plugin is based on the same. That's the main reason why. Also it's difficult to make the UI for variadic parameters within the editor (the 'add parameter' and 'remove parameter' links) when you need to specify both a name and a value. I think we'll leave it as it is for now - you could try dumping all parameters in to local variables in a function event so then you can reference them by name. That also allows for nicer syntax (ParamA + ParamB rather than Function.Param("ParamA") + Function.Param("ParamB")) as well as error checking (ParamC is rejected, but Function.Param("ParamC") is allowed).
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sat Nov 10, 2012 4:40 am

Having the parameters inside the function helps a little (although it adds an extra step to maintenance and adds to the growing list of variables in the expression helper), but it leaves the problem that A) It's unclear what the parameters are from the action calling the function since it only separates by comma, not name, B) the action calling the function has its variables mangled together. Compare the format of the plugin from rex to the native function call (using some completely made up code for example purposes):

Comparison screenshot between the rex plugin and native plugin

Now I could make these variables in the event calling the function too, but now I have 3 variables calling the action and 3 variables naming the parameters inside the function.

You also have to hope that you never decide to replace the order of your variables - Suddenly "Function.Param(0) + Function.Param(1) / Function.Param(2)" becomes completely different when you decide you don't need parameter 1 any more - Function.Param(2) becomes Function.Param(1)and you need to refactor because the position of the parameters changed.

I hate to complain about the awesome work you guys are doing, but it seems like right now the plugin from rex is actually more powerful and easier to use. It just seems that using numbers to define parameters as well as requiring local variables on both sides to make the code readable is probably the only thing I can think of in C2 that makes things harder than regular programming

B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Sat Nov 10, 2012 2:16 pm

I just don't like the idea of using strings as parameter names. If we went down that route, I'd want Construct 2 to be able to tell you in the editor if you used an incorrect parameter name, to help prevent you making mistakes from typos. That would mean using some kind of built-in expressions (like local variables) which aren't just simple strings. I'm not sure how we could design that.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sat Nov 10, 2012 4:17 pm

It would be nice to at least have the option - I would be much less concerned about a mistyping a string while having a clean project than mistyping a number and having a messy project (ie. I can typo a parameter index from Function.Param(1) to Function.Param(11) and C2 will not warn me). I'm not suggesting you switch from one to the other, it would just be nice if it supported both like the plugin by rex. Having a prompt when using a string (like when using a layer by name) would be handy, but I don't think it's critical considering there's no prompt for parameter typos by number anyway.

Also there are already parts of C2 that take a string as a parameter with no error checking - For example, calling a key in a dictionary.

This is just all my opinion. I have no idea how the rest of the C2 community feels about this, so maybe they are not as fussed as I am. I'm more than happy to keep using the plugin by rex, but it would be nice to see some of those features cross over into a plugin that is supported 'in-house' as it were.boolean2012-11-10 16:25:36
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Sat Nov 10, 2012 4:49 pm

I second this. For now I will keep using rexrainbow's object for the convenience of being able to name parameters. In my opinion it was a big improvement over Classic's function object mainly for that reason.
B
21
S
6
G
10
Posts: 1,024
Reputation: 7,445

Post » Sat Nov 10, 2012 11:24 pm

I agree that the official function plugin should keep using "number" index, since it makes the interface simple. "expression:Call" could not use "string" parameter as well.

To support "string" index, a solution is using my hash table plugin or official dictionary to transfer parameters into JSON string and set this string in parameter of official function. In "On function", unpack the JSON string by hash table plugin or official dictionary. to get parameter by string key.
[code](string key, value) -> hash table -> JSON string -> function parameter
-> "call function -> "on function"
-> function parameter (JSON string) -> hash table -> (string key, value)[/code]




Edit:
I update my hash table plugin to support create JSON string in an single expression.
rexrainbow2012-11-11 01:55:58
B
97
S
22
G
176
Posts: 4,108
Reputation: 103,019

Post » Sun May 12, 2013 9:19 am

Hi - Gonna res this thread because I'm getting a bit lost without named parameters.

How about a compromise? Let us set a note when we create a new function, where we can key in a description of what we want the parameters to be.

Then function call can show this note - if it detects one was set for the function name you key in.

I've kind of tried to do this with comments, but my functions are sat in one sheet and it is niggly to toggle between where I am to remind myself of what I need to do.

Also whilst I'm here, is there no way to make function call detect if the function name is invalid? Or even bring a drop down of the function names you are using?
B
20
S
6
G
2
Posts: 157
Reputation: 3,109


Return to How do I....?

Who is online

Users browsing this forum: MrLOL64 and 21 guests