Python/BGE help please?

I have been trying to create an FPS recently, and I used some code from tutorialsforblender3d.com. Here’s the code:

def main():

controller = GameLogic.getCurrentController()

obj = controller.owner

servo = controller.actuators["move"]    

check_Prop(obj, servo)

lin_Speed = servo.linV
    
###############
            
lin_Speed = forward_back(obj, controller, lin_Speed)

lin_Speed = left_right(obj, controller, lin_Speed)

lin_Speed = run(obj, controller, lin_Speed) 

lin_Speed = jump(obj, controller, lin_Speed)

lin_Speed = move_Diagonal(obj, controller, lin_Speed)
    
touch_ground = ground(controller)

lin_Speed = servo_settings(obj, servo, touch_ground, lin_Speed)
    
###############

move(obj, controller, servo, lin_Speed)

stop(obj, controller, servo, touch_ground, lin_Speed)

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

def check_Prop(obj, servo):

if obj.has_key("walking") == False:            
    
    obj["walking"] = 10.0                        

if obj.has_key("running") == False:            
    
    obj["running"] = 1.5 * obj["walking"]        

if obj.has_key("jumping") == False:
    
    obj["jumping"] = 10.0
    
if obj.has_key("jump_once") == False:
    
    obj["jump_once"] = 1

if obj.has_key("servo_status") == False:
    
    obj["servo_status"] = "Off"

if obj.has_key("touch_ground") == False:
    
    obj["touch_ground"] = 0

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

def forward_back(obj, controller, lin_Speed):

forward = controller.sensors["forward"]
back = controller.sensors["back"]

linX, linY, linZ = lin_Speed        
            
######### ground -- forward/back 
            
if forward.positive == True and back.positive == False:    
    
    linY = -obj["walking"]
                    
elif back.positive == True and forward.positive == False:
    
    linY = obj["walking"]

elif back.positive == False and forward.positive == False:
    
    linY = 0.0

elif back.positive == True and forward.positive == True:
    
    linY = 0.0


lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def left_right(obj, controller, lin_Speed):

left = controller.sensors["left"]
right = controller.sensors["right"]

linX, linY, linZ = lin_Speed
        

###########  ground -- left/right

if left.positive == True and right.positive == False:

    linX = obj["walking"]
            
elif right.positive == True and left.positive == False:
    
    linX = -obj["walking"]

elif left.positive == False and right.positive == False:
    
    linX = 0.0

elif left.positive == True and right.positive == True:

    linX = 0.0
            
############ save linear velocity
lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def run(obj, controller, lin_Speed):

linX, linY, linZ = lin_Speed

runSen = controller.sensors["run"]

if runSen.positive == True:
    
    linX_Run = obj["running"] * ( linX / obj["walking"] )
    
    linY_Run = obj["running"] * ( linY / obj["walking"] )
    
    linX = linX_Run
    linY = linY_Run
    
############ save linear velocity
lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def jump(obj, controller, lin_Speed):

jump = controller.sensors["jump"]

linX, linY, linZ = lin_Speed

############# ground -- jumping

if jump.positive == True:
    
    if obj["jump_once"] == 1:
                
        obj["jump_once"] = 0
        
        linZ = obj["jumping"]
        
elif jump.positive == False:
    
    obj["jump_once"] = 1
    
    linZ = 0.0
        
############ save linear velocity
lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def move_Diagonal(obj, controller, lin_Speed):

linX, linY, linZ = lin_Speed

###########  ground -- diagonal

if linX != 0.0 and linY != 0.0:
    
    linX = 0.707 * linX
    linY = 0.707 * linY
            
############ save linear velocity
lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def ground(controller):

ground = controller.sensors["ground"]
    
if ground.positive == True:
    
    touch_ground = 1

elif ground.positive == False:
    
    touch_ground = 0


return touch_ground

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

def servo_settings(obj, servo, touch_ground, lin_Speed):

linX, linY, linZ = lin_Speed

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

if touch_ground == 1:
                
    if linX == 0.0 and linY == 0.0 and linZ == 0.0:

        servo.forceLimitX = [0.0, 0.0, False]        
        servo.forceLimitY = [0.0, 0.0, False]
        servo.forceLimitZ = [0.0, 0.0, False]
        
    elif (linX != 0.0 or linY != 0.0) and linZ == 0.0:
    
        servo.forceLimitX = [-1000.0, 1000.0, True]    ####  for left/right movement    
        servo.forceLimitY = [-1000.0, 1000.0, True]    ####  for forward/back movement
        servo.forceLimitZ = [0.0, 0.0, True]        ####  for gravity        
    
    elif linZ != 0.0:
        
        servo.forceLimitX = [0.0, 0.0, True]            ####  sideways momentum        
        servo.forceLimitY = [0.0, 0.0, True]            ####  forward/back momentum
        servo.forceLimitZ = [-1000.0, 10000.0, True]       ####  jump force        

        
########## not touching the ground

elif touch_ground == 0:

linX = 0.0
linY = 0.0
linZ = 0.0

    servo.forceLimitX = [0.0, 0.0, True]    ####  sideways momentum        
    servo.forceLimitY = [0.0, 0.0, True]    ####  forward/back momentum
    servo.forceLimitZ = [0.0, 0.0, True]    ####  gravity and jump momentum
                
############ save linear velocity
lin_Speed = [linX, linY, linZ]

return lin_Speed

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

def move(obj, controller, servo, lin_Speed):

linX, linY, linZ = lin_Speed

if (linX != 0.0 or linY != 0.0 or linZ != 0.0) and obj["servo_status"] != "On":

    controller.activate(servo)
    
    obj["servo_status"] = "On"                            

servo.linV = lin_Speed    

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

def stop(obj, controller, servo, touch_ground, lin_Speed):

linX, linY, linZ = lin_Speed

if linX == 0.0 and linY == 0.0 and linZ == 0.0:        

    stop_Delay = obj.sensors["stop"]
    
    if touch_ground == 1 and obj["servo_status"] == "On": 
                                                
        stop_Delay.reset()
        
        obj["servo_status"] = "Off"
            
    elif obj["servo_status"] == "Off":
                                                
        if stop_Delay.positive == False:
            
            controller.deactivate(servo)                            

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

main()

The code is just fine except for one major difficulty: the servo control is running as a global transformation, not a local. I’m not great with Python so try to make it simple

My other issue is with bounds. When I try to add box/convex hull bounds to my collision box the game slows down immensely. All items inside of the box are parented to it and are set as ghosts. When I turn off bounds, the game works fine. When I turn them on though I get about 1.2 frames per second.

Help?

Before I read anything of the script, please edit your first post and surround it with code tags.

You find it in “Go Advanced”

  • remove the current script text,
  • cut&paste it again,
  • select the new script text
  • click #

You will see it is surounded by [ CODE ] [ /CODE] tags and is correctly formatted when posting.

By the way, a .blend would be much better as this script requires to know the context of the script.

Set the things inside the bounds as “no colision” instead of “ghost” it is better.

Maybe you need this:
use local linear V

it seems like a complex code to start out with. You really should try to use code which you can understand at first… so experiment with controlling servos with python first, then when you know what is happening in the main script.

And yes, please use code tags, it preserves indentation.