Millisecs and max integer value
Blitz3D Forums/Blitz3D Programming/Millisecs and max integer value
| ||
In the game I am developing, I use millisecs() alot. For example: In the online chat-function I refresh the chatbox every x seconds, and I store the current millisecs in a variable to know when it should be updated again But then I remembered Blitz3D has a maximum value an integer variable can hold. So my question is: what will Blitz3D store when millisecs() returns a higher value than the maximum integer value? |
| ||
In the PeekInt document help, it says -2147483648 is the lowest 2147483647 is the highest. I heard that when MilliSecs() goes too high, it goes from the highest number, to the lowest number and keeps counting up again. |
| ||
When the positive millisecs value gets maxed out, it will flip over to the maximum negative value, begin counting back to zero, and eventually flip over to a positive count again. For safe timeout interval checks, use a relative comparision. eg. Where 'current_time' is the current Millisecs time, and 'deadline_time' was the Millisecs time when the interval timeout was set, with the number of millisecs for the interval added to it. |
| ||
I am not sure how much of a concern this is, unless your Blitz app is run on a machine which will run for over three weeks without a restart. Anyway, these might help to give a more useable time-keeping format instead of Millisecs() directly, though of course it will be a little slower than Millisecs() alone, I'm sure iot would be a neglible difference for most purposes. While Not KeyDown(1) Print Weeks(Elapsed(MilliSecs))+" wks "+Days(Elapsed(MilliSecs))+" dys "+Hours(Elapsed(MilliSecs))+" :: "+Minutes(Elapsed(MilliSecs()))+" : "+Seconds(Elapsed(MilliSecs())) Wend Function Elapsed%(Millis) If (Millis<1) Millis=Millis+2147483647 End If Millis=Millis Mod 2147483647 Return Millis End Function Function Seconds#(Time%) Local fSec#=Float(Time Mod 60000) * 0.001 Return fSec# End Function Function Minutes%(Time%) Local fMin#=(Floor#(Float#(Time*0.001)-(Float(Time% Mod 60000) * 0.001))) Local nMin= Int(Floor#(fMin# / 60.0)) Return nMin Mod 60 End Function Function Hours%(Time%) Local fHours#=(Floor#(Float#(Time*0.001)-(Float(Time% Mod 36000000) * 0.001))) Local nHours= Int(Floor#(fHours# / 3600.0)) Return nHours Mod 24 End Function Function Days%(Time%) Local fDays#=(Floor#(Float#(Time*0.001)-(Float(Time% Mod 86400000) * 0.001))) Local nDays= Int(Floor#(fDays# / 86400.0)) Return nDays Mod 7 End Function Function Weeks%(Time%) Local fWeeks#=(Floor#(Float#(Time*0.001)-(Float(Time% Mod 604800000) * 0.001))) Local nWeeks= Int(Floor#(fWeeks# / 604800.0)) Return nWeeks Mod 7 End Function |
| ||
Use a timer instead of Millisecs(). Avoids all of this hassle. [edit] I've used Blitzmax for ages now and was thinking this: myTimer:tTimer = CreateTimer(1) Repeat If myTimer.Ticks() >= 5 'do stuff myTimer._ticks = 0 EndIf Forever...unfortunately I'd forgotten that Blitzmax timers <> Blitz3D timers, and Blitz3D ones are next to useless. |
| ||
and Blitz3D ones are next to useless. Yeah, that's pretty much true; I've never used a B3D timer for anything except limiting games to 60 FPS, and I don't even use THAT anymore, since I discovered the CPU breathing code. :P It's true, though, that MilliSecs() will "loop" around to minimum integer when they pass maximum integer. This is why you must ALWAYS SET YOUR TIMERS TO MilliSecs() AT THE BEGINNING OF YOUR PROGRAM, because otherwise they might start at 0, which would mean that MilliSecs() would have to catch up. I am not sure how much of a concern this is, unless your Blitz app is run on a machine which will run for over three weeks without a restart. Another word about MilliSecs(): I have found that the value of MilliSecs() is always increasing once a computer's been turned on, EVEN IF IT'S LEFT IN STANDBY OR HIBERNATION MODE. I know this because my PC is almost always on standby, and my laptop stays in hibernation; I rarely tell either of them to actually Shut Down. So you may encounter this MilliSecs() problem more than you think, and it's a good idea to have a plan for it. |
| ||
Thanks for all the information, I have changed my code now in a way that nothing weird will happen when Millisecs() flips to a negative value |
| ||
Yeah, I've always heard abd things about B3D Timers, so I've never used them myself. Glad you got a working solution, blitsytze! |