Array push back

Get help using Construct 2

Post » Thu Apr 09, 2015 9:03 pm

Currently in order to decide if I want to add a value into the stack I have to do a condition to compare before the action to push the value.
That doubles the work, doubles the number of comparisons.
I want to do a comparison in the action, without having to add a value to the stack if the comparison comes up with a value I don't want to add.
Nothing is not an option. ha ha

Edit:
To be more specific. I want to see if a value exists, before I add it, but theres two different values, x, and y, so two different arrays... as stacks.

Even more specific. The last value in the stack is what need to be compared.
Image ImageImage
B
170
S
50
G
179
Posts: 8,379
Reputation: 113,427

Post » Thu Apr 09, 2015 9:54 pm

Does it have to be done in the expression itself, or would it be okay to use a function (e.g "AddValue") that accepts the Array.UID as a param? This would involve adding all arrays you operate on to an ArrayFamily, and the function would pick the appropriate instance using the UID. It would definitely simplify your events on one end, but I'm not sure if that's what you're after.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Thu Apr 09, 2015 10:09 pm

Its in a for loop, and the arrays are children in the container sprite.
I don't think a function can work in a loop, and they have no way to add nothing.
Of course you could do some conditional trickery and create a param that you don't use, but that ruins the use of the index.

I'm thinking a nested string in a variable may be the best solution.
Sad.
Image ImageImage
B
170
S
50
G
179
Posts: 8,379
Reputation: 113,427

Post » Thu Apr 09, 2015 10:32 pm

Hmm, I don't quite follow. This is the way I'm envisioning the basic events if you used a function. What will not work here?

YOUR LOOP:
For "" from x to y:
-- Call "AddValue" (ArrayA.UID, ValueForA)
-- Call "AddValue" (ArrayB.UID, ValueForB)
-- Call "AddValue" (ArrayC.UID, ValueForC)

YOUR FUNCTION:
On "AddValue", Pick ArrayFamily by UID Function.Param(0)
-- If ArrayFamily.Back IS NOT EQUAL TO Function.Param(1)
----- ArrayFamily.Push(Function.Param(1))
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Thu Apr 09, 2015 11:04 pm

I see now. Yes that will work, but I was hoping there was some way to do conditionals in the actions so I wouldn't have to add more conditions.
Image ImageImage
B
170
S
50
G
179
Posts: 8,379
Reputation: 113,427

Post » Thu Apr 09, 2015 11:29 pm

I think I found a little hack you could use :D

Array.Push(YourValue) // push a new element regardless of the comparison
Array.Delete(Array.At(Array.Width-2) = Array.At(Array.Width-1) ? Array.Width-1 : Array.Width) // compare and delete if need be

I might have screwed that up, but essentially you can exploit the fact that trying to delete an element outside of the array does NOTHING. So you can put your conditional in the delete event. If the comparison passes, use an index outside the array (so it won't delete anything). If it fails, then delete the element you just added by using the last index in the array.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Thu Apr 09, 2015 11:44 pm

That might work.
Image ImageImage
B
170
S
50
G
179
Posts: 8,379
Reputation: 113,427


Return to How do I....?

Who is online

Users browsing this forum: dop2000, MrQuickGame and 35 guests