Generating a random number to choose an attack

This forum is currently in read-only mode.
From the Asset Store
Zombie attack is a survival game, where you have 2 types of games: Killer and survival.
  • Hi. Could someone point me to where I could learn about generating random numbers? What I'm basically trying to do is have it so that when an enemy moves in range, it attacks in one of four ways. I figured one way of doing it would be to generate a random number that would trigger a variable and thus determine the attack (like if the number is 1-20 it shoots, 20-40, it punches, etc).

    Of course, if you have a better way, I'm all ears. Thanks in advance for any help.

  • You can generate random numbers by typing random(40). This will give you a random number between 0 and 40.

  • Actually, you would want to do Random(40) + 1 because without the + 1 it will return a number 0 through 39, not through 40.

  • Thank you very much for the quick replies, it's appreciated.

    I just realized I might have another small problem and I'm unfortunately away from my machine with Construct temporarily. I'm assuming that I would have the sequence be: when enemy moves within x pixels of player, random number is rolled, compare random number to enemy private variable, if random number is (for example) 1, do kick, 2 do punch, etc. However, I want enemies to do one attack slightly more often than others, like: if random number is 1, kick, 2 or 3, punch, 4, 5, or 6, shoot. Could you tell me how I can set specific ranges for variables as in this example?

    Thanks again.

  • The way to do that is, assuming the random number rolled will be the variable applied to the sprite:

    Using events simply make multiple conditions stating the range you want to use.

    IE (pseudo code)

    (condition)

    Sprite1.variable "roll" equals 1

    Sprite1.variable "roll" equals 2

    Sprite1.variable "roll" equals 3

    (event)

    Do stuff

    -----

    (condition)

    Sprite1.variable "roll" equals 4

    Sprite1.variable "roll" equals 5

    (event)

    Do different stuff

    -----

    (condition)

    Sprite1.variable "roll" equals 6

    (event)

    Do some more different stuff

    So basically you change the variable of the sprite itself (randomly) to match a list of pre-determined conditions.

    ~Sol

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you very much (again)!

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