SCA_PythonJoystick activeButtons for jumping

Hello, I have a new problem. My script is running with an Always Sensor True Pulse. In the past, I used to attach a Joystick Sensor to the script with Tap enabled for jumping. But since there is SCA_PythonJoystick, maybe I could have the same thing in Python. I know how to get the buttons, but as far as I know there is no reference to if a button is just activated. I had hoped there was something like bge.logic.KX_INPUT_JUST_ACTIVATED for SCA_PythonJoystick.activeButtons. Is there a reference to whether a button pulse is True or False? Did someone deal with this already and knows how to jump with the SCA_PythonJoystick?

The following blend is a player movement demo that will also work with a joystick. Make sure you are connected before running the game. As expected, holding the button will give a pulse every logic tic.

Attachments

SimpleMovement05.blend (82.1 KB)

Hi there! I’ve been working on a project where the player has the option of using an xbox controller or the keyboard. I have made it so that the character can be controlled by an xbox controller in windows, linux, or mac. You’re welcome to check out my project. There’s a script there that shows you how to solve your problem called ‘shared_button_tap.py’ Also, check the script that is called ‘shared_set_gamepad.py’

Basically, you’ll want to make some object properties that keep track of your player input. When your player newly inputs up for example, one of your object properties returns true for one logic tick. You’ll see how this is done in the scripts. You’re welcome to re-use the scripts I wrote if you find they’re worthy… It’s still a work in progress.

~doakey3

The project:

https://docs.google.com/file/d/0ByCbzbfsSzJLeXVwd2JSa01EX3c/edit?usp=sharing

BTW: I’m new to the game engine and several weeks ago I was wondering how to do character movement. Your blend file was very helpful in teaching me how to use localLinearVelocity. Thanks!

Thanks for sharing, doakey3. The method of the ‘shared_button_tap.py’ sounds to be the solution I need. I’ll look into it for sure. But I wonder. Shouldn’t a joystick return information about when a button is pushed or released? If so, then this definitely should be accessible through SCA_PythonJoystick in the future, in my opinion.

Thanks to the Blender community I managed to learn a lot of things too. And there’s still so much to learn. :yes:

Edit: Very nice! It looks promising.

I totally agree. I was pretty disappointed when I found that there was no ‘JUST_ACTIVATED’ attribute in the SCA_PythonJoystick. I imagine it will be added within the next few blender releases. It just makes sense.

It’s there. There’s an explanation on the page I linked, and it’s available by browsing the code on the Google Code page (in Trunk/BGE). I also added an example in Trunk/BGE/Examples. If you want to download the file, you can SVN check-out the entire code, or you can navigate to the blend file / Python script, right-click on it, and select “Download from Link”, or something to that effect.

Oh, I’m seeing this too late. But thanks anyway. However, I managed to write my own script. Actually it’s not so complicated. Here is the code to get all buttons (of my gamepad, but you can configure all buttons yourself):

from bge import logic

pulseButtons = [0, 4, 5, 6, 7]
tapButtons = [1, 2, 3, 9]

def init(own):
    if not 'joystick' in own:
        tap = []
        for button in tapButtons:
            tap.append(False)
        own['tap'] = tap
        own['joystick'] = logic.joysticks[0]

def getPulseButtons(buttons):
    input = []
    for i in range(len(pulseButtons)):
        status = 0
        button = pulseButtons[i]
        if button in buttons:
            status = 1
        input.append(status)
    return input

<b>def getTapButtons(own, buttons):
    tap = own['tap']
    input = []
    for i in range(len(tapButtons)):
        status = 0
        button = tapButtons[i]
        if button not in buttons:
            if tap[i]:
                own['tap'][i] = False
        elif not tap[i]:
            status = 1
            own['tap'][i] = True
        input.append(status)
    return input</b>

def getButtons(own, joystick):
    buttons = joystick.activeButtons
    run, lCam, rCam, left, right = getPulseButtons(buttons)
    action, jump, fire, menu = getTapButtons(own, buttons)
    return run, action, jump, fire, lCam, rCam, left, right, menu

def main(cont):
    own = cont.owner
    init(own)
    joystick = own['joystick']
    if joystick:
        buttons = getButtons(own, joystick)
        if True in buttons:
            print(buttons)

I’ve added the blend so you can test directly.
I’ve added another blend also, just to illustrate how easily it could be used.

Edit: @ doakey3: I’ve looked at your script and could figure out how it worked. Thanks for that. It was quite large so at first I was thinking to maybe shorten it, but instead I wrote the above, and I hope it’s more or less doing the same thing.

Edit2: Updated the code a bit (and blends). Little improvement in getTapButtons

Attachments

ActiveButtons_pulse_tap.blend (76.1 KB)ShootEnemy14.blend (98.1 KB)