Page 1 of 1

time based couters

Posted: Thu Apr 21, 2011 9:24 am
by Wulfie
Hello folks.

I have finally sorted out what my first project will be. Now I'm stuck lol. A huge portion of my game will depend on the amount of time that has passed.

An example of what I am trying to do;

Say I want to simulate the discharging of a battery.

Start
BatteryCharge = 100 (static # or percentage, either would work)
**a mix of percentage drain on a static # would be ideal.
After one hour of Real World Time the Battery Charge would = 80 (again static # or percentage would work)

The math basics I get what I'm stuck on is 2 things.

How do I pull current time and build a counter around that ?

How can I make it so that if you close the game all together and come back an hour later and load it, an hour has still passed and the battery is still drained by 20% / 20 points ??

Any assistance with how to code this and or how to get time and count from it both during play and offline time, would be greatly appreciated. As i stated this is a big part of my game idea. The HUD/GUI 2d tile grid etc etc I can handle in lua.

Thank you again for your feedback / assistance

Re: time based couters

Posted: Thu Apr 21, 2011 9:38 am
by nevon
Use os.time to get the current timestamp. Save that timestamp along with your charge and whatever else you need to save, in the love.quit callback. Then when the game is loaded again, the elapsed time is os.time()-oldTimestamp.

Re: time based couters

Posted: Thu Apr 21, 2011 10:34 am
by Wulfie
Awesomesauce thank you so much for the quick and informative response. I appreciate it.

for some reason I could not get time::gettime out of my head.. lol

Re: time based couters

Posted: Thu Apr 21, 2011 10:53 am
by BlackBulletIV
By the sounds of it, a tweening library would help. EmmanuelOga's tweener library might do the trick.

Re: time based couters

Posted: Thu Apr 21, 2011 10:59 am
by nevon
BlackBulletIV wrote:By the sounds of it, a tweening library would help. EmmanuelOga's tweener library might do the trick.
Whut? How do you figure that?

Re: time based couters

Posted: Thu Apr 21, 2011 8:08 pm
by BlackBulletIV
Wulfie wrote:How can I make it so that if you close the game all together and come back an hour later and load it, an hour has still passed and the battery is still drained by 20% / 20 points ??
Argh, I didn't read that.

From what I skimmed I originally thought that Wulfie wanted a timer that wasn't lasting, which a tween can pull off fine (although the practicality of an hour tween I don't know).