Pixel Vision 8 Button() Lua Tutorial

The main form of input for Pixel Vision 8 is the controller's buttons. You can get the current state of any button by calling the Button() method and supplying a button ID. There are optional parameters for specifying an InputState and the controller ID. When called, the Button() method returns a bool for the requested button and its state.

The InputStateenum contains options for testing the Downand Releasedstates of the supplied button ID. By default, Downis automatically used which returns truewhen the key was pressed in the current frame. When using Released, the method returns trueif the key is currently up but was down in the last frame.

Usage

Button ( button, state, controllerID )

Arguments

NameValueDescription
buttonButtonsAccepts the Buttons enum or int for the button's ID.
stateInputStateOptional InputState enum. Returns down state by default.
controllerIDintAn optional int representing a controller ID. Player 1 is 0 and player 2 is 1. leaving this empty will deafult to player 1.

Returns

ValueDescription
boolReturns a bool based on the state of the button.

Button Enums

Each controller has 8 buttons. A button has a specific ID or you can reference it by the button’s enum name.

EnumValue
Buttons.Up0
Buttons.Down1
Buttons.Left2
Buttons.Right3
Buttons.A4
Buttons.B5
Buttons.Select6
Buttons.Start7

Input State Enums

There are two input states you can use to test a button’s current state:

EnumValue
InputState.Down0
InputState.Released1

Button Example

In this example, we will loop through all of the buttons on controller 1 and display their names on the screen.

Running this code will output the following:

ButtonOutput_image_0.png

Learn more about making Pixel Vision 8 games by checking out the docs.

Step 1

Create a new file called code.lua.{1} in your project folder.

Step 2

Create a new local variable called pressedButtons inside the script:

001 local pressedButtons = {}

This array will store any buttons pressed during the current frame

Step 3

Create a new local variable called buttons inside the script:

002 local buttons = {
003 Buttons.Up,
004 Buttons.Down,
005 Buttons.Left,
006 Buttons.Right,
007 Buttons.A,
008 Buttons.B,
009 Buttons.Select,
010 Buttons.Start
011 }

A list of all the buttons to check on each frame

Step 4

Create a new function called Init():

012 function Init()
013 
014 end

Step 5

Add the following code to the script:

013   DrawText("Button()", 1, 1, DrawMode.Tile, "large", 15);
014   DrawText("Lua Example - Press a direction or action button", 8, 16, DrawMode.TilemapCache, "medium", 15, -4);

Example Title

Step 6

Create a new function called Update():

016 function Update(timeDelta)
017 
018 end

Step 7

Add the following code to the script:

017   pressedButtons = {}

Clear the pressedButtons array on each frame

Step 8

Create the following Loop:

018   for i = 1, #buttons do
019   
020   end

Loop through all the buttons

Step 9

Add the following condition to the script:

019     if(Button(buttons[i], InputState.Down, 0)) then
020     table.insert(pressedButtons, tostring(buttons[i]))
021     end

Test if player 1's current button is down and save it to the pressedButtons array

Step 10

Create a new function called Draw():

024 function Draw()
025 
026 end

Step 11

Add the following code to the script:

025   RedrawDisplay()

Clear the display

Step 12

Create a new local variable called message inside the script:

026   local message = table.concat(pressedButtons, ", "):upper()

Convert the pressedButtons into a string and draw to the display

Step 13

Add the following code to the script:

027   DrawText(message:sub(0, #message), 8, 32, DrawMode.Sprite, "medium", 14, - 4)

DrawText("Buttons Down:", 8, 8, DrawMode.Sprite, "large", 15)

Final Code

When you are done, you should have the following code in the code.lua file:

`Lua 001 local pressedButtons = {} 002 local buttons = { 003 Buttons.Up, 004 Buttons.Down, 005 Buttons.Left, 006 Buttons.Right, 007 Buttons.A, 008 Buttons.B, 009 Buttons.Select, 010 Buttons.Start 011 } 012 function Init() 013 DrawText("Button()", 1, 1, DrawMode.Tile, "large", 15); 014 DrawText("Lua Example - Press a direction or action button", 8, 16, DrawMode.TilemapCache, "medium", 15, -4); 015 end 016 function Update(timeDelta) 017 pressedButtons = {} 018 for i = 1, #buttons do 019 if(Button(buttons[i], InputState.Down, 0)) then 020 table.insert(pressedButtons, tostring(buttons[i])) 021 end 022 end 023 end 024 function Draw() 025 RedrawDisplay() 026 local message = table.concat(pressedButtons, ", "):upper() 027 DrawText(message:sub(0, #message), 8, 32, DrawMode.Sprite, "medium", 14, - 4) 028 end