While loop Drivers and additive movement Im working on a little project that uses drivers... I have used them extensively but Im failing to find a solution for this problem. In this project I wont use scripts. Also, this is not on BGE, but on the regular viewport.

Im trying to make a driver expression on an empty0 so that it's X location is added by 1 every time some other event happens. For example, if a cube location goes from 0 to 1, add+1 to empty0 once. But this must work on runtime, 24fps.

So this is the problem: driver expressions are little pieces of code that are run every frame change, therefore every time I do something like:

"add +1 to variable1 if some other variable2==1" 

it doesn't do it only once, it will check 24 times a second if the variable2==1 and will add +1 to variable1 24 times in a second. I wanted it to only add +1 one single time every time the cube goes to x==1. If I move the cube back to zero, nothing should happen, If I move the cube back to 1 again, it should add+1 to empty0 xloc one more time only, until I repeat the process

If drivers had a "While loop" to check "while variable2==1" before making the addition to variable1, it would solve the problem. But I don't think there is, and I cannot think of another way of simulating a "while" using other drivers and tricks.

The core of the problem is the fact that a driver is being run 24 times a second and I wanted to add a simple +1 to x location of an object just once every time a cube location =1.

Maybe someone here has an idea of how to solve it...

Heres a blend file as example of the problem:
Add a property to keep track of the drivers previous value, after adding the location driver.
The update order of the drivers will have exactly one update/frame, where the new location of the driver isn't copied to the custom property.


(I will add more details later, if needed.)


  Minor point
  The self.location[0] with itself as a driver is not very elegant. Use a shape key if you want to have a visual indicator of 0 and 1 as in the example file.
  Also use the Transformation Constraints instead of the channel locks, since the channel locs won't stop changing of the number via the transform (N) panel.




Changing the property every full second

You can check for full seconds with frame%24 == 0.

Expression: var if frame%24 != 0 else var+1 if var1==1 else var
with var = empty.y and var1 = cube.x as in the original demo file.

Unraveled, this is:

# do nothing (value = var) for 23 of 24 frames
if (frame%24 != 0):
    return var
else:
    # on every 24th frame (frame%24 == 1) check for the condition
    if (var1 == 1):
        return var + 1
    else:
        return var