How do I find a number within an Array?

Get help using Construct 2

Post » Sun Oct 29, 2017 7:39 am

I'm having an issue where at the start of my layout I fill my array with 1's. Then after that, I go in and add some 2's to the array. However, whenever I try to set a global variable to the first X Index of 2 it always returns -1, meaning there is no 2 in the array even though there plainly is. I even set it up so that the code literally says

Array Contains Value 2 -> Set TestVar to Array.IndexOf(2)

And the TestVar returns -1 everytime, how is this possible?

.CAPX LINK
https://drive.google.com/open?id=0BzhJb ... 3hYVXBZNjg


Thanks for any help that can be provided
Last edited by VictoryX on Sun Oct 29, 2017 8:30 am, edited 1 time in total.
Image
B
53
S
21
G
3
Posts: 210
Reputation: 5,633

Post » Sun Oct 29, 2017 8:17 am

Your function is never executed, because loopindex is always =0
I suggest you add a large text field to your layout and use it for debugging.
Try for yourself:
DebugText->Append text "Loopindex:" & loopindex
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
19
S
9
G
158
Posts: 2,401
Reputation: 84,429

Post » Sun Oct 29, 2017 8:31 am

You are correct, oversight on my part. I fixed the function not running and yet TestVar returns -1. I even double checked the array in debug mode and visually saw it contained the value 2.
Image
B
53
S
21
G
3
Posts: 210
Reputation: 5,633

Post » Sun Oct 29, 2017 10:39 am

I think IndexOf() only works with 1D arrays, as it can't return two values for X and Y axes.
Array->Contains value can tell you if this value exists, but to find its coordinates in 2D array you need to loop through it using "For each XY".
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
19
S
9
G
158
Posts: 2,401
Reputation: 84,429


Return to How do I....?

Who is online

Users browsing this forum: Lancifer and 0 guests