Controllers: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
No edit summary
(changed sample code to print button glyphs instead of numbers :))
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Pico-8 supports the use of game controllers that can be connected to a computer, either with a wired USB connection or a wireless Bluetooth connection. Pico-8's controller support is based on the [[SDL]] library.
PICO-8 supports the use of game controllers that can be connected to a computer, either with a wired USB connection or a wireless Bluetooth connection. PICO-8's controller support is based on the [[SDL]] library.


A game can have up to 8 simultaneous players. Players 1 and 2 can use game controllers or corresponding keys on the keyboard. Players 3-8 must use game controllers.
A game can have up to 8 simultaneous players. Players 1 and 2 can use game controllers or corresponding keys on the keyboard. Players 3-8 must use game controllers.
Line 5: Line 5:
== Keyboard controls ==
== Keyboard controls ==


While a game is playing, Pico-8 recognizes keyboard presses as controls for players 1 and 2. For example, player 1 can use the cursor keys, Z, and X for the six possible inputs.
While a game is playing, PICO-8 recognizes keyboard presses as controls for players 1 and 2. For example, player 1 can use the cursor keys, Z, and X for the six possible inputs.


For a complete list of default key mappings for players 1 and 2, see [[Btn|btn()]].
For a complete list of default key mappings for players 1 and 2, see [[Btn|btn()]].
Line 13: Line 13:
== Configuring controllers ==
== Configuring controllers ==


You can configure controller mappings by editing the <code>sdl_controllers.txt</code> file in the Pico-8 [[Configuration|configuration]] directory. Each line of the file is a controller mapping in the format used by SDL. You only need one line for each kind of controller you wish to use.
You can configure controller mappings by editing the <code>sdl_controllers.txt</code> file in the PICO-8 [[Configuration|configuration]] directory. Each line of the file is a controller mapping in the format used by SDL. You only need one line for each kind of controller you wish to use.


Here is an example of a mapping line:
Here is an example of a mapping line:
Line 23: Line 23:
You can generate this line using one of these tools:
You can generate this line using one of these tools:


