How do I pass an array as a parameter ?

Get help using Construct 2

Post » Fri Mar 24, 2017 11:31 pm

How do I pass an array as a parameter ?
How do I pass an array as a return value ?
How do I have a local array in an event ?
Same for dictionaries and XML trees...
And how do I count occurences of an element in an array ?
B
3
Posts: 5
Reputation: 185

Post » Sat Mar 25, 2017 7:34 am

For this explanation I assume the array is named array and the parameter through which you pass it is parameter 0.

How do I pass an array as a parameter ?

Enter the parameter you want as "array.uid" (without quotes) when calling the function. Of course make sure that only the specific array you want is picked in the sub-event from which you call the function.

Then in the "on function" event just use an array pick by UID condition as the second condition, enter "function.param(0)" as the UID by which you want to pick. Alternatively if you use your own variable you use to identify arrays, you can just pass that and pick an instance that has the right value.

How do I pass an array as a return value ?

Just use array.UID again, similar to above.

How do I have a local array in an event ?


You can't set an array as local to an event, this is one of the drawbacks of construct 2. There are some workarounds though. One I use is that I have a global dummy array. In the every event/function/whatever/ it is used it first gets cleared, resized. Then I use it as I wish. So far this has not came back to bite me, but knowing how unpredictable loops sometimes are in construct (the "for"/"for each" loops seem to run iterations in parallel rather than sequentially) I only use it in while and repeat loops only or events without loops.

Another is to create the array in the event to use it and destroy it in the end. But this one is not as simple as you might think, because in the tick/frame in which an object is created it cannot be picked events apart from the pick by UID event. A workaround for this is to have a function that creates and object and returns its UID.

The an alternative for one dimensional arrays is to use a local string variable and build a coma "," or semicolon ";" or pipe "|" string where each value is an array element. Then use the tokenat and tokencount expressions to retrieve the element. That is truly local but it has its own limitations.

Same for dictionaries and XML trees...

They also cannot be set to local. I would suggest trying to do something similar as I wrote for arrays.

And how do I count occurrences of an element in an array ?

I assume you meant occurrences of a particular element value (how many time the array contains "foo" for example). This one is simple. Just use a "for each element" array condition. Then add a subevent to the event containing the "for each element" checking the value at "array.CurX" if it matches the desired value. In that subevent add 1 to a local variable tracking occurrences.
B
15
S
6
Posts: 85
Reputation: 1,195

Post » Sat Mar 25, 2017 8:57 am

Thanks for the answers ! I kinda feared it would come to that, but now that I know someone has used them and is mostly fine. I feel more confident in trying myself. I'm not used to using UIDs, but as long as it's unique and immutable, it is really the same thing as passing the name of the object like I'd do in any text-based language, just more wordy. So, should be safe.
Alright, I'll try that out !
Yes, my last question was strangely worded but you got the meaning right :)
B
3
Posts: 5
Reputation: 185

Post » Sat Mar 25, 2017 10:47 am

Wait a minute.
In my project there are 4 arrays so far (and planning to add more). I have created each one by double-clicking on the layout, so actually each one is actually a different class, right ? It suited me fine because each one has a different size and very different treatments.
But I want to be able to count the occurences of a particular element in any type of array, because that's useful for any array. But the 'pick by UID' condition will only iterate over a particular sub-class of array, right ?
Same thing for 'pick by evaluate' or 'pick by comparison', they both ask for a user-defined sub-class of array. If there is a way to simply pick 'Array', that would do the trick for me.
But if there is not, then I see only two choices :
- make my arrays into instances of a single array sub-class, which means having to make an additional test each time I select an array (and additional risk of error and cost of maintenance)
- make a count function for each sub-class, which does not make it totally useless, but not far from...
Did I miss something ? Is there a better way ?
B
3
Posts: 5
Reputation: 185

Post » Sat Mar 25, 2017 11:35 pm

You can have a temp array in the function.

And pass any array to it as a parameter by using the Array.AsJSON expression.
Set the temp array to the passed parameter with the Array > Load action.

You can make instances of an array during runtime.
Which will make the picking (based on a instance variable) a lot easier.

If the arrays are 1D, then there is an awesome plugin to create arrays with a tag, during runtime.
plugin-mkarray-arrays-on-runtime_t179478

You can even (not that you need it now) bring an array into a container with an another object. That way each object has its own array, picking the object ... picks the array.
B
33
S
18
G
28
Posts: 2,474
Reputation: 20,912


Return to How do I....?

Who is online

Users browsing this forum: alextro, Yahoo [Bot] and 6 guests