blendenzo.com
News Tutorials Games Downloads Links Contact

Featured Author:
blendenzo!!
-blendenzo-

Tutorials - Blender Game Engine

Making an On-screen Timer

by blendenzo

After successfully sneaking into the enemy base and planting bombs in strategic locations, your special forces unit has mere minutes to make it to safety. You can feel the tension building as you activate the countdown and turn to make your final exit. Adrenaline pumping through your veins like blood, you look to your wristwatch, hoping for more time than you know you have...

So you made a cool game. But as the player is blown to pieces by his own bombs he says to himself, "What a dumb game. They don't even have a countdown or anything. How am I supposed to know how much time I have left?" Well, can you blame him? Halo has a timer. Why don't you? Especially when making a timer is so easy. Oh, you don't think so? Well then fire up your Blender. We've got work to do.

1. The Setup

Now that you've got Blender started, delete the default cube and switch to camera view [0]. Add a plane in front of the camera and set it up as a text object. (If you don't know how to do this, read ST150's tutorial on this subject, or quickly import one of my premade text objects using the Blender Library script.) In addition to the "Text" property (in the Logic buttons [F4]), give the text object a property named "timer" of the variable type Timer. Now add an "Always" sensor and a "Python" controller and connect the two.



2. The Code

Bring up the Blender Text Editor and press [Alt+N] to start a new file. Enter the following code:

OutputTime.py
own = GameLogic.getCurrentController().getOwner()

minutes = int(own.timer/60)
seconds = int(own.timer%60)

own.Text = str(minutes) + ":" + str("%02i"% seconds)

The only thing in the code that should seem confusing is the "%02i"% modifier. It should be read "modify the following Int variable to 2 places with a leading zero for numbers less than ten."

Go back to the logic buttons and enter the name of your Python script in the "Script" field of the Python controller. Press [P] and you should now have a working timer.

You can either use your timer as an overlay scene or as part of the game scene. If you use it in the game scene, make sure it is very close to the camera so that it doesn't get obscured by other objects. Perhaps you want the timer to begin only after a certain event has occured. Place your timer object in a separate, hidden layer and add an instance of it using the "Edit Object --> Add Object" actuator when that event occurs.

As you can see, the timer is versatile, yet simple to implement.

3. Alternate Formats

You may have some sort of special need for your timer to display tenths of a second. In that case, use the following script:

OutputTime.py
own = GameLogic.getCurrentController().getOwner()

minutes = int(own.timer/60)
seconds = int(own.timer%60)
tenths = ((own.timer%60) - int(own.timer%60)) * 10

own.Text = str(minutes) + ":" + str("%02i"% seconds) + "." + str(int(tenths))

(Your browser may break up the second line of code, but you should keep it all on one line in Blender to avoid Python indentation errors.)

Change the "* 10" to "* 100" for hundredths, "* 1000" for thousandths, and so on.

What if you want the timer to be right-aligned? Just use my right-aligned text tutorial, and run the text through the reverseText.py script. (Combine the two scripts into one. I've done it before. It works well.)

Counting down is easy as well. Just pick a maximum time value and subtract from it. I'll use 300 (five minutes) for the example:

OutputTime.py
own = GameLogic.getCurrentController().getOwner()

timer = 300 - own.timer
minutes = int(timer/60)
seconds = int(timer%60)

own.Text = str(minutes) + ":" + str("%02i"% seconds)

I'm sure there are many other things you could do with your timer, but I hope this tutorial has helped you along the way. Feel free to email me with any questions (use the contact button in the menu).

-blendenzo

...you look to your wristwatch, hoping you have more time than you know you have. Five seconds! You frantically sprint through the final door, the force of the explosion knocking you to your face... Alive! You imagine what could have been if it weren't for that watch...


Back to the tutorials index...


Website design by Tony "blendenzo" DiRienzo. All content © Copyright Tony DiRienzo unless otherwise noted.