Controllers Questions

Hi I am trying to get my mouse look working. It currently does but at the wrong time. I want it to complete after a certain time. So I would like to have a controller that is an AND controller to detect mouse movement and if my instructions property is = 16 then do the movement. But I can’t seem to achieve this. I can’t find any controller to do AND and python.

Use states, or just use that instructions variable in your python setup to do like:
if obj[‘instructions’] == 16:
Go on with the mouse look code

well this is the python so I don’t know where I would put that code line

######################################################
#
#   MouseLook.py        Blender 2.49
#
#   Tutorial for using MouseLook.py can be found at
#
#    www.tutorialsforblender3d.com
#
#   Released under the Creative Commons Attribution 3.0 Unported License.    
#
#   If you use this code, please include this information header.
#
######################################################

# define main program
def main():
    
    # set default values
    Sensitivity =  0.0005
    Invert = 1
    Capped = False
    
    # import Rasterizer
    import Rasterizer
    
    # get controller
    controller = GameLogic.getCurrentController()
    
    # get the object this script is attached to
    obj = controller.owner
    
    # get the size of the game screen
    gameScreen = gameWindow(Rasterizer)
    
    # get mouse movement
    move = mouseMove(gameScreen, controller, obj)
    
    # change mouse sensitivity?
    sensitivity =  mouseSen(Sensitivity, obj)
    
    # invert mouse pitch?
    invert = mousePitch(Invert, obj)
    
    # upDown mouse capped?
    capped = mouseCap(Capped, move, invert, obj)
    
    # use mouse look
    useMouseLook(controller, capped, move, invert, sensitivity)
        
    # Center mouse in game window
    centerCursor(controller, gameScreen, Rasterizer)
    
#####################################################

# define game window
def gameWindow(Rasterizer):
    
    # get width and height of game window
    width = Rasterizer.getWindowWidth()
    height = Rasterizer.getWindowHeight()
    
    return (width, height)

#######################################################

# define mouse movement function
def mouseMove(gameScreen, controller, obj):

    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]

    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]

    # distance moved from screen center
    x = width/2 - mouse.position[0]
    y = height/2 - mouse.position[1]
    
    # initialize mouse so it doesn't jerk first time
    if obj.has_key('mouseInit') == False:
        obj['mouseInit'] = True
        x = 0
        y = 0
    
    #########    stops drifting on mac osx
       
    # if sensor is deactivated don't move
    if not mouse.positive:
        x = 0
        y = 0
    
    #########  -- mac fix contributed by Pelle Johnsen
                    
    # return mouse movement
    return (x, y)


######################################################

# define Mouse Sensitivity
def mouseSen(sensitivity, obj):
    
    # check so see if property named Adjust was added
    if obj.has_key('Adjust') == True:
        
        # Don't want Negative values
        if obj['Adjust'] < 0.0:
            obj['Adjust'] = 0.0
        
        # adjust the sensitivity
        sensitivity = obj['Adjust'] * sensitivity

    # return sensitivity
    return sensitivity

#########################################################

# define Invert mouse pitch
def mousePitch(invert, obj):
    
    # check to see if property named Invert was added    
    if obj.has_key('Invert') == True:
            
        # pitch to be inverted?
        if obj['Invert'] == True:
            invert = -1
        else:
            invert = 1
            
    # return mouse pitch
    return invert

#####################################################

# define Cap vertical mouselook
def mouseCap(capped, move, invert, obj):
    
    # check to see if property named Cap was added
    if obj.has_key('Cap') == True:            
    
        # import mathutils
        import Mathutils
        
        # limit cap to 0 - 180 degrees
        if obj['Cap'] > 180:
            obj['Cap'] = 180
        if obj['Cap'] < 0:
            obj['Cap'] = 0
        
        # get the orientation of the camera to world axis
        camOrient = obj.orientation
        
        # get camera Z axis vector
        camZ = [camOrient[0][2], camOrient[1][2], camOrient[2][2]]
        
        # create camera z axis vector 
        vec1 = Mathutils.Vector(camZ)
        
        # get camera parent
        camParent = obj.parent
        
        # get parent orientation to world axis
        parentOrient = camParent.orientation
        
        # get parent z axis vector
        parentZ = [parentOrient[0][2], parentOrient[1][2], parentOrient[2][2]]
        
        # create parent z axis vector
        vec2 = Mathutils.Vector(parentZ)
        
        # find angle between two
        angle = Mathutils.AngleBetweenVecs(vec1, vec2)
            
        # get amount to limit mouselook
        capAngle = obj['Cap']
                
        # get mouse up down movement
        moveY = move[1] * invert
            
        # check capped angle against against camera z-axis and mouse y movement
        if (angle > (90 + capAngle/2) and moveY > 0)   or (angle < (90 - capAngle/2) and moveY < 0)  == True:
            
            # no movement
            capped = True
    
    # return capped
    return capped

