Setting and resetting a Timer using Python scripting

Hi there!

I’m very new to the BlenderArtists.org forums, and this is my first post. Before diving into byte arrays and if statements, I would like to ask: is there a forum for newcomers? You know, the one that nobody reads, “Introduce yourself” or something?

So, I’m quite new to Python programming too, though I’ve learned the basics of computer programming 3 years ago. I’m currently prototyping in the Blender Game Engine, and, for this prototype, I would like to set up a timer that resets itself every x seconds.
It’s a music-related thing so I would like the it to be quite fast and accurate because I want some music notes to be triggered on a precise timing each time. It should be simple, yet my code doesn’t do the job.

I made a timer property called “baseTempo” that I want to reset to 0 each TempoOffset seconds. So I’ve written this:

import bge

controller = bge.logic.getCurrentController()
owner = controller.owner

print(owner, "timer property is", owner["baseTempo"], "right now!")

#^This is okay.

StartTime = owner["baseTempo"]
print("Start time is", StartTime)

#^This is okay.


TempoOffset = 5.0
print("Tempo offset is ", TempoOffset)

#^This is okay too


if (StartTime + TempoOffset) <= owner["baseTempo"]:
    StartTime = owner["baseTempo"]
    print(StartTime, "should be", TempoOffset, "times larger than the original start time")
    print("yay")

#^This is never triggered

CONSOLE OUTPUT:
Blender Game Engine Started
Grid timer property is 0.01666666753590107 right now!
Start time is 0.01666666753590107
Tempo offset is  5.0
Blender Game Engine Finished

I tried to play with the if statement to see where I was wrong: I replaced the “<=” by “>”, and it worked, though it was clearly not what I wanted. I tried a “while” or an “if not” statement but I did not work.
Maybe all this is just me not understanding that the statement is checked one time only, even when connected to an Always Sensor.

What have I done wrong?

Thanks!

I have already made the suggestion of a Newbie Introduction Forum so do not feel alone.

Hopefully the staff is still considering this request.

Welcome!

I need to review your post more thoroughly to see if I can provide a helpful answer.

By that time someone may already have answered it.

But if not be patient and you will find many helpful Blender users, I am confident of that.

EDIT:

It is a simple enough code to work on. I will be away from my computer this morning so if no one gets the time to figure put the answer before me or you do not figure it out yourself than rest assured tonight I will find an answer.

Looks to me like you have your logic wrong.

You set starttime to basetempo, then add offset to it and check whether its less than basetempo… it wont be unless offset is < 0. This is also why > works.

Thanks for the replies!

But baseTempo is a timer property, so it constantly changes, doesn’t it? Its value is even displayed when I start the Game Engine. Or maybe

owner["baseTempo"]

is not the right way to access the ever-changing value of a timer property, is it?

Thanks for the replies!

But baseTempo is a timer property, so it constantly changes, doesn’t it? Its value is even displayed when I start the Game Engine. Or maybe

owner["baseTempo"]

is not the right way to access the ever-changing value of a timer property, is it?

Hi!

Okay, so, basically, I found another way to do it, though I still don’t know why the boolean condition of the “if” statement cannot be True.
Indeed, the problem comes from the line

if (StartTime + TempoOffset) &lt;= owner["baseTempo"]:

It seems that we cannot compare timer properties and it does not show any error statement. Maybe this is a bug. But I’m way too unskilled to say if it is or not.

Should we report this as a bug?

I wouldn’t report it as a bug.

Not that familiar with the game engine, BUT… i still think your code says this

A = B
C = 5

if A + C <= B

or B + C <= B

or C <= 0

which is only true if C is < 0… no matter what B (the timer) is.

it seems to me you just want something like


if owner["bt"] &gt; TempOffset:
    owner["bt"] = 0


Well, indeed, I just throw my raw code here but, basically, you’re right, what I want to do is:

if owner["baseTempo"] &gt;= WhateverFloat:
    owner["baseTempo"] = 0.0

(with WhateverFloat being TempoOffset in my case)
And it’s not working. I tested it for the 50th time just now, and it’s still not effective…
…However, if I had set up the BGE Always sensor in Pulse mode sooner, it would have worked! So, indeed, this was just me not understanding how the Always sensor works. I thought it was always in Pulse mode due to its nature, and therefore would not only read the script once, at the very beginning of the run time, and I was shamefully wrong!

I’m sorry about all this, and thanks for your patience and help!

Someone correct me if I am wrong but I think original the problem here is that StartTime is a pointer to the timer and not a value getter. It will always only be a fraction of the second behind the timer because it keeps pointing back.

If anyone has suggestions how to get the value of the timer I could use that…