Need random events to happen during game play

Hi,

I need some random events to happen during the game play of my game. But not sure how to make it so it looks random.

Basically it’s weather effects like snow, rain and fog but I want them to appear randomly during the game.

Any ideas ?

Dave [import]uid: 117617 topic_id: 29122 reply_id: 329122[/import]

Couldn’t you create a timer to commence the effect, passing in a randomised number as the delay parameter? [import]uid: 33275 topic_id: 29122 reply_id: 117141[/import]

Good idea, forgot all about the delay parameter.

Think I have done this right (between 1 and 5 minutes) -

enterScene:
weatherTimer = timer.performWithDelay( math.random(60,300)*1000, weatherEffectsOn )

overlayEnded:
timer.cancel( weatherTimer )
weatherTimer = timer.performWithDelay( math.random(60,300)*1000, weatherEffectsOn )

Basically overlayEnded is called if the “Game Over” overlay is displayed and they click re-match.

I also switch off weather effects (weatherEffectsOff) after each goal, so this means the effects will only happen once per match at a random time between 1 and 5 minutes.

Dave [import]uid: 117617 topic_id: 29122 reply_id: 117150[/import]

The only thing I’d suggest is to localise the math.random call and perhaps do the calculation outside of the actual timer call, something like:

[code]

local mRan = math.random

local ranDelay = mRan(60,300)*1000

weatherTimer = timer.performWithDelay(ranDelay, weatherEffectsOn)

[/code] [import]uid: 33275 topic_id: 29122 reply_id: 117151[/import]

Hi,

Can I ask why that is, is it a speed thing ?

I use math.random a lot in this game.

Dave [import]uid: 117617 topic_id: 29122 reply_id: 117152[/import]

If you use math.random lots of times you certainly want to localise it - otherwise you’re performing a lookup to the math library each time, it’s definitely a performance hit.

[import]uid: 33275 topic_id: 29122 reply_id: 117155[/import]

What you have there will make it pick a random time, and then after that time change the weather. After that same time, change the weather again, etc… For example, it picks three minutes as the delay. After every three minutes, the weather changes.

If you wanted it to change the weather and reset the random delay each time (like after three minutes change weather, and then it switches to after 4.5 minutes change the weather and then 2 minutes), you could try something like this:

[lua]local mRan = math.random

local function changeWeather()
–Add a randomizer for what to change it to
–Still in the weather function:
if (weatherTimer) then
timer.cancel(weatherTimer)
local ranDelay = mRan(60,300)*1000
weatherTimer=timer.performWithDelay(randDelay, changeWeather) – Will set another random delay each time
end
end
changeWeather() – Begin the weather changing and timers[/lua]

[import]uid: 147322 topic_id: 29122 reply_id: 117165[/import]

Cheers “binc”.

Dave [import]uid: 117617 topic_id: 29122 reply_id: 117178[/import]