###############################################

# define useMouseLook
def useMouseLook(controller, capped, move, invert, sensitivity):
                
    # get up/down movement
    if capped == True:
        upDown = 0
    else:
        upDown = move[1] * sensitivity * invert 
        
    # get left/right movement
    leftRight = move[0] * sensitivity * invert 
        
    # Get the actuators
    act_LeftRight = controller.actuators["LeftRight"]
    act_UpDown = controller.actuators["UpDown"]  
    
    # set the values
    act_LeftRight.dRot = [ 0.0, 0.0, leftRight]
    act_LeftRight.useLocalDRot = False  
    
    act_UpDown.dRot = [ upDown, 0.0, 0.0]
    act_UpDown.useLocalDRot = True
    
    # Use the actuators 
    controller.activate(act_LeftRight)
    controller.activate(act_UpDown) 
    
#############################################

# define center mouse cursor
def centerCursor(controller, gameScreen, Rasterizer):
    
    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]
    
    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]
    
    # get cursor position
    pos = mouse.position
        
    # if cursor needs to be centered
    if pos != [ width/2, height/2]:
        
        # Center mouse in game window
        Rasterizer.setMousePosition(width/2, height/2)
        
    # already centered.  Turn off actuators
    else:
        # Get the actuators
        act_LeftRight = controller.actuators["LeftRight"]
        act_UpDown = controller.actuators["UpDown"]  
        
        # turn off the actuators 
        controller.deactivate(act_LeftRight)
        controller.deactivate(act_UpDown) 

##############################################

# Run program
main()

would i put it like before everything?

hi ok so I tried both of these codes and both didn’t work. Do I need () insted of {}?


if obj['instructions'] == 16 {

# define main program
def main():
    
    # set default values
    Sensitivity =  0.0005
    Invert = 1
    Capped = False
    
    # import Rasterizer
    import Rasterizer
    
    # get controller
    controller = GameLogic.getCurrentController()
    
    # get the object this script is attached to
    obj = controller.owner
    
    # get the size of the game screen
    gameScreen = gameWindow(Rasterizer)
    
    # get mouse movement
    move = mouseMove(gameScreen, controller, obj)
    
    # change mouse sensitivity?
    sensitivity =  mouseSen(Sensitivity, obj)
    
    # invert mouse pitch?
    invert = mousePitch(Invert, obj)
    
    # upDown mouse capped?
    capped = mouseCap(Capped, move, invert, obj)
    
    # use mouse look
    useMouseLook(controller, capped, move, invert, sensitivity)
        
    # Center mouse in game window
    centerCursor(controller, gameScreen, Rasterizer)
    
#####################################################

# define game window
def gameWindow(Rasterizer):
    
    # get width and height of game window
    width = Rasterizer.getWindowWidth()
    height = Rasterizer.getWindowHeight()
    
    return (width, height)

#######################################################

# define mouse movement function
def mouseMove(gameScreen, controller, obj):

    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]

    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]

    # distance moved from screen center
    x = width/2 - mouse.position[0]
    y = height/2 - mouse.position[1]
    
    # initialize mouse so it doesn't jerk first time
    if obj.has_key('mouseInit') == False:
        obj['mouseInit'] = True
        x = 0
        y = 0
    
    #########    stops drifting on mac osx
       
    # if sensor is deactivated don't move
    if not mouse.positive:
        x = 0
        y = 0
    
    #########  -- mac fix contributed by Pelle Johnsen
                    
    # return mouse movement
    return (x, y)


######################################################

# define Mouse Sensitivity
def mouseSen(sensitivity, obj):
    
    # check so see if property named Adjust was added
    if obj.has_key('Adjust') == True:
        
        # Don't want Negative values
        if obj['Adjust'] < 0.0:
            obj['Adjust'] = 0.0
        
        # adjust the sensitivity
        sensitivity = obj['Adjust'] * sensitivity

    # return sensitivity
    return sensitivity

