How do I pick randomly from a set of specific numbers?

Get help using Construct 2

Post » Tue Nov 22, 2016 10:49 am

Relatively simple question, I hope. I'm sure it can be done, but I just can't figure out what I need to type to make it so.

Let's say for example that my enemy has 5 attacks that it can do at various ranges. At close range, it can do attacks 1, 3, and 5. But at long range it can do 2, 4, and 5. In these cases, doing round(random(1, 5)) wouldn't work, because that would include the possibility of the other ranges' attacks in the result. I also can't rearrange the animations themselves, because there'd be no way to include attack #5 in both besides duplicating it, which I'd rather avoid. Is there a value I can type in to just say "set the variable to 1, 3, or 5"? Or do I have to do a workaround of some kind?
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Tue Nov 22, 2016 11:09 am

You can do that with "choose(1, 3, 5)"

https://www.scirra.com/manual/126/system-expressions

Values
choose(a, b [, c...])
Choose one of the given parameters at random. E.g. choose(1, 3, 9, 20) randomly picks one of the four numbers and returns that. This also works with strings, e.g. choose("Hello", "Hi") returns either Hello or Hi. Any number of parameters can be used as long as there are at least two.
B
34
S
9
G
3
Posts: 206
Reputation: 7,851

Post » Tue Nov 22, 2016 11:46 am

Ah, thank you! I figured it was just an expression I was overlooking. I was searching 'pick' when I should've looked for 'choose'. Much appreciated. :D
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Tue Nov 22, 2016 3:00 pm

@CJacobsSA You are welcome. I use it a lot too. Very helpful. If you need it more advanced you could also work with and array but "choose" is nice and quick ;-)
B
34
S
9
G
3
Posts: 206
Reputation: 7,851


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 30 guests