Help with some a timer

Monkey Forums/Monkey Programming/Help with some a timer

DeadFall(Posted 2012) [#1]
So I'm trying to make it so when I reload the players gun it takes x time to reload. But so far everything I've done just returns the initial variable I put into the function.

Here's the code

[monkeycode]
Function time:Int(value:Int)

Local startTime:Int = Millisecs()
Local currentTime:Int = value

If currentTime > 0
currentTime = value - ( (Millisecs() -startTime) / 1000)
End If

Return currentTime

End Function
[/monkeycode]

And I'm calling like this

[monkeycode]
If time(reloadtime) <= 0 Then
ammo = maxAmmo
canFire = True
End If
[/monkeycode]

But like I said all it does it returns like the variable I set first.


Jesse(Posted 2012) [#2]
you are either going to have to set some variables as global or create a Class to take care of that.

as it stands millisecs = startTime and millisecs - starttime = 0 and 0 / 1000 = 0

so yea that would never work.


DeadFall(Posted 2012) [#3]
Thank you. I realized I didn't even need starttime, just changed it to "1" and it works now.

But now I have a problem were it's being called before I want it called. And it's not resetting after hitting 0.


Samah(Posted 2012) [#4]
Generally i keep a field that stores "next available time".

[monkeycode]Field nextAvailableFireMillis:Int
.....
Local currentMillis:Int = Millisecs()
' check if the gun is ready to do any action
If currentMillis >= nextAvailableFireMillis Then
If (pressed reload) Then
' TODO: do reload
' make the gun unavailable for the entire reloading time
nextAvailableFireMillis = currentMillis + (reload time millis)
ElseIf (pressed fire) Then
' TODO: fire bullet
' make the gun unavailable for the time it takes to fire a bullet
nextAvailableFireMillis = currentMillis + (fire rate millis)
End
End[/monkeycode]