Problem with Millisecs()
Monkey Forums/Monkey Programming/Problem with Millisecs()
| ||
So I'm trying to use Millisecs for a method to reload a gun. It works and all but the counter doesn't start back at 0 each time, it just keeps going up. Even when not being called. So once the time gets passed like 68k it starts to lag my game. [monkeycode]reloadEndTime = Millisecs() +reloadtime * 1000[/monkeycode] Anyway to make it so it returns back to 0 once it's finished? |
| ||
Global timer:int = Millisecs() Global timerFlag:int = 0 Method OnUpdate:int() If timerFlag = 0 timer = Millisecs() timerFlag = 1 EndIf If Millisecs() >= timer + 1000 'do stuff like fire bullet timer = Millisecs() EndIf End |
| ||
Okay here's a different question, what would be the best way to make a timer class of my own that doesn't use Millisecs? |
| ||
You could count frames in the "onUpdate", other than that, using Millisecs is a good idea, and it will grant stable execution while counting frames, as the framerate can be modified depending on rendering complexity, could be not as efficient. Kind untested suggestion: [monkeycode] Class Timer Field interval:Int = 100 Method New(defaultInterval:Int) Self.interval = defaultInterval _counter = Millisecs() End Method Method Fired:Bool() if Millisecs()>_counter+interval then _counter = Millisecs() Return True Else Return False EndIf End Method Private _counter:Int = 0 End Class[/monkeycode] Haven't tested it, as I'm far from my desktop computer, but it should work like this: [monkeycode] 'Create the timer: Global myTimer:=New Timer(100) 'This timer "fires" every 100 millisecs() 'Inside the OnUpdate: If myTimer.Fired Then 'Do stuff EndIf 'To modify the Timer interval (speed) myTimer.interval = 200 [/monkeycode] |
| ||
I cannot believe, that a Millisecs() value over 68.000 will lag the game. Could'nt this be caused by an other problem? |
| ||
So once the time gets passed like 68k it starts to lag my game. I have left my apps running for days before and not had any problem with Millisecs(), maybe there is a bug in your code? |
| ||
I normally have a globally accessible gameTime:float which is counted in seconds and updated every time OnUpdate() is called. It's better than using raw Millisecs() because (1) You can easily pause the game while looking at maps and such (2) You can manipulate time to slow things down a bit if the game is lagging (3) It's probably more efficient than calling OS functions all the time For timers I use something like this: To use it you just start it up and update it on every app update. Then you can call IsDone() to check if it's finished. If it's not finished and you want to show an animation (e.g. a clip filling up) you read param and draw it appropriately. That's an old version, from before I started using a global gameTime. I may change it to use that and then I can get rid of the update function, but param will have to become a property or be replaced by a function. |
| ||
Here's a timer class I wrote to emulate the TTimer class in Blitzmax. It still uses Millisecs() but with added flexibility of being able to reset it. http://www.monkeycoder.co.nz/Community/posts.php?topic=1416#12820 |
| ||
Hey guys thanks for the help. Turns out the problem I was having wasn't due to the Millisecs, like you guys suggested. But the code you provided also helps me with my original code. Thanks again! |