Making a Timer/Stopwatch

Hello everyone!

I have been working on making a timer in Blender, and I have finally figured out how to get one that keeps track of time using the timer property. However, I have been unable to figure out how to stop the timer in game after a specific event happens, so that you can show how long it took the player to beat the level/game. Does anyone know how to do this? I was thinking that there had to be a stop function of some sort, but if there is, it has thus far eluded me.

Thanks in advance for any help :smiley:

This might help with a simple logic approach. Any questions just ask, but the gist is that a property actuator sets the timer to zero when a sensor triggers it.


CountdownTimer.blend (359 KB)

Or the property actuator with “toggle”
will start and stop the timer.

Hello everyone, thanks for the quick replies!

The goal of my timer was so that the timer would stop and show how much time has passed since it started. It appears as though your timer counts down, where the timer I am looking for needs to count up. I can do the timer part I just don’t know how to make it stop and display the time it took to complete a task.

It appears that the toggle actuator should work, but when I use it it simply resets my time to 0.01 and then begins counting again.

Here is an example of what I want my timer to do: Suppose you were making a racing game and you wanted to display how long the race took after it had finished. You want to make the timer to freeze as soon as the racer passes the finish line so that you can see how long it took. I have the timer set up, but I can’t get it to freeze.

Here is the code I am using to control my timer:

import bge

def main():
    cont = bge.logic.getCurrentController()
    clock = cont.owner

    clock.text = str(clock['time'])

# Script to use to make a timer in Blender                     #
# 'clock' is a text object and 'time' is a timer property      #
# These names can be changed to whatever you want              #
# This script uses an always sensor with true level triggering #
# and a python controller                                      #

I was wondering if you could use the get() function to retrieve the current time display, and then tell the “clock” object to display the value received from the get() function. Does anyone know if this would work, and, if so, how to do it?

Sorry- would this help? It is a basic finish line / race time setup, although the finish line could be made to be triggered by anything.

EDIT: Credit to Equip who made this ( a looooong time ago!)


You’re right, I suppose I worded my question wrong. I don’t necessarily need to stop the timer, but I need to stop the display. I looked at the links you provided, but I didn’t really see anything about stopping display. Perhaps I just missed it?

Also, I have been meaning to change my scripts over to module mode, but I have been working on other aspects of my projects, and as of yet have not had time to change over. :o

I looked at the .blend file you attached, and it appears that it accomplishes what Monster was suggesting, and I think it should work perfectly, but I am unsure how to implement this into my own code. What part of your setup is telling the display to stop?

In the code, simply change the line to an:

if clock["time"] < 30:
    clock.text = str(clock['time'])

The timer does not stop (if you look at the debug menu the timer is always running unless you reset it by pressing space.) What happens is that when the cube collides with the finish line, the finish line has a property that copies the timer into a float. If you tinkered with the code and used sdfgeoff’s suggestion too you will have a way to record the quickest time too (by comparing the new time with the last one and seeing if its quicker).

logic bricks only:

timer type: timer - it counts automatically in seconds
display type: int - acts as display
active type: bool - when true it will measure - when false it will not measure

now you need logic to transfer the measured time to the display:

property sensor name: “start” prop: active mode: equal value: true pulses: None!
–> AND —> Property Actuator name: “reset time” mode: assign prop: timer value: 0.0

property sensor name: “measure” prop: active mode: equal value: true pulses: True!
—> AND —> Property Actuator name “display time” mode: assign prop: display value: timer

formatted type:String - keeps a formatted display

connected to sensor “measure
—> AND —> Property Actuator name “display formatted time” mode: assign prop: formatted value: display/60+":"+display%60

How you get it from the properties to screen is a different topic :wink:

formatted seconds with leading zeroes:

—> AND
-----> Property Actuator name “formatted mins” mode: assign prop: formatted value: display/60+":"
-----> Property Actuator name “formatted secs” mode: add prop: formatted value: (display/10)%6+""+display%10

Hi guys, thanks for all the amazing help so far!

Your explanation of the .blend file you posted makes perfect sense, and I think I want to run my timer like that. It seems to be very simple, but I am having some problems with taking the information from your .blend file and using it in mine. I will post a .blend file with my current setup, since I think that will be more useful than me attempting to explain myself (since half of the time I really don’t know what I’m talking about) :o

I’d really rather get the setup for this timer figured out with python as opposed to logic bricks, because I plan on using this timer very often, and python is much easier to reproduce in another file (all I have to do is append it from a scripts library that I am compiling) As I said above, I really like Rubbernuke’s method, but if you have any other python solutions, I would be glad to hear them, since you definitely know more about these kinds of things than I do. :wink:

Here’s the .blend file --------> timer.blend (386 KB)

Here is a reworked example that is scripted in modules. Each component is as separate as possible- this means however you want to trigger the timer (or finish it) it should be easy to do. Bear in mind this is my first attempt at a module script so it is probably full of redundant stuff…

The controls are A to start the timer, B to stop and SPACE to reset everything.

Also, do you know you can copy logic bricks? By selecting the empty object, then the object with the logic you want, go to OBJECT > GAME > copy logic bricks. So if you want to give Monsters logic a try (its very good!) you can and just copy it whenever you want, with the bonus that it is more efficient than a Python controller.

Anyway, hope this helps a bit.




NewTimer.blend (382 KB)

I looked at your new timer .blend file, and I understood everything you were doing. Using the information in that .blend file, I was able to adjust my own timer file and now it works perfectly! Thank you so much for all the help you have been. This will be a script that I will be using very often.

Also, no I did not know that you could copy logic bricks. That is a very nice piece of information to have, and I think that I will hold on to Monster’s logic brick setup in case I want to use that one at any time.

As I said above, I was unaware that you could copy logic bricks, and now that I know this, your logic brick setup seems to be a very serious alternative to scripting, and I always like to have more than one way to do something, since it gives my the ability to use what I feel works best for a situation. Thank you once again for the very useful input into yet another one of my many questions when it comes to Blender.

Thanks also to sdfgeoff for your help with my question.

Hi DarkFire7,

Don’t worry. This is really just to show the options. Formatting display strings is much easier in Python. I wanted to show measure and display are two separate tasks and you have the choice to choose the methods that fits most in your particular situation.