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

0 favourites
  • 4 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • 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?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)