joystick for RPG 2.49b

hey i came up with a way that might be easier… i think :rolleyes:… i just define the input as they are to be imported under “game logic”… then made a different script to manage its usage… feel free to use as you please… and let me know what you thing…

Joystick


def VersionCheck():
    try:
        import Mathutils
        VERSION = 2.49
    except:
        import mathutils
        VERSION = 2.5
    return VERSION


VERSION = VersionCheck()
    
if VERSION == 2.49:
    GameLogic.VERSION = VersionCheck()
    import GameLogic as g
elif VERSION == 2.5:
    import bge
    g = bge.logic

cont = g.getCurrentController()
scene = g.getCurrentScene()
own = cont.owner

#Get_Sensor_Name
joystick = cont.sensors["joystick"]

#Read_Axis
g.Left_X = joystick.axisValues[0] #Left/Right
g.Left_Y = joystick.axisValues[1] #Up/Down

g.Right_Y = joystick.axisValues[3] #Up/Down
g.Right_X = joystick.axisValues[4] #Left/Right

g.trigger = joystick.axisValues[2] #Left/Right_Trigger

#Read_Buttons
g.A = joystick.getButtonStatus(0)
g.B = joystick.getButtonStatus(1)
g.X = joystick.getButtonStatus(2)
g.Y = joystick.getButtonStatus(3)
g.L_Bumper = joystick.getButtonStatus(4)
g.R_Bumper = joystick.getButtonStatus(5)
g.Back = joystick.getButtonStatus(6)
g.Start = joystick.getButtonStatus(7)
g.LTS = joystick.getButtonStatus(8)
g.RTS = joystick.getButtonStatus(9)

g.Left_Trigger = g.trigger > 10000
g.Right_Trigger = g.trigger < -10000

g.center = joystick.hatValues[0]

#Buttons
##A_Button
if g.A == True:
    g.A_buttonTimer += 0.5
else:
    g.A_buttonTimer = 0.0

if g.A_buttonTimer > 0.1 and g.A_buttonTimer < 0.9:
    g.A_buttonStatus = 'pressed'

elif g.A_buttonTimer > 0.9:
    g.A_buttonStatus = 'held'

elif g.A_buttonTimer == 0.0:
    g.A_buttonStatus = '_'

##B_Button
if g.B == True:
    g.B_buttonTimer += 0.5
else:
    g.B_buttonTimer = 0.0

if g.B_buttonTimer > 0.1 and g.B_buttonTimer < 0.9:
    g.B_buttonStatus = 'pressed'

elif g.B_buttonTimer > 0.9:
    g.B_buttonStatus = 'held'

elif g.B_buttonTimer == 0.0:
    g.B_buttonStatus = '_'

##X_Button
if g.X == True:
    g.X_buttonTimer += 0.5
else:
    g.X_buttonTimer = 0.0

if g.X_buttonTimer > 0.1 and g.X_buttonTimer < 0.9:
    g.X_buttonStatus = 'pressed'

elif g.X_buttonTimer > 0.9:
    g.X_buttonStatus = 'held'

elif g.X_buttonTimer == 0.0:
    g.X_buttonStatus = '_'

##Y_Button
if g.Y == True:
    g.Y_buttonTimer += 0.5
else:
    g.Y_buttonTimer = 0.0

if g.Y_buttonTimer > 0.1 and g.Y_buttonTimer < 0.9:
    g.Y_buttonStatus = 'pressed'

elif g.Y_buttonTimer > 0.9:
    g.Y_buttonStatus = 'held'

elif g.Y_buttonTimer == 0.0:
    g.Y_buttonStatus = '_'

##L_Button
if g.L_Bumper == True:
    g.L_buttonTimer += 0.5
else:
    g.L_buttonTimer = 0.0

if g.L_buttonTimer > 0.1 and g.L_buttonTimer < 0.9:
    g.L_buttonStatus = 'pressed'

elif g.L_buttonTimer > 0.9:
    g.L_buttonStatus = 'held'

elif g.L_buttonTimer == 0.0:
    g.L_buttonStatus = '_'

##R_Button
if g.R_Bumper == True:
    g.R_buttonTimer += 0.5
else:
    g.R_buttonTimer = 0.0

if g.R_buttonTimer > 0.1 and g.R_buttonTimer < 0.9:
    g.R_buttonStatus = 'pressed'

elif g.R_buttonTimer > 0.9:
    g.R_buttonStatus = 'held'

elif g.R_buttonTimer == 0.0:
    g.R_buttonStatus = '_'

##Back_Button
if g.Back == True:
    g.Back_buttonTimer += 0.5
else:
    g.Back_buttonTimer = 0.0

