Best practice to enter an array of values to an action

For developers using the Construct 2 Javascript SDK

Post » Tue Nov 04, 2014 8:31 pm

I need to get an array of values, like ["First","second","more,"etc"], into an action. The way I tried was to add several AddAnyTypeParam() calls but that will limit the number of values and give a lot of maybe unused input fields.

If I use a single free form entry I need to parse the input and handle all strange inputs. I also need to explain the format to the user.

Any thoughts how handle this?
B
18
S
7
Posts: 88
Reputation: 1,424

Post » Thu Nov 06, 2014 6:52 am

JSON string.
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679

Post » Thu Nov 06, 2014 9:41 pm

I am on my pad now so i can't check but will the parameter input really accept {xxxx}?
B
18
S
7
Posts: 88
Reputation: 1,424

Post » Sat Nov 08, 2014 5:30 am

Json string like "[1,2,3]" , it is a string.
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679

Post » Sat Nov 08, 2014 1:06 pm

I tested to use a JSON string as the input but could not get pass the input syntax checker.

First test for reference, this was an expected fail:
Image

Next, use single quotes as string input, not allowed:
Image

Ok, let's escape the double quotes, that might work, but no:
Image

Single quotes are not allowed as string in JSON and will also break the runtime right away.

More ideas needed.
B
18
S
7
Posts: 88
Reputation: 1,424

Post » Sat Nov 08, 2014 3:24 pm

In C2 you escape double quotes by doubling them :)
"[""Arne"",""was"",""here""]"
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sat Nov 08, 2014 5:26 pm

I wouldn't use a string: you have to parse it, which adds unnecessary string handling overhead, and JSON is especially awkward to use in expressions since all double-quotes have to be doubled up.

I'd just use an action to add one item, and the user can use multiple actions to add multiple items.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

Post » Sun Nov 09, 2014 12:30 am

Ashley wrote:I wouldn't use a string: you have to parse it, which adds unnecessary string handling overhead, and JSON is especially awkward to use in expressions since all double-quotes have to be doubled up.

I'd just use an action to add one item, and the user can use multiple actions to add multiple items.



It is a good idea. And it might need to indicate the end of aray assigning, maybe another action.
Action Push an element
Action push an element
....
Action do something using this array, then clean it
B
108
S
26
G
259
Posts: 4,430
Reputation: 145,679

Post » Sun Nov 09, 2014 12:48 pm

I tested with the JSON style input and it's very easy from the plugin side, more or less just a single line to get the array. However the 14 double quotes to get 11 characters in is a bit hard to put that syntax on the user. So I would probably just keep this as a last resort.

Having several actions and push the items one by one gets more complicated as I am already in a transaction flow and the array is also connected to a property.
All my actions in the flow is already standalone so I need to setup a sub flow just for this. From a user point of view this method seems fairly natural, just need to understand if the action is an AND or an OR for values(filters in my case).

My current test implementation is a simplified "CSV" method to get all values in one action. There is some special parsing involved, but not that much.

But what I really want of course is a AddAnyTypeArrayParam() :D
B
18
S
7
Posts: 88
Reputation: 1,424


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests