2
$\begingroup$

I am making bge games and want them full controller support and I have already scripted the input, but the axis and the buttons index is changing when having different amount of controllers. Here is what I mean:

#joystick for buttons, joystickaxis for axis

#2 controller

#controller 1
joystick = bge.logic.joysticks[0]
joystickaxis = bge.logic.joysticks[1]

#controller 2
joystick = bge.logic.joysticks[1]
joystickaxis = bge.logic.joysticks[0]

#4 controller 

#controller 4
joystick = bge.logic.joysticks[3]
joystickaxis = bge.logic.joysticks[0]
#controller 3
joystick = bge.logic.joysticks[2]
joystickaxis = bge.logic.joysticks[1]
#controller 2
joystick = bge.logic.joysticks[1]
joystickaxis = bge.logic.joysticks[2]
#controller 2
joystick = bge.logic.joysticks[0]
joystickaxis = bge.logic.joysticks[3]

#3 controller

#controller 3
joystick = bge.logic.joysticks[2]
joystickaxis = bge.logic.joysticks[0]
#controller 2
joystick = bge.logic.joysticks[1]
joystickaxis = bge.logic.joysticks[1]
#controller 1
joystick = bge.logic.joysticks[0]
joystickaxis = bge.logic.joysticks[2]

#1 controller

joystick = bge.logic.joysticks[0]
joystickaxis = bge.logic.joysticks[0] 

Now I need my script to know how many controllers are registered in bge and I would like to know if there is a python function for that, I mean bge seems at least to know how many there are.

$\endgroup$
2
  • $\begingroup$ Just to let you know: Code repetition is no good design. It makes development and maintenance really hard. I suggest to use common programming techniques, like looping, encapsulation, parameters and so on. $\endgroup$
    – Monster
    Feb 13, 2018 at 7:16
  • $\begingroup$ Thank you for the suggestion, I will try to keep that in mind. $\endgroup$ Feb 13, 2018 at 10:27

1 Answer 1

1
$\begingroup$

bge.logic.joysticks gives a list of the max number of controllers (eight), now if there is not a controller the list will have None in that slot.

All you need to do is count how many not None items are in that list, or what I did, count how many are None.

connected_controllers  = 8 - bge.logic.joysticks.count(None)

Since we know there are always eight controller slots, it counts how many are "None" and subtracts that from 8. Quite simple really.

$\endgroup$
3
  • $\begingroup$ len(bge.logic.joysticks) - bge.logic.joysticks.count(None) $\endgroup$
    – Monster
    Feb 13, 2018 at 7:11
  • $\begingroup$ @Monster that is essentially the same as what I have. Except because the controller list is 8 I skip that check. $\endgroup$
    – David
    Feb 13, 2018 at 15:02
  • $\begingroup$ It is just a modification of your snippet (thanks for that hint). The modification avoids to hard-code the amount of possible joysticks. $\endgroup$
    – Monster
    Feb 14, 2018 at 6:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .