why random(a)+b does not working for timers?

i wanted to do simple event like a spawner spawn objects 2sec or 3 secs later. only random(3) or random(2) works but random(1)+2 is not working .it spawns only 2secs later.

any idea?


The correct form is Random() with a capital ‘R’, the expression editor should throw you an error when/if you try random() :confused:
Try Random(1) + 2 instead. It should work.

oh i wrote it with r here my bad.i tried it with Random(x) number it doesnt work.is there any other way to do this?

Maybe the trigger once condition causing the problem depends on what you trying to achieve.
If you want to spawn an object every 2-3 seconds, I suggest to remove the trigger once condition from the event.
If you want to spawn an object only once after 2 or 3 seconds then spawn nothing for a while, instead of using the trigger once just pause the timer after reset and when you need to spawn an object just start the timer again.
If you want only one object to be present in the scene at the time, instead of trigger once, simply check the number of objects in the scene. If the number of objects in the scene < 1 or = 0 then spawn a new object.

without trigger only once is same.i think they didnt add that to timers:/
i just want to spawn a single object that spawns 2secs later or 3 secs later . i mean im trying to make random spawn like this. 1 spawn 1empty 1 spawn 2 empty pattern like picture below


The problem is that Random(1) + 2 can return 2 or 3, but the expression is evaluated at each frame, if the game runs at 60 FPS and the timer is >= 2 seconds, there are 60 chances per second to get a 2 and run the timer before 3 seconds. Maybe I can explain it better:
Imagine that the timer is at 1.98 seconds, the next frame it will be 2.01 seconds, now you start to test at every frame the expression:

Frame 1: Random(1) + 2 = 3, timer at 2.01 seconds >> actions doesn't run Frame 2: Random(1) + 2 = 3, timer at 2.04 seconds >> actions doesn't run Frame 3: Random(1) + 2 = 2, timer at 2.07 seconds >> actions launched!
Do you get it?, the timer is at 2.07 seconds and the event will be launched, Random(1) has 50% chances to be = 0, so it’s very likely that the event will be launched very close to 2 seconds :neutral_face:

To fix it generate the random number only one time per timer reset, this way the random number will be kept over frames, avoiding the bad behavior:

Conditions: Timer "1spawn" is greater than Random1SpawnVariable Actions: Create object, set animation, set frame, etc. Reset timer "1spawn" Do = Random(1) + 2 to the variable Random1SpawnVariable

2 things to note:
Trigger once has no effect, because the timer condition will be true only one time anyway, as you reset the timer.
Not sure, but modifying the animation frame first, and then switch the animation will surely set the animation frame to 0 again.

1 Like

it works like a charm^^ thank you

but i thought that random(1)+2 will evaluated at each seconds because it says “time in seconds:” .it’s good to learn these facts.