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
1
Years of Service
User Offline
Joined: 27th Nov 2016
Location: The Darkness Beyond Time
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
6
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
10
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
6
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
10
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
11
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
6
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

Login to post a reply

Server time is: 2018-09-21 14:22:22
Your offset time is: 2018-09-21 14:22:22