function init(self)
msg.post(".", "acquire_input_focus")
end
function on_input(self, action_id, action)
if action.pressed then
if action_id == hash("X") then
print("X")
elseif action_id == hash("Y") then
print("Y")
elseif action_id == hash("A") then
print("A")
elseif action_id == hash("B") then
print("B")
end
end
end
Lecric!!! Firstly, I must say itâs fantastic to see someone else creating their own controller for Defold! Hereâs my own mighty beast. Itâs connected to a raspberry pi that I use to trigger animations on a screen behind me when I DJ.
In terms of your controller issue, you will need to do as britzl says. Create a new *.gamepads file and add it there. Thatâs what I did.
However, I quickly ran into some problems with the android OS I was running my app on, which wouldnât recognise the gamepad. The solution? Get a rasperry pico (5âŹ) and set it up as a macropad. Itâs automatically recognised as a keyboard (rather than a gamepad, which face compatibility issues on different devices), itâs 0 latency, and you can connect it to anything. If you donât mind soldering the buttons (they are the easiest thing to solder, I promise) itâs much cheaper and much more compatible, with no disadvantages over a gamepad device. I can even share the code for a simple macropad. itâs here. Thereâs also a youtube video, although, it comes with a few gotchas which are solved in my repo.
Please, share some information about your project!! And although I am sure you can get your gamepad working, please ask me any questions if you take the macro keyboard route.
Hey Josh,
Itâs cool to know that Iâm not the only one who had the idea.
Iâm going to put it in the bookstores to play my game Drop In Music.
The buttons in the game will have the same position in the game and it will allow for some physical things to push
I didnât know I had to use gamepad input, but I still donât understand how to use it.
I have created a new file, added a new gamepad, but when I test it doesnât show. (i test with gamepad tester)
Hereâs my thread looking into the same thing. You have screenshots of the input and gamepad files, as well as the code you need to print what the gamepad gives you. Try setting it up exactly like that and seeing if it works.
The other option (which I think might actually be better) is to try the following in your on_input function and seeing if you are getting raw data.
if action_id == hash("raw") then
pprint(action.gamepad_buttons)
pprint(action.gamepad_axis)
pprint(action.gamepad_hats)
end
If you canât get this to work, and you donât want to solder, you could get a raspberry Pico with pre-soldered header pins and buttons with screw terminals. The Pico even has a 5v output so you could still light your buttons up.
The keycodetable says what key code is activated by each IO connector. Youâll need to customise that.
Youâll also need to customise the GPs table to go from 0 to 20 (it currently goes from 0 to 8).
Youâll also have to change the for i in range(9):both times that it appears. Change the 9 to 20.
Is it compulsory to turn on the Pico each time to use it? How did you do it?
No, you donât have to turn it on. You plug it into the USB, and start pressing buttons.
What language are you using? I have the impression that itâs MicroPython.
Itâs either python or micropython. Sorry I cannot help you more than that. I know just the basics. But itâs a very basic project.
Is there anything to implement in defold to make the controller playable.
No. If you use my code, itâs the same as pressing a key on a USB keyboard. It works on anything you can plug a USB keyboard into (I would guessâŚ).
Is it possible to send messages to the Pico from Defold and how? To change the leds for example.
You have two options for the LEDs:
You can get 5v from Pin40 (labelled VBUS) of the pico [assuming your USB connection provides 5v]. That means the LEDs will turn on as soon as the pico is plugged in, and stay turned on.
You can get messages from the buttons themselves to the pico (but you canât send messages from defold to the Pico). So, you could program the pico so that LEDs turn on and off according to button presses (if your LEDs work with 3.3v, which is what the pico outputs from GP). It would involve putting these lines of code in somewhere:
from machine import Pin -- this fetches the library.
pin_led = Pin(26, mode=Pin.OUT) -- defines GP26 as controlling an LED
pin_led.on() -- sends 3.3v to GP26
pin_led.off() -- sends 0v to GP26
Read more about controlling LEDs with a pico here.
Itâs obligatory to push start buttons for start controller ?
If yes, How do you get it started since itâs in your box?
Is there really no way to communicate with Pico via native or whatever?
I donât know much about it, but Iâd be willing to give it a go. (like electronics )