if g.Back_buttonTimer > 0.1 and g.Back_buttonTimer < 0.9:
    g.Back_buttonStatus = 'pressed'

elif g.Back_buttonTimer > 0.9:
    g.Back_buttonStatus = 'held'

elif g.Back_buttonTimer == 0.0:
    g.Back_buttonStatus = '_'

##Start_Button
if g.Start == True:
    g.Start_buttonTimer += 0.5
else:
    g.Start_buttonTimer = 0.0

if g.Start_buttonTimer > 0.1 and g.Start_buttonTimer < 0.9:
    g.Start_buttonStatus = 'pressed'

elif g.Start_buttonTimer > 0.9:
    g.Start_buttonStatus = 'held'

elif g.Start_buttonTimer == 0.0:
    g.Start_buttonStatus = '_'

##LTS_Button
if g.LTS == True:
    g.LTS_buttonTimer += 0.5
else:
    g.LTS_buttonTimer = 0.0

if g.LTS_buttonTimer > 0.1 and g.LTS_buttonTimer < 0.9:
    g.LTS_buttonStatus = 'pressed'

elif g.LTS_buttonTimer > 0.9:
    g.LTS_buttonStatus = 'held'

elif g.LTS_buttonTimer == 0.0:
    g.LTS_buttonStatus = '_'

##RTS_Button
if g.RTS == True:
    g.RTS_buttonTimer += 0.5
else:
    g.RTS_buttonTimer = 0.0

if g.RTS_buttonTimer > 0.1 and g.RTS_buttonTimer < 0.9:
    g.RTS_buttonStatus = 'pressed'

elif g.RTS_buttonTimer > 0.9:
    g.RTS_buttonStatus = 'held'

elif g.RTS_buttonTimer == 0.0:
    g.RTS_buttonStatus = '_'

##Left_Trigger
if g.Left_Trigger == True:
    g.Left_TriggerTimer += 0.5
else:
    g.Left_TriggerTimer = 0.0

if g.Left_TriggerTimer > 0.1 and g.Left_TriggerTimer < 0.9:
    g.Left_TriggerStatus = 'pressed'

elif g.Left_TriggerTimer > 0.9:
    g.Left_TriggerStatus = 'held'

elif g.Left_TriggerTimer == 0.0:
    g.Left_TriggerStatus = '_'

##Right_Trigger
if g.Right_Trigger == True:
    g.Right_TriggerTimer += 0.5
else:
    g.Right_TriggerTimer = 0.0

if g.Right_TriggerTimer > 0.1 and g.Right_TriggerTimer < 0.9:
    g.Right_TriggerStatus = 'pressed'

elif g.Right_TriggerTimer > 0.9:
    g.Right_TriggerStatus = 'held'

elif g.Right_TriggerTimer == 0.0:
    g.Right_TriggerStatus = '_'

#D-PAD
if g.center == 1:
    g.UP = True

if g.center == 4:
    g.DOWN = True

if g.center == 8:
    g.LEFT = True

if g.center == 2:
    g.RIGHT = True

if g.center == 0:
    g.UP = False
    g.DOWN = False
    g.LEFT = False
    g.RIGHT = False

##UP
if g.UP == True:
    g.upTimer += 0.5
else:
    g.upTimer = 0.0

if g.upTimer > 0.1 and g.upTimer < 0.9:
    g.up_hatStatus = 'up_pressed'

elif g.upTimer > 0.9:
    g.up_hatStatus = 'up_held'

elif g.upTimer == 0.0:
    g.up_hatStatus = ''

##DOWN
if g.DOWN == True:
    g.downTimer += 0.5
else:
    g.downTimer = 0.0

if g.downTimer > 0.1 and g.downTimer < 0.9:
    g.down_hatStatus = 'down_pressed'

elif g.downTimer > 0.9:
    g.down_hatStatus = 'down_held'

elif g.downTimer == 0.0:
    g.down_hatStatus = ''

##LEFT
if g.LEFT == True:
    g.leftTimer += 0.5
else:
    g.leftTimer = 0.0

if g.leftTimer > 0.1 and g.leftTimer < 0.9:
    g.left_hatStatus = 'left_pressed'

elif g.leftTimer > 0.9:
    g.left_hatStatus = 'left_held'

elif g.leftTimer == 0.0:
    g.left_hatStatus = ''

##RIGHT
if g.RIGHT == True:
    g.rightTimer += 0.5
else:
    g.rightTimer = 0.0

if g.rightTimer > 0.1 and g.rightTimer < 0.9:
    g.right_hatStatus = 'right_pressed'

elif g.rightTimer > 0.9:
    g.right_hatStatus = 'right_held'

