question regarding how I use timer.performWithDelay

I finally got my game running on a device, and now I am working on refining certain things, as best I can.

The thing I’m having trouble with is my ‘enemy firing timer’ setup.
It works, but not quite… as good as I’d like.
I’d appreciate any input on how I could achieve the following -

I have a timer being triggered when the playing collides with an enemy’s radar.
Basically when the user collides with an enemy’s radar, the enemy starts
casting rays towards the user and if the first hit of ray is the user then the enemy fires a bullet.
The problem is that the firing “delay” is constant and I’d prefer to have a bit of randomness, to give the illusion (however minor) of Ai.

I thought adding math.random(500, 2500) would do just that, but unfortunately it picks a random value (obviously between the specified values) and that is the constant rate of fire rather than on each iteration changing the delay.
If an enemy casts a ray and it hits the player, I want them to instantly fire a bullet towards the player, rather than the delay be set for the time between the enemy cast the ray and calls the function to fire a bullet.

Here is my radar collision function:

  local onEradarCollision = function( self, event )          if (event.phase == "began" and event.other.class == "PlayerTank") then                  EnemyFiringTimer = timer.performWithDelay(math.random(500, 2500), function()         ETfire( tank.turret, event.other )         end, -1)          elseif (event.phase == "ended" and event.other.class == "PlayerTank") then            print("collision with tank.radar radar ended.")       if EnemyFiringTimer then timer.cancel(EnemyFiringTimer) end          end    return true   end   tank.radar.collision = onEradarCollision   tank.radar:addEventListener("collision", tank.radar)  

– the ETfire function is responsible for casting a ray towards the user and then firing a bullet if the first hit was the player. –

I mean, I understand that when the user hits the radar is triggers the timer which chooses a value between, in this case, 500, 2500.
Obviously it doesn’t change because I’m not hitting the radar over-an-over calling that function.

My question is how can I code it to where the EnemyFiringTimer’s delay is constantly changing between the specified values?

hehe I hope I made enough sense… I’ve been staring at my code all day and it’s starting to get to me. :wink:

-Saer
 

Edit: Say I collide with three radars at the same time, each enemy will get a random value between 500, 2500 but whatever value it chosen will be the constant delay rather than changing.
I know it’s not constantly changing because I’m only colliding with the radar once.
I’m just trying to figure out to have the delay constantly change.
 

What’s happening is you are making a single call to timer.performWithDelay, which is set to have infinite iterations.

Instead, have each timer.performWithDelay call only use 1 iteration, but also have it manually trigger another timer.performWithDelay:

local function repeater(turret, other) ETfire( turret, other ) timer.performWithDelay(math.random(500, 2500), function() repeater( turret, other ) end, 1) end EnemyFiringTimer = timer.performWithDelay(math.random(500, 2500), function() repeater( tank.turret, event.other ) end, 1)

I’ve just done that quickly, so it might not be 100% correct, but the general idea should be.

Thanks, APP!

That did the trick! :smiley:
I tried something similar before, but my problem was that I didn’t have two timers.

Thanks again!

-Saer

What’s happening is you are making a single call to timer.performWithDelay, which is set to have infinite iterations.

Instead, have each timer.performWithDelay call only use 1 iteration, but also have it manually trigger another timer.performWithDelay:

local function repeater(turret, other) ETfire( turret, other ) timer.performWithDelay(math.random(500, 2500), function() repeater( turret, other ) end, 1) end EnemyFiringTimer = timer.performWithDelay(math.random(500, 2500), function() repeater( tank.turret, event.other ) end, 1)

I’ve just done that quickly, so it might not be 100% correct, but the general idea should be.

Thanks, APP!

That did the trick! :smiley:
I tried something similar before, but my problem was that I didn’t have two timers.

Thanks again!

-Saer