Python value: "toggle"

Hey all!

I would like to know the best way to change a value True->False->True->False, ect, using the same button to activate it. I tried a series of “if / else” statements, but the value jumps back and forth several times, depending on the button press length.

I am trying to ‘zoom in’ and ‘zoom out’ with the middle mouse button; changing the mouse sensitivity while zoomed in. I would post a .blend, but the file would be too unorganized and confusing.

Thank you for any help! ~~ Stu_Flowers

value = not value?

Have the zoom animation carried out by an IPO property actuator (afaik, there is no easier way to determine the current state of object animation, and that’s what you want to determine what the mouse sensitivity value will be, because mouseclicks sometimes “go over” IPO actions, and the whole thing ends up out of sequence):


# Assuming your animation is 20 frames in lenght
# All properties are of type int, and are initially set to: zstate = 1, zoom = 0, frame = 1

# Make sure states don't keep changing when you hold down MMB
if middlemouse.isPositive() and own.zstate == 1:
    own.zoom += 1
    own.zstate = 0
    
if not middlemouse.isPositive():
    own.zstate = 1

# When MMB is clicked again it will go back to zero, instead of two.
if own.zoom > 1:
    own.zoom = 0

# Frame handler
if own.zoom == 1:
    if own.frame < 20:
        own.frame += 1
else:
    if own.frame > 1:
        own.frame -= 1

# Mouse sensitivity switch
if own.frame > 10:
    # Change mouse sensitivity
else:
    # Change it back to original

#Activate your zoom actuator/s:
GameLogic.addActiveActuator(zoom, 1)

Note: I just typed this up, so I didn’t test it, but it should work fine. Also I assumed you know how to do the whole cont and own thing and therefore set your own sensor + actuator assigments.

If there is anything you don’t understand, ask away.

Nice!! Thanks a bunch, Social! It works like a charm…or…champ…whatever!

Oh, the wonderful things you can do with python! The prescision and accuracy! The options!!

------eh hem------please pardon my over-exuberant joy.

You can also create a simple bool toggle switch without Python. In the Assign Property actuator, simply type !xxx (xxx being the name of your property) in the box where you would normally type the value to be assigned.

Yea, python is usefull. Glad I could help.

I know about the “Bool Toggle” method. In this case the implementation of that method would look like this:

http://i33.photobucket.com/albums/d55/0social0/BoolToggle.png

Personally, I prefer writing a script rather than stacking bricks. It’s much easier to find and change things like values, actions, etc.

Scripting just makes things more manageable for me, more options and precision, bricks can clutter fast, and I don’t like that.

Of course, that’s just my preference.

hhmm, I tried logic bricks before, but not with that extra skip property. The script works better in my case though, because I just placed it into my pre-existing mouse script.

Thanks guys.