4
$\begingroup$

This is from an answer I received here: timer

I suggest to avoid this method to measure time. The BGE will try to keep the given frame rate as much as possible, but there is no guaranty.

I recommend to use a timer property. It counts the seconds. I think that is what you really want.

Then you have several ways to show the time. I think you want to format it to minutes:seconds. You can use this little script timeToText:

 import bge 
 from datetime import datetime

 textObject = bge.logic.getCurrentController().owner 
 time = datetime.fromtimestamp(textObject["time"]) 
 textObject.text = '{:%M:%S}'.format(time)

It expects a property time to provide the seconds and should be applied to an text object.

image

You can indeed refresh the display each 60 frames. But you will not know when the seconds changed. Therefore I suggest to run it constantly.

Hint: If you want to show microseconds too, use this format: {:%M:%S.%f}

This works. But the timer in bge counts upwards. What if I want it to count let's say from 30 minutes downwards?

How can I "invert" the timer? I guess I can assign the original value 30 to the property time but how do I achieve the countdown logic afterwards?

$\endgroup$
3
$\begingroup$

You could simply negate the time property (-1800 seconds) and parameter of datetime.fromtimestamp(-textObject["time"]):

enter image description here

import bge 
from datetime import datetime

textObject = bge.logic.getCurrentController().owner
if textObject["time"] < 0:
    time = datetime.fromtimestamp(-textObject["time"])
    textObject.text = '{:%M:%S}'.format(time)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.