Randomly Pick a value from an array

Get help using Construct 2

Post » Thu May 30, 2013 4:10 am

How do i randomly pick a value from a given array.

I have an array where "On Start of Layout" i "Push back" some value into the array.

Later on in the game i would like to randomly select a value of this array.

i have done something like below (this action is executed with the following condition)

System countdown >= 1 (this is just a countdown timer).
System Every 1.5 Seconds



When i play the game it only displays the randomNumber.

B
5
Posts: 10
Reputation: 245

Post » Thu May 30, 2013 4:14 am

The array has 4 elements.
B
5
Posts: 10
Reputation: 245

Post » Thu May 30, 2013 4:18 am

Use the & not +
B
100
S
31
G
11
Posts: 1,552
Reputation: 21,497

Post » Thu May 30, 2013 4:23 am

@ArcadEd

Thanks for the hint, however it still does not solve the problem





username55552013-05-30 04:24:55
B
5
Posts: 10
Reputation: 245

Post » Thu May 30, 2013 4:47 am

Set randomNumber to floor(random(charArray.width)). And do make sure your arrays size is not smaller than what you need.vee412013-05-30 04:49:13
B
24
S
8
G
7
Posts: 756
Reputation: 7,242

Post » Thu May 30, 2013 4:55 am

What Vee41 says. Plus, make sure your array Height and Depth are at least 1, even if you are not using them. Having them at 0 caused me similar issues in the past.

B
100
S
31
G
11
Posts: 1,552
Reputation: 21,497

Post » Thu May 30, 2013 5:35 am

@vee41 & ArcadEd thanks for the hint. But i have found the problem. Instead of Push back, i should put it at front.

It seems this is what happen when i use "push back"

Array ["","","","","element","element","element"]

So when a randomNumber generates the number 0 - 4 it will returns 0 by default because there is nothing there.

If i use "push front" this is what happen

Array ["element, "element", "element", ....]

Thanks for the hint though.
B
5
Posts: 10
Reputation: 245


Return to How do I....?

Who is online

Users browsing this forum: DJKeefy, mekonbekon, SputnikCZ, TRMG and 9 guests