XBox controller and python script

I have a simple python script that reads the axes and button values from a xbox controller. The problem i am experiencing is that when the script starts, the buttons and axes respond as expected. Single tap on buttons works fine. But once i move any of the axes, the button response becomes continuous when any button is pressed. I assume the problem is in my script, since this happens with multiple xbox controllers.

I have attached the script (internal to blend file) and blend file.

Any help would be appreciated.

Attachments

Game Controller Test 002.blend (464 KB)

i have a working script here. its called gamepad_finder.

it doesnt auto find all the devices, you have to copy the empty and change the id game property.

some versions of blender dont work with two controllers. it switches the axis and buttons between the devices.

if you are looking for a way to read gamepad inputs, you should have a look at the rest of the repository. the test.blend is a working demo of it.

That is not my problem, the xbox controller works fine, it just that after using the axis joysticks, the behavior of the buttons changes, and the tap mode no longer works. I have seen blender bugs on these controller, but they didn’t seem to apply to this situation.

I started with the xbox controller from the github

UPBGE xbox controller seems to work flawlessly for me*

The one i am using is a Logitech. It is the second controller to show this behavior, maybe it is my script, but it seems like a rather simple script. Can someone try the script i attached to my blend file (above) and see if they get the same behavior?

have you tried upbge 2.2 or so?

What is upbge 2.2?

https://upbge.org

i couldnt look at the blend earlier since i was away from my computer, but i will test your file with my controllers.

EDIT:
after a quick look, i think the script is not designed to work in tap mode. you need true pulse on the sensors or the script wont run. the “skip” or “freq” needs to be at 0.

Thanks for your help

I have tried the same blend file using UPBGE, and it exhibits the same behavior. I have attached the script, since it seems exhibit the same behavior on all xbox controllers.



import bge
import GameLogic




# This number will be used to lower the output value of the Xbox Controller 
reduction = 32768


# This is the lowest value the Xbox Controller can output and still trigger an event 
axisTh = 0.10 


# 'cont' is the Controller logic brick that uses this script 
cont = GameLogic.getCurrentController() 


# 'obj' references objects in the current Scene - This allows selected objects to be manipulated by the script - 
obj = bge.logic.getCurrentScene().objects


# 'char' references objects with character physics - This allows selected characters to be manipulated by the script -
char = bge.constraints.getCharacter


own = cont.owner


# These two are the Sensor logic bricks connected to the python Controller 
# Example - sensor1 = cont.sensors["SensorName"] - 
aXis = cont.sensors["Axis"]
bUtt = cont.sensors["Butt"]


numAxes = aXis.numAxis
numButton = bUtt.numButtons
#numHats = bUtt.numHats


if 'init' not in own:
    own['init'] = 1
    print("
Game Started ......")
    print("Number of axes = ",numAxes)
    print("Number of buttons = ",numButton)
    #print("Number of Hats = ",numHats)
    print("
")




#  Arrays to keep track of axis values and button status
buttStatus = [False]*numButton
axisValues = [0]*numAxes
#hatValues = [0]*numHats


noData = True
for i in range(numAxes):
  axisValues[i] = round(aXis.axisValues[i] / reduction,3)
 
if abs(axisValues[0]) > axisTh:
     noData = False
if abs(axisValues[1]) > axisTh:
     noData = False
if abs(axisValues[3]) > axisTh:
     noData = False
if abs(axisValues[4]) > axisTh:
     noData = False     
if abs(axisValues[2]) != 1.0:
     noData = False       
if abs(axisValues[5]) != 1.0:
     noData = False   
         
if not noData:
    print("Axis values ",axisValues)
    
for i in range(numButton):
 buttStatus[i] = bUtt.getButtonStatus(i)
 if buttStatus[i]:
     print("Button ",i," Pushed")
     
def main():
    i =0


# This runs the main function 
main()

what are trying to do?

Please see initial post above

Please see initial post above. Erratic behavior of xbox controller using python script.

i mean, whats the end goal? why do you need this to work? this doesnt look effective for any game setup. if you need something to analyze joysticks, then ive provided a working setup.

Game Controller Test 002 dae.blend (520 KB) this should work

Thanks for your reply. I am new to game development with Blender. With the streaming behavior of the button presses, how do i detect a single button press, and not get multiple hits. For example, if A button changes weapons, now do i handle to multiple hits i would get while the user holds down the A button?

my keymapper code has a function called “GAMEPADDER”(bottom of input.py) that solves this issue if you want to go python route. but i recommend just using one brick per button until you learn more.

Can you post the input.py file? I am trying to go the python route. I modified you blend to one brick for the 0 button, even if i use tap or level or continuous trigger, i still get multiple hits for each button.

these pages can be found by clicking on my sig, then clicking the keymapper repository.


download the 0.2.0 zip file, extract. the second link points to a wiki page. it will help you set it up. there is a test.blend in the extras folder showing a complete setup where the scripts are packed into the blend.

im working on making the webpages more clear, im still new at this :slight_smile:

Thanks for your input. I looked at the keymapper data. From what i can see, this mainly deals with mouse and keyboard input, along with the joystick axes. That is not my problem, i am interested in getting the button hits from the xbox controller, not the axis values.

I see the info on: JOYBUTTON (int)(optional): The ID of the button to look for. only “B”, see JOYAXIS[2].
But don’t understand how to use it.

I also think i should report this as a bug, because the the joystick sensor behavior in the game engine does not perform as designed.