OK, if you add a game property and change it to timer it counts up. Is there a way to make it count down. I got it kinda working by adding a always----and----property. Changed property to add and set it to “-.03”. It seems to work ok ,but depending on what the number is set to, determines how fast it counts down. Like if you set to “-.1” you end up counting like 3 or 4 secs for every sec.
set the always true pulse at 60 ticks
if you really need it to count down use 2 properties and a little python to change the sign.
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
own["timer2"] = -own["timer1"]
Python?
this is a joke isn’t it?
A) when you need it as event (e.g. when reaching a certain limit) you can simple let a sensor check if the timer value is above that limit. You can even start with the negative of the limit and check when the timer value is above zero-> Property Sensor [a timer value is float therefore never check for equality!].
B) if you constantly need the timer value (e.g. for display it to the user) you can constantly trigger a Property Actuator that converts the timer value into a value of another property. E.g. Mode: Assign; Property: otherPropertyName; Value: timerPropertyName
C) you constantly want to format the timer value with one or more a complex formula, then use python (e.g. the python string formatting to get HH:MM:SS:sss). The Property Actuator is able to do that too but it gets pretty complex and hard to read. You might even need more than one.
So dependent on why you need the timer there are different solutions that fit differently.
Yeah, definitively. The best solution is clearly to make it all in Python. You can use the time.time() function for that. Even better, do something like this:
import time
def makeTimer(offset = 0):
x = time.time() + offset
return lambda: x - time.time()
mytime = makeTimer()
def loop():
print( mytime() )
Is this overenginering? Am I overdoing it? Definitively, that’s why is fun.
he needed it to count down not up :spin:
why ?? i don’t know.
On the function I posted, if you want to count down you just do:
mytime = makeTime(-10) #Or from whatever number you want to count down.
def loop():
print( -mytime() )
Clearly? Maybe your view is not so clear as you seems to miss things. The best solution is what fits best.
If your concept is so clear, why you are using the BGE? It is not Python, maybe you should use Python only:p.
How am I suppose to use this? I never really used python in BGE, so not sure what I’m suppose to with that bit of code.
To be different, LOL.
That was kind of a joke, don’t take it seriously.
If your concept is so clear, why you are using the BGE? It is not Python, maybe you should use Python only:p.
I do use Python only, not a single logic brick is needed in my addon, hence I don’t use them. You can still use them, but I dislike doing so.
Is as if you’re implying BGE only worth are logic bricks.
How am I suppose to use this? I never really used python in BGE, so not sure what I’m suppose to with that bit of code.
You call the loop() function each frame. To do so, one way would be using a Python controller on module mode (sciptName.loop) and and Always sensor on pulse mode. If you check the console you will see the time in seconds. To actually put that somewhere else, for example in a property you can do object[“PropertyName”] = mytime() inside the loop. To get the object, you can use:
scn = bge.logic.getCurrentScene()
obj = scn.objects["YourObjectName"] #OR
obj = bge.logic.getCurrentController().owner #To get the object that calls this script.
So all together would be:
import time, bge
def makeTimer(offset = 0):
x = time.time() + offset
return lambda: x - time.time()
mytime = makeTimer(-10)
obj = bge.logic.getCurrentController().owner
def loop():
obj["PropertyName"] = -mytime()
I got it working and half way understand that. One problem with what you posted. If you run what you posted, it starts counting from 10 and goes up. This seems to work
import time, bge
scn = bge.logic.getCurrentScene()
obj = bge.logic.getCurrentController().owner
def makeTimer(offset = 0):
x = time.time() + offset
return lambda: x - time.time()
mytime = makeTimer(30)
def loop():
obj["timer"] = mytime()
Seems @edderkop confused me, the original code was already right. The difference is subtle so I did not realize:
Count backwards.
return lambda: x-time.time()
Count normally.
return lambda: time.time()-x
P.S. The lambda, which is probably what you don’t understand, is like making a function inside a function, so it would be equivalent than doing:
def makeTimer():
x = bla bla
def func():
return x-time.time()
return func
It creates a new “func” function each time, each “func” function you create uses a different “x” variable. It’s a good hack to have class-like functionalities without actually using classes.