Python - Gamekeys

Edited this post for other question…

cont = GameLogic.getCurrentController()
own = cont.getOwner()

W = cont.getSensor(“W”)
Q = cont.getSensor(“Q”)
E = cont.getSensor(“E”)
run = cont.getActuator(“Run”)
runleft = cont.getActuator(“Runl”)
runright = cont.getActuator(“Runr”)
Leftrun = cont.getActuator(“Lrun”)
if W.isPositive():
GameLogic.addActiveActuator(run, True)
else:
GameLogic.addActiveActuator(run, False)

if Q.isPositive():
GameLogic.addActiveActuator(Leftrun, True)
else:
GameLogic.addActiveActuator(Leftrun, False)

if W.isPositive() and Q.isPositive():
GameLogic.addActiveActuator(runleft, True)
else:
GameLogic.addActiveActuator(runleft, False)

if W.isPositive() and E.isPositive():
GameLogic.addActiveActuator(runright, True)
else:
GameLogic.addActiveActuator(runright, False)

I’ve made this script. The problem is that when you press W + Q, the action for only Q plays in staid of the action for W and Q… Is there any way to fix this…? For example keypress Q = true but Keypress W = false?

Thanks a lot. Hope you understand :stuck_out_tongue:

Maybe this will work?



cont = GameLogic.getCurrentController()
own = cont.getOwner()
 
W = cont.getSensor("W")
Q = cont.getSensor("Q")
E = cont.getSensor("E")
run = cont.getActuator("Run")
runleft = cont.getActuator("Runl")
runright = cont.getActuator("Runr")
Leftrun = cont.getActuator("Lrun")

if W.isPositive() and not (Q.isPositive() or E.isPositive()):
   GameLogic.addActiveActuator(run, True)
else:
   GameLogic.addActiveActuator(run, False)

if Q.isPositive() and not W.isPositive():
   GameLogic.addActiveActuator(Leftrun, True)
else:
   GameLogic.addActiveActuator(Leftrun, False)

if W.isPositive() and Q.isPositive():
   GameLogic.addActiveActuator(runleft, True)
else:
   GameLogic.addActiveActuator(runleft, False)
 
 
if W.isPositive() and E.isPositive():
   GameLogic.addActiveActuator(runright, True)
else:
   GameLogic.addActiveActuator(runright, False)


Had to fix a little but thanks a lot :wink: This was what I was looking for. :slight_smile:

Incidentally… What version of Blender are you using? The latest versions should provide deprecation warnings with that script. This simply means it will not work when 2.5 is released, but is ok for now.

A version that should not give warnings is:



cont = GameLogic.getCurrentController()
own = cont.owner
 
W = cont.sensors["W"]
Q = cont.sensors["Q"]
E = cont.sensors["E"]
run = cont.actuators["Run"]
runleft = cont.actuators["Runl"]
runright = cont.actuators["Runr"]
Leftrun = cont.actuators["Lrun"]

if W.positive and not (Q.positive or E.positive):
   cont.activate(run)
else:
   cont.deactivate(run)

if Q.positive and not W.positive:
   cont.activate(Leftrun)
else:
   cont.deactivate(Leftrun)

if W.positive and Q.positive:
   cont.activate(runleft)
else:
   cont.deactivate(runleft) 
 
if W.positive and E.positive:
   cont.activate(runright)
else:
   cont.deactivate(runright)

I have not tried this out.