How do I Create A Random Timer?

Get help using Construct 2

Post » Wed Sep 09, 2015 3:39 am

What I'm looking to do is have a timer that counts down to zero every time a level begins, but I want the starting number to vary each time the level is played. Is there an example of this somewhere?
B
19
S
3
Posts: 62
Reputation: 1,764

Post » Wed Sep 09, 2015 4:39 am

Here you go.

Create a global variable "Random Timer"
Create 3 separate Events

Condition: On Layout start
Action: Set variable Random Timer to "round(random(a,b))" where A is your min number and B is your max number. This will select a number between the two.

Condition: Every 1 Second
Action: Subtract 1 from Random Timer

Condition: Variable Random Timer <= 0
Action: Do what you want here..

Edit: Added Round Expression. Thank you for pointing that out @RamPackWobble
Last edited by Badmiracle on Wed Sep 09, 2015 8:46 am, edited 1 time in total.
B
77
S
25
G
9
Posts: 85
Reputation: 9,230

Post » Wed Sep 09, 2015 6:50 am

@Badmiracle - If you change your last event from Variable Random Timer = 0 to Variable Random Timer <= 0 then it should work .

As you have it the timer will be a floating number and might never = 0 (it might be 0.4 then -0.6 for example)

or you could round the random to the nearest whole number.

There are other ways to do this.
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,882

Post » Wed Sep 09, 2015 9:31 am

no problem.

here is another method.

https://goo.gl/5z52Ix
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,882

Post » Wed Sep 09, 2015 9:47 am

@Badmiracle

Instead of
Condition: Every 1 Second
Action: Subtract 1 from Random Timer

Use
Condition: Every tick
Action: Subtract dt from Random Timer

to be more accurate.
B
15
S
6
Posts: 105
Reputation: 1,356

Post » Wed Sep 09, 2015 11:20 am

For the random number it is better to use "floor(random(b+1))" (or "choose" function if you don't have many numbers to choose from) instead of "round(random(a,b))".

round(random(a,b)) does not generate numbers equally, border numbers (a and b) have 50% less chances to be generated than the numbers between "a" and "b".

EDIT: it should actually be "floor(random(b-a+1)) + a" if you want a range between f.ex. 5 and 10. What I've mentioned before is for ranges starting from 0, like 0 to 10.
ImageImageImage
B
27
S
16
G
79
Posts: 1,013
Reputation: 44,245


Return to How do I....?

Who is online

Users browsing this forum: bclikesyou, brunopalermo, kellehk, lahbach, ST314 and 30 guests