elif g.rightTimer == 0.0:
    g.right_hatStatus = ''

Commands


######Joystick_Controls_Script######
## by_Quniton_'Adamsky225'_London ##
############################
def VersionCheck():
    try:
        import Mathutils
        VERSION = 2.49
    except:
        import mathutils
        VERSION = 2.7
    return VERSION

VERSION = VersionCheck()
    
if VERSION == 2.49:
    GameLogic.VERSION = VersionCheck()
    import GameLogic as g
elif VERSION == 2.7:
    import bge
    g = bge.logic

cont = g.getCurrentController()
scene = g.getCurrentScene()
own = cont.owner

targets = scene.objects

rate = 10.0 #max ~movement speed

#get TargetsCam
up = targets["OBforward"]
down = targets["OBback.001"]
left = targets["OBleft.001"]
up_left = targets["OBforward_left"]
down_left = targets["OBback_left"]
right = targets["OBright.001"]
up_right = targets["OBforward_right"]
down_right = targets["OBback_right"]
Cam = targets["OBCam"]

#Get_Sensors
grounded = cont.sensors["Ground"]

#Get_Actuators
tracker = cont.actuators["Tracker"]

#Finders
##Grounded
if grounded.positive:
    g.status = 'grounded'
    g.playerStatus = 'idle'
else:
    g.playerStatus = 'fall'
    g.status = 'airbound'

own['prop'] = g.disable

#Analog
##Action(Run)
if g.status == 'grounded' and g.disable == False:
    own.setLinearVelocity([0,g.yPos,g.zPos], True)
    ###Forwards
    if g.Left_Y > 6.5 and g.lockon == False:
        g.playerStatus = 'run'
        tracker.object = down
        cont.activate(tracker)
        g.yPos = -7.0

    ###Backwards
    elif g.Left_Y < -7.5 and g.lockon == False:
        g.playerStatus = 'run'
        tracker.object = up
        cont.activate(tracker)
        g.yPos = -7.0

    ###Left
    elif g.Left_X < -6.5 and g.lockon == False:
        g.playerStatus = 'run'
        tracker.object = left
        cont.activate(tracker)
        g.yPos = -7.0

    ###Right
    elif g.Left_X > 7.5 and g.lockon == False:
        g.playerStatus = 'run'
        tracker.object = right
        cont.activate(tracker)
        g.yPos = -7.0
    else:
        g.playerStatus = 'idle'
        cont.deactivate(tracker)
        g.yPos = 0

#Button
##Grounded
if g.status == 'grounded' and g.disable == False:
    ###Button(A)
    #Jumping
    if g.A_buttonStatus == 'pressed':
        g.zPos = 10 #Jumping speed
        g.jump = True
    else:
        g.zPos = grounded.hitPosition[2] - 0.1
        g.jump = False

    ###Button(B)
    if g.B_buttonStatus == 'pressed':
        g.pick_Timer = 100

    if g.pick_Timer > 1 and g.pick_Timer < 100 and g.near == 'item':
        g.disable = True
        g.pickup = True

    if g.B_buttonStatus == 'pressed' and g.near == 'box':
        g.pick_Timer = 100

    if g.pick_Timer > 1 and g.pick_Timer < 100 and g.near == 'box':
        g.disable = True
        g.pickup = True

    ###Button(X)
    if g.X_buttonStatus == 'pressed' and g.near == 'item':
        g.weaponUse_Timer = 500

    ###Button(Y)
    if g.Y_buttonStatus == 'pressed':
        g.blank = ''

    ###Button(LB)
    if g.LB_buttonStatus == 'pressed':
        g.blank = ''

    ###Button(RB)
    if g.R_buttonStatus == 'held' and g.X_buttonStatus == 'pressed' and g.dash_Timer == 0:
        g.dash_Timer = 45

    if g.dash_Timer > 10 and g.dash_Timer < 45:
        g.disable = True
        g.dash = True
        g.yPos = -13

    ###Button(LTB)
    if g.LTB_buttonStatus == 'pressed':
        g.blank = ''

    ###Button(RTB)
    if g.RTB_buttonStatus == 'pressed':
        g.blank = ''

Please use code tags rather than quote tags. As you see this code snippet is nearly unreadable.

Too me this code looks way to complicated.
I do not think it can easily be integrated in another project than your one.

Btw.
GameLogic is available in 2.5+. Therefore you can still import it without any problems.

Hi @knight225,
What do you mean with 2.49b: Blender version or other thing, please?

Thanks for this, I still use 2.49 for an old project, as well as 2.74 for new projects. some of this this code might come in handy. :slight_smile:

its blender version 2.49b… and sorry for the quote tags… first time post…