Python Question

Hi, this is more of a general python question rather than game engine, but anyways…

i have two values: MAX , X , and Y

I want to continuously add (or subtract) Y to X until it reaches MAX

I already tried this:

own[“X”] += Y* math.copysign(1 ,abs(own[“MAX”])- abs(own[“X”]))

but it won’t work if MAX is negative.

Please help me.

Try this:


x = -144
y = 2
max = -100
while x < max:
    x = x + y
    print(str(x))


It doesn’t work, it reaches max instantly, and I want it to reach it gradually.

Thats because your computer is fast… you need to add in a sleep function


import time
x = -144
y = 2
max = -100
while x < max:
    x = x + y
    print(str(x))
    time.sleep(1)

Insert a sleep command . . .


import time
y = 0
x = 44
y = 2
duration = .1
max = 100
while x < max:
    x = x + y
    time.sleep(duration)
    print(str(x))

Just change duration for make it go for whatever time you want it to take. You will not be able to run any other
functions until the while loop has finished, and the sleep command will execute each time the while loop runs, so do not make duration to long.

in game engines, you would usually use a formula that contains a time variable supplied by the engine and return the current location / value…

I tried this:

while own["value"] < own["maxvalue"]:
    own["value"] = own["value"] + 2
    time.sleep(1)

I don’t need to print anything because I’m just using debug properties

Blender freezes.

Never mind, i just figured it out

own["X"] += Y* math.copysign(1, own["MAX"] - own["X"])