Counting REAL time instead of Game Time

Zorobay

Member
Reaction score
1
Hi. How do you count real time instead of gametime? Like a periodic event that happens every 0.01 seconds of real time instead of game time...
 

tooltiperror

Super Moderator
Reaction score
231
JASS:
library RealTime

	globals
		private real time
	endglobals
	
	private function Update takes nothing returns nothing
		set time=time+1.
		call TriggerSleepAction(1.00)
		call ExecuteFunction(SCOPE_PRIVATE+"Update") // I forget, what's the prefix? // Romek: Fixed
	endfunction

	function GetRealTime takes nothing returns real
		return time
	endfunction

endlibrary


It will not be precise at all, but it's as close as you will get. Side note, I forget how to get the library function prefix.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Why not Timer or periodic timer? I feel like it's much easier to do. Althought my jass is rusty here's what I came up with

JASS:
library Time initializer init

    globals
        private integer seconds = 0
    endglobals
    
    public function GetSeconds takes nothing returns integer
        return seconds
    endfunction
    
    public function GetMinutes takes nothing returns real
        return seconds * 0.01677777
    endfunction
    
    private function callback takes nothing returns nothing
        seconds = seconds + 1
    endfunction
    
    private function init takes nothing returns nothing
        local trigger t = CreateTrigger( )
        call TriggerRegisterTimerEvent( t, 1.00, TRUE )
        call TriggerAddAction( t, function callback )
    endfunction
    
endlibrary
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
Hi. How do you count real time instead of gametime? Like a periodic event that happens every 0.01 seconds of real time instead of game time...
For this purpose, you can use the known multipliers for game time to real time for the set game speed, using [ljass]GetGameSpeed[/ljass]. "Fast" in the F10 menu is MAP_SPEED_NORMAL (1.0x), "Normal" is MAP_SPEED_SLOW (1.25x), "Slow" is MAP_SPEED_SLOWEST (1.66x). I have not tested the editor's Fast or Faster.

By checking how long a timer runs during a wait of a specified length, you can get a multiplier for game time to real time. Since waits are inaccurate (AFAIK always running up to a tick longer than the specified time), multiple trials should be averaged and a long period should be used. Here is my test code.

JASS:
globals
    udg_Interval = 30.
    udg_Adjustment = 0.
    udg_Increment = 1.
endglobals

function tester takes nothing returns nothing
    local timer t = CreateTimer()
    call TimerStart(t, 2*udg_Interval, false, null)
    call TriggerSleepAction(udg_Interval)
    if TimerGetElapsed(t) > 0.10 and TimerGetElapsed(t) < TimerGetTimeout(t) then
        set udg_Adjustment = (((1 - (1 / udg_Increment)) * udg_Adjustment) + ((udg_Interval / TimerGetElapsed(t)) / udg_Increment))
        set udg_Increment = (udg_Increment + 1)
    else
    endif
    call PauseTimer(t)
    call DestroyTimer(t)
    call BJDebugMsg("One game second is " + R2S(udg_Adjustment) + " real seconds.")
    set t = null
endfunction

function runtest takes nothing returns nothing
	call ExecuteFunc("tester")
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001_Copy takes nothing returns nothing
    local timer t = CreateTimer()
    call TimerStart(t, bj_PI/3., true, function runtest)
endfunction

Why not Timer or periodic timer?
Because timers run in game time, which varies in scale with game speed.
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
count game time, just like timer, try to measure TriggerSleepAction(0.0) with timer on different game speeds.
TriggerSleepAction(0) appears to pass however much time is left until the next game tick. Longer waits are needed for a valid comparison.

If TSA measures game time, why does a timer run to a (consistently) different elapsed time for the same TSA value on different game speeds? (Tested.)

[edit] I only seem to be able to have a game run at MAP_SPEED_SLOWEST, MAP_SPEED_SLOW, and MAP_SPEED_NORMAL. MAP_SPEED_FAST and MAP_SPEED_FASTEST do not seem to differ from normal. Their factors are 0.6, 0.8, and 1.0 (tested and also reported here), and their handle IDs are always (?) 0-2 for the first three, so you can use:
JASS:
function GameToRealFactor takes nothing returns real
    return (3+GetHandleId(GetGameSpeed()))/5
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top