#########################################################

# define Invert mouse pitch
def mousePitch(invert, obj):
    
    # check to see if property named Invert was added    
    if obj.has_key('Invert') == True:
            
        # pitch to be inverted?
        if obj['Invert'] == True:
            invert = -1
        else:
            invert = 1
            
    # return mouse pitch
    return invert

#####################################################

# define Cap vertical mouselook
def mouseCap(capped, move, invert, obj):
    
    # check to see if property named Cap was added
    if obj.has_key('Cap') == True:            
    
        # import mathutils
        import Mathutils
        
        # limit cap to 0 - 180 degrees
        if obj['Cap'] > 180:
            obj['Cap'] = 180
        if obj['Cap'] < 0:
            obj['Cap'] = 0
        
        # get the orientation of the camera to world axis
        camOrient = obj.orientation
        
        # get camera Z axis vector
        camZ = [camOrient[0][2], camOrient[1][2], camOrient[2][2]]
        
        # create camera z axis vector 
        vec1 = Mathutils.Vector(camZ)
        
        # get camera parent
        camParent = obj.parent
        
        # get parent orientation to world axis
        parentOrient = camParent.orientation
        
        # get parent z axis vector
        parentZ = [parentOrient[0][2], parentOrient[1][2], parentOrient[2][2]]
        
        # create parent z axis vector
        vec2 = Mathutils.Vector(parentZ)
        
        # find angle between two
        angle = Mathutils.AngleBetweenVecs(vec1, vec2)
            
        # get amount to limit mouselook
        capAngle = obj['Cap']
                
        # get mouse up down movement
        moveY = move[1] * invert
            
        # check capped angle against against camera z-axis and mouse y movement
        if (angle > (90 + capAngle/2) and moveY > 0)   or (angle < (90 - capAngle/2) and moveY < 0)  == True:
            
            # no movement
            capped = True
    
    # return capped
    return capped

###############################################

# define useMouseLook
def useMouseLook(controller, capped, move, invert, sensitivity):
                
    # get up/down movement
    if capped == True:
        upDown = 0
    else:
        upDown = move[1] * sensitivity * invert 
        
    # get left/right movement
    leftRight = move[0] * sensitivity * invert 
        
    # Get the actuators
    act_LeftRight = controller.actuators["LeftRight"]
    act_UpDown = controller.actuators["UpDown"]  
    
    # set the values
    act_LeftRight.dRot = [ 0.0, 0.0, leftRight]
    act_LeftRight.useLocalDRot = False  
    
    act_UpDown.dRot = [ upDown, 0.0, 0.0]
    act_UpDown.useLocalDRot = True
    
    # Use the actuators 
    controller.activate(act_LeftRight)
    controller.activate(act_UpDown) 
    
#############################################

# define center mouse cursor
def centerCursor(controller, gameScreen, Rasterizer):
    
    # extract width and height from gameScreen
    width = gameScreen[0]
    height = gameScreen[1]
    
    # Get sensor named MouseLook
    mouse = controller.sensors["MouseLook"]
    
    # get cursor position
    pos = mouse.position
        
    # if cursor needs to be centered
    if pos != [ width/2, height/2]:
        
        # Center mouse in game window
        Rasterizer.setMousePosition(width/2, height/2)
        
    # already centered.  Turn off actuators
    else:
        # Get the actuators
        act_LeftRight = controller.actuators["LeftRight"]
        act_UpDown = controller.actuators["UpDown"]  
        
        # turn off the actuators 
        controller.deactivate(act_LeftRight)
        controller.deactivate(act_UpDown) 

##############################################

# Run program
main()

}

I also tried


if Player['instructions'] == 16 {

# define main program
def main():

}

As Joeman16 said: Use states. There is no need to add confusing logic to the mouselook script.
Keep functions separated as much as you can. This increases readability and reusability. This even allows to replace such functions with different methods.

Here is a way with states:

Place the logic that changes your “instruction property” in one state (call it instruction state). If it is “16”, switch to another state (call it mouselook state) with the state actuator.

The mouse look state runs the mouselook logic.

I hope it helps

That is how it is currently set up. No mouse look until instructions is equal to 16. But for some reason it ignores that. Maybe there is a if not equal function?

sure the property sensor has an “not equal” mode