0 Favourites

Another question about using 'random'

  • Good afternoon. Just bought the personal version of Construct 2 and couldn't be happier :)

    I am having trouble getting a random function to work. I am attempting to create a turn based game, where the enemy has two options each turn and picks a random one of those two each time. This is what I have.

    ----------------------------

    Global number 'Target' = 0

    Global number 'Turn' = 0

    System / Turn = 2            - Set 'Target' to floor(random(1))

    System / Turn = 2            - Play 'Animation1'

    System / Target = 0

    System / Turn = 2            - Play 'Animation2'

    System / Target = 1

    On 'Animation1' finished    - Set 'Turn' to 3       

    OR          

    On 'Animation2' finished

    ----------------------------

    Right now animation1 plays every time. If I disable animation1 then nothing happens. Please help me learn how to use random properly.

  • Manual: Generate a random float from 0 to x, not including x.

    Try random(2).

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Unfortunately when I try random(2) -or- floor(random(2)) nothing happens at all...

  • One other common mistake i've made myself is this (pseudocode):

    [ randChoice=random(2) if randChoice==0 then say "0" if randChoice==1 then say "1" ]

    You'd think this would work, but it will only work an incredibly small amount of the time. Why? You're not converting the random number into an integer. So it rolls numbers like 1.023882 or 0.47366 so it rarely ever hits exactly 0 or 1.

    So, use this instead:

    int(random(2))

    And of course, remember random is zero based. So, int(random(2)) will give you either 0 or 1.

    *Sigh, just saw the post above after I wrote this. You use floor, I use int.   

    <img src="smileys/smiley36.gif" border="0" align="middle" />

  • Try a 'system: trigger once' after the 'turn == 2' condition so you only set target to a random value once otherwise you'll be switching animations continuously.

  • You guys are amazing, totally solved it. Thank you!

    I will have a Kickstarter up in a few weeks for my video game, using Construct 2 to make my prototype. I will give the program and the community plenty of credit.

    If you want to check out my game so far it is at-

    necromancergame.com

  • Good luck! Looks like you've got a lot of it designed already.   <img src="smileys/smiley1.gif" border="0" align="middle" />

    I'm curious what you'll use if you get the funding. Continue with C2 or make an engine in a C variant?

  • The purpose of the Kickstarter will be to hire a programmer, so I can take care of pretty much everything else (art/writing/design/mechanics e.t.c)

    Hopefully I will find a very experienced programmer and so I will basically let them decide what engine/program/language to use that will best suit the game.

    Although some friends of mine have said that I should use the Kickstarter funds to hire a digital artist instead, let them re-do all of the art work, and learn to program myself.

    Either path is pretty daunting...

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