create timer property in python?

I can create a property within python just by assigning a value i.e. (own[“propertyname”] = 1). but how do I create a timer property instead of just an integer property?


Hope this helps!

Please refrain from posting multiple threads based around the same subject. Your thread already gets bumped to the top of the queue when someone posts on it and you can ask multiple questions at the same time. I have posted a much more efficient method in your previous thread.

the question is, “how do I create a timer property in python?” I know I can create one in the property panel.

one post is about how to pause/disable a timer property in python. this post is asking how to create a timer property in python. in the other post, you got distracted by what I was going to use the timer property for. you keep posting alternatives to what I am trying to do instead of answering my question of how to pause or disable a property. I had an idea: “instead of trying to pause the timer, I could just delete it and create a new one.” instead of posting in my already created thread on a similar topic, I created a new one. in this one I didn’t explain what I was going to use it for so that people would not get distracted by my methodology and just answer the question.

Ah, I missed that part. My bad.

Hi, you can do this:

There’s a buil-int module in python module named time. It provides ways to get time and date.

We are going to use two functions that time provides: time and monotonic.
Basically they return the time in seconds, the time function give it based on your compute clock, while the monotonic is like the time function, but it’s a clock that cannot be affected by your system clock. Due to it, we will use the monotonic function, because if we use the time one, edit the system clock will make go forward the property time.

Basically to get time time that have passed, we use: currentTime - lastTime.

So we have to do is store the time the first frame (or the time you want to reset the time), and from there, the all next frames what we will do is subtract it to the current time.

Example: first frame the time is = 13300.0, store it, four seconds later the actual time is: 13304.0, so we subtract it with the first time stored, and it give us: 4.0 (seconds). So here you have the passed time.

Example code in bge:

from bge import logic
from time import monotonic
cont = logic.getCurrentController()
own = cont.owner

if "init" not in own:
    own["init"] = True
    own["time"] = monotonic()

own["timePassed"] = monotonic() - own["time"]

Attacht that with an always sensor (triggered), and add two float properties to the object named: “time” and “timePassed”.

Hope it helps.

that is a fantastic explanation, thank you.:slight_smile:

… however, the question is, "how do I create a timer property instead of just an integer property?

there are five kinds of properties: timer, float, integer, string, and boolean. four of them can be created easily by assigning a value to a property like (own[“property”]).

float:
own[“property”] = 0.01

integer:
own[“property”] = 1

string:
own[“property”] = “one”

boolean:
own[“property”] = true

I do not know how to create a “timer” property because the only way I know how to create a property in python is to assign it a value.

how do I create a timer property in python?

The timer property is just a float. Blender knows that it is a timer because you set it in the property panel. The game engine checks all properties for a timer flag* and increments them automatically. But AFAIK you can’t set that flag inside a game.

Properties can be anything. Floats, strings, bytestrings, ints, lists, dictionaries, vectors, even classes.

If you were to subclass the blender KXobject you could set up a property as a timer but… that would be more difficult that wrting your own timer.

*Or it compares the list of properties with a list of timers OR the KXgameobject has a special method for updating timers. IDK which method is used.

now THAT is an answer to my question! thank you so very much! <3<3<3<3<3<3<3<3<3<3<3<3<3<3<3

You can also call time.time() or time.clock() depending on your operating system. Both of these return seconds as a float.

On Windows the precision of time.time() seems to be horrible. Instead of 0.0 at runtime I get somewhere between 35.0 and -30.0…
time.clock() works well for me, but may present problems for Linux users. I believe on Linux time.clock() calls for the processor clock, but I cannot remember with certainty…

There is also time.monotonic.