* [http://www.generalarcade.com/gamepadtool/ General Arcade's GamePadTool] for Windows or Mac
* [http://www.generalarcade.com/gamepadtool/ General Arcade's GamepadTool] for Windows, Mac and Linux
* The <code>controllermap</code> tool distributed with [https://www.libsdl.org/download-2.0.php SDL2]
* The <code>controllermap</code> tool distributed with [https://www.libsdl.org/download-2.0.php SDL2]
* Find your SDL2 controller mapping in [https://github.com/gabomdq/SDL_GameControllerDB/blob/master/gamecontrollerdb.txt this list].


The complete procedure for using GamePadTool:
The complete procedure for using GamePadTool:
Line 31: Line 32:
# Start GamePadTool. The tool should detect the controllers and allow you to select between them.
# Start GamePadTool. The tool should detect the controllers and allow you to select between them.
# Make sure that the controller is selected, then click "Copy Mapping String." This copies the mapping string to your clipboard.
# Make sure that the controller is selected, then click "Copy Mapping String." This copies the mapping string to your clipboard.
# Edit the <code>sdl_controllers.txt</code> file in the Pico-8 [[Configuration|configuration]] directory. Paste the mapping string onto its own line, then save the file.
# Edit the <code>sdl_controllers.txt</code> file in the PICO-8 [[Configuration|configuration]] directory. Paste the mapping string onto its own line, then save the file.
# Start (or restart) Pico-8 to use the new configuration.
# Start (or restart) PICO-8 to use the new configuration.


== Connecting controllers ==
== Connecting controllers ==


Pico-8 assigns controllers to players in roughly the order they are detected when Pico-8 starts up. Player 1 gets the first controller.
PICO-8 assigns controllers to players in roughly the order they are detected when PICO-8 starts up.


There is no way to assign specific controllers to specific players. Also, there is no way to force the first controller to get assigned to Player 3. To use a controller with player 3, you must have at least three controllers connected. (Players 1 and 2 can continue to use the keyboard, but there must be controllers connected for those players.)
By default, player 1 gets the first controller. You can change which player gets the first controller by providing the <code>-joystick</code> command line argument when starting PICO-8. Its value is a number from 0 to 7 (player 1 through 8). For example, to start assigning controllers with player 3:


If you connect a controller while Pico-8 is running, you must restart Pico-8 before you can use it.
<code>pico8 -joystick 2</code>
 
There is no way to assign specific controllers to specific players.
 
If you connect a controller while PICO-8 is running, you must restart PICO-8 before you can use it.


== Testing controllers ==
== Testing controllers ==
Line 49: Line 54:
function _update()
function _update()
end
end
glyphs={"\139","\145","\148","\131","\142","\151"}
function _draw()
function _draw()
   cls()
   cls()
Line 55: Line 62:
     for b=0,6 do
     for b=0,6 do
       if btn(b,p) then
       if btn(b,p) then
         print(b, b*5 + 10, p*7, p+1)
         print(glyphs[b+1], b*8 + 10, p*7, p+1)
       end
       end
     end
     end
Line 67: Line 74:
* [[Keyconfig|keyconfig]]
* [[Keyconfig|keyconfig]]
* [[SDL]]
* [[SDL]]
[[Category:Reference]]

Latest revision as of 04:58, 30 June 2022

PICO-8 supports the use of game controllers that can be connected to a computer, either with a wired USB connection or a wireless Bluetooth connection. PICO-8's controller support is based on the SDL library.

A game can have up to 8 simultaneous players. Players 1 and 2 can use game controllers or corresponding keys on the keyboard. Players 3-8 must use game controllers.

Keyboard controls

While a game is playing, PICO-8 recognizes keyboard presses as controls for players 1 and 2. For example, player 1 can use the cursor keys, Z, and X for the six possible inputs.

For a complete list of default key mappings for players 1 and 2, see btn().

You can customize the key mappings for players 1 and 2 interactively using the keyconfig command. You can also edit config.txt directly.

Configuring controllers

You can configure controller mappings by editing the sdl_controllers.txt file in the PICO-8 configuration directory. Each line of the file is a controller mapping in the format used by SDL. You only need one line for each kind of controller you wish to use.

Here is an example of a mapping line:

79000000000000001100000000000000,Retrolink Classic Controller,x:b3,a:b2,b:b1,y:b0,back:b8,start:b9,leftshoulder:b4,rightshoulder:b5,leftx:a3,lefty:a4,platform:Mac OS X,

You can generate this line using one of these tools:

The complete procedure for using GamePadTool:

  1. Connect a controller to your computer.
  2. Start GamePadTool. The tool should detect the controllers and allow you to select between them.
  3. Make sure that the controller is selected, then click "Copy Mapping String." This copies the mapping string to your clipboard.
  4. Edit the sdl_controllers.txt file in the PICO-8 configuration directory. Paste the mapping string onto its own line, then save the file.
  5. Start (or restart) PICO-8 to use the new configuration.

Connecting controllers

PICO-8 assigns controllers to players in roughly the order they are detected when PICO-8 starts up.

By default, player 1 gets the first controller. You can change which player gets the first controller by providing the -joystick command line argument when starting PICO-8. Its value is a number from 0 to 7 (player 1 through 8). For example, to start assigning controllers with player 3:

pico8 -joystick 2

There is no way to assign specific controllers to specific players.

If you connect a controller while PICO-8 is running, you must restart PICO-8 before you can use it.

Testing controllers

Here is code for a simple cart that lets you test controllers for all players:

function _update()
end

glyphs={"\139","\145","\148","\131","\142","\151"}
function _draw()
  cls()
  for p=0,8 do
    print(''..p..':', 0, p*7, 1)
    for b=0,6 do
      if btn(b,p) then
        print(glyphs[b+1], b*8 + 10, p*7, p+1)
      end
    end
  end
end

See also