Snippet Game Time

Nestharus

o-o
Reaction score
84
Game Time​

---------------------------------------------
It's time to get your game on


JASS:

/*Utility Information
//===================================================================
Name: GameTime
Version: 1.0
Author: Nestharus

Description: This will track the game time.

Requirements: None

Installation: NA

Usage:
------------------------------------------------------------------
-GetElapsedGameTime takes nothing returns real
 will return the current elapsed game time
 
 local real currentGameTime = GetElapsedGameTime()
------------------------------------------------------------------*/
//===================================================================

library GameTime initializer Initialization
    globals
        private timer gameTime = CreateTimer()
    endglobals
    
    function GetElapsedGameTime takes nothing returns real
        return TimerGetElapsed(gameTime)
    endfunction
    
    private function Initialization takes nothing returns nothing
        call TimerStart(gameTime, 1000000, false, null)
    endfunction
endlibrary
 

Sevion

The DIY Ninja
Reaction score
424
I don't know why anyone would want GameTime, but you used it in Timed ;o Then again, I guess some people would have usage in this. ;o Nice.
 

Nestharus

o-o
Reaction score
84
Submitted because a lot of my other stuff will require this.

People don't like the idea of these small snippets plugging into an arching framework, so I gotta split them : |.

MFN had like... 15 plugins... so get ready for a torrent of submissions ><
 

Azlier

Old World Ghost
Reaction score
461
Hey, this looks like something that actually might work. I see no actual use, but still...

Make this one change, though:
JASS:

globals
    private integer gameTime = 0
endglobals

private function Initialization takes nothing returns nothing
    call TimerStart(CreateTimer(), 10000, true, function AddToGameTime)
endfunction
 

Nestharus

o-o
Reaction score
84
That'd be a bad thing to do =o. It'd make it slightly slower

JASS:
return gameTime + TimerGetElapsed(runGameTime)


Using a constant vrs using GetExpiredTimer() is better =).
 

Azlier

Old World Ghost
Reaction score
461
Ah, didn't see that. Unconstantize the timer, instead. Having a constant handle is a stupid idea.

Vex's optimizer tries to inline constants, you see? What happens when it inlines runGameTime with CreateTimer()?
 

Nestharus

o-o
Reaction score
84
It has a lot to do with it??

JASS:
return gameTime + TimerGetElapsed(runGameTime)


And if that's not enough

JASS:
function GameTime_Get takes nothing returns real
        return GameTime__gameTime + TimerGetElapsed(GameTime__runGameTime)
    endfunction
    
    function GameTime__AddToGameTime takes nothing returns nothing
        set GameTime__gameTime = GameTime__gameTime + 10000
    endfunction
    
    function GameTime__Initialization takes nothing returns nothing
        call TimerStart(GameTime__runGameTime , 10000 , true , function GameTime__AddToGameTime)
    endfunction


Is it inlined? no

Ok, performing another test using the Vex Optimizer Program. I apparently mis-understood ^_^. I thought you meant Vex's JH Optimizer, which does inline etc.

More proof
JASS:
function GameTime_Get takes nothing returns real
return E+TimerGetElapsed(V)
endfunction
function PV takes nothing returns nothing
call DisplayTextToPlayer(GetLocalPlayer(),0,0,I2S(GetHandleId(GetExpiredTimer())))
call DisplayTextToPlayer(GetLocalPlayer(),0,0,I2S(GetHandleId(V)))
set E=E+1
endfunction
function QV takes nothing returns nothing
call TimerStart(V,1,true,function PV)
endfunction
 

Jesus4Lyf

Good Idea™
Reaction score
397
This is a bit silly.

The reason you make the timer periodic and expire is because the game may last longer than the period.

But reals have a maximum value anyway - why not just use that for the period? Same effect, more efficient.

Anyway, a significantly better version of this (the version I use):
JASS:
library GameTime initializer OnInit
    globals
        private constant real FOREVER = &#039;ever&#039;
        
//=======================================================
        private timer GameTimer = CreateTimer()
    endglobals
    
    public function Get takes nothing returns real
        return TimerGetElapsed(GameTimer)
    endfunction
    
    private function OnInit takes nothing returns nothing
        call TimerStart(GameTimer, FOREVER, false, null)
    endfunction
endlibrary
 

Nestharus

o-o
Reaction score
84
Changed : )

Originally had it like what you showed Jesus, but changed because I had wanted to do it for infinity (kinda pointless I guess).

Now it's back to what it was : D.

Also, I don't know of a maximum value for reals?? If you know of one, please let me know ^_^.

As far as I know, reals can go up until your computer crashes o-o (pretty much infinite)
 

Igor_Z

You can change this now in User CP.
Reaction score
61
So this is for getting game time and using it in other triggers or what? Add some discription up there plz
 

Jesus4Lyf

Good Idea™
Reaction score
397
constant timer stays because it's faster
Prove it.
it's pointless to use one that's not constant
Wrong, some version of Optimizer will inline it screwing this system completely?
smaller numbers run faster than bigger numbers
Prove it.
if you have found a limit for reals I'd like to know it. I've yet to find a limit, and I've made some massive ones. They can be bigger than strings O_O.
How long is a piece of string?

Anyway, they're 32 bit. I have my doubts about how well Warcraft III does bounds checking, but I expect strange things happen when you pass a certain limit.
 

Nestharus

o-o
Reaction score
84
Jesus4Lyf, you are right that they are 32 bit, but wc3 has stuff in the background "for reals only" that allows them to go up to mega sizes, and I have yet to find a limit : ). Integers have nothing in them that allows them to get bigger tho : (.

For reals, you can't directly assign them big numbers : ). I do stuff like real = 10000000

set real = real * 10000000

or real .00000001
set real = real /10000000

you can just increase and decrease them to any limit (you are limited on the numbers you input, but if you use other reals then it's still any limit o-o).

etc : d.

Wrong, some version of Optimizer will inline it screwing this system completely?

Can you show me a version? I used Vexorian's latest without any probs and I posted the code up.

smaller numbers run faster than bigger numbers

I think this really only applies for when you pass the 32 bit limit on reals : ), which is why I changed it : P.

constant timer stays because it's faster

Constants always run faster? Constant functions are pure machine code and constant variables are static ;o. Then again, the optimization here would be for starting the timer and retrieving the timer (very slight, would probably have to run it mondo times to see a dif).

So this is for getting game time and using it in other triggers or what? Add some discription up there plz

There is a description =o.
 

Nestharus

o-o
Reaction score
84
Already made the timer non-constant since that's what everyone wanted =). This was made for the community, not me, so you guys get the final say. That's my new outlook on things ; D.
 

Nestharus

o-o
Reaction score
84
It'll do that on certain optimizers from what I heard. Vex's isn't one of them and I've yet to see a case of where it does that ; ).
 
General chit-chat
Help Users

      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