Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Simple timer for your game (n00b level)

Author
Message
Starshyne Emir
2
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 18th Jun 2018 17:00
How many times did you need a timer and couldn't bake one yourself? Your problems are over!

I'll explain how the thing work and I bet anyone shall understand:

Right before the game loop starts, you shall do the following:

started = timer()

This will record the moment your level started.

If you need to set up a time limit, you do this:

time_limit = seconds * 1000 where seconds are obviously the amount of time you want to set up in... seconds.

To call whatever you wish to happen when time is over, just do this:

IF timer() >= started + time_limit THEN...

Got it?

Here's a little example:



Hope someone find it useful.
[size=+2]Forever and one[/size]
seppgirty
FPSC Reloaded Backer
9
Years of Service
User Offline
Joined: 3rd Jul 2009
Location: pittsburgh, pa.
Posted: 18th Jun 2018 23:24
Nice little lesson starshyne. Thanks for that.
gamer, lover, filmmaker
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 18th Jun 2018 23:45 Edited at: 19th Jun 2018 09:01
Nice! It's different from my method, but like they always say, there's more than one way for an alligator to eat someone's mother-in-law.

One caveat though.... on occasion, for whatever reason, my timer has read in a negative value!
Therefore I always use abs(timer()) to get a positive value. Just a little tip from your friend Derek!

(edited: forgot an ABS..LoL)
Send your parents to noisy sprite demo hell... enter the D-Zone
Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Jun 2018 05:18 Edited at: 19th Jun 2018 05:19
Quote: "One caveat though.... on occasion, for whatever reason, my timer has read in a negative value!
Therefore I always use abs(timer()) to get a positive value. Just a little tip from your friend Derek! "


It's due to using a signed integer and when your computer has run for a long time without a restart, the value wraps around from it's highest positive value to it's lowest negative value and continues to count upwards.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 19th Jun 2018 09:13
Quote: "It's due to using a signed integer and when your computer has run for a long time without a restart, the value wraps around from it's highest positive value to it's lowest negative value and continues to count upwards."


Ah. Well that explains it.
I was very a paranoid about the timer value!
Send your parents to noisy sprite demo hell... enter the D-Zone
Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 20th Jun 2018 04:11
Come to think of it, abs could cause some wonky behavior.

Mark = abs(timer()) //ex: Mark = abs(-100) = 100

// later...

If abs(timer()) - Mark > 50 then doStuff()

//ex: if abs(-20) - Mark > 50 = If 20 - 100 > 50 = doStuff isn't going to get called for a very long time.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Mage
12
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 20th Jun 2018 04:16 Edited at: 20th Jun 2018 04:19
Mark = timer()
If abs(timer() - Mark) > 50 then doStuff()
Derek Darkly
7
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 21st Jun 2018 02:25 Edited at: 30th Jun 2018 15:43

EDIT: Sorry, ramblings of a madman here. Disregard.
Send your parents to noisy sprite demo hell... enter the D-Zone
Starshyne Emir
2
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 13th Dec 2018 12:58
My timer has a flaw, indeed. If the machine is turned on for too long, it can crash.
You can solve that with a simple workaround:


Then, instead of using TIMER() to check how much time has passed, you just need to adjust your code to rely on fc variable to do the same thing. Since it is resetted everytime your game loop starts, it is impossible for it to crash due long game sessions - but if you need something larger for larger time spans, set FC as a double integer and you can run it for a lifetime without crashing.
God is real - except if declared as integer.
Mage
12
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 14th Dec 2018 18:52
Would only work if your frame rate was capped and you didn't dip below the cap. In which case the timer code was pointless to begin with.
What I posted avoids the timer "roll over" problem you are concerned with.

Mage's Modular Code - Get awesome UI controls and powerful Bitmap Fonts!
Screen Device Recovery - Stop your apps from crashing when minimized/tabbed/screen locked.
CPU Friendly Frame Limiter - Make your app generate less computer heat + noise, and use less battery life!

Login to post a reply

Server time is: 2019-03-19 16:41:53
Your offset time is: 2019-03-19 16:41:53