Gamepad

The gamepad is based on an XBox-style gamepad. Scion2D supports up to 4 controllers/gamepads. The gamepad usually will have two analog thumb sticks, a D-Pad, four face buttons, two shoulder buttons, two analog trigger buttons, and two center buttons.

Action Buttons

ButtonTypeDescription
GP_BTN_Aaction buttonOne of the four action buttons on the controller. Usually the A button on the XBox and the Circle button on a playstation style controller.
GP_BTN_Baction buttonOne of the four action buttons on the controller. Usually the B button on the XBox and the Circle button on a playstation style controller.
GP_BTN_Xaction buttonOne of the four action buttons on the controller. Usually the X button on the XBox and the Circle button on a playstation style controller.
GP_BTN_Aaction buttonOne of the four action buttons on the controller. Usually the Y button on the XBox and the Circle button on a playstation style controller.

Special Buttons

ButtonTypeDescription
GP_BTN_BACKspecial buttonOne of the three special buttons on the controller. Usually the SELECT button on the XBox and the Circle button on a playstation style controller.
GP_BTN_STARTspecial buttonOne of the three special buttons on the controller. Usually the START button on the XBox and the Circle button on a playstation style controller.
GP_BTN_GUIDEspecial buttonOne of the three special buttons on the controller. May not be supported by your controller.

Axes

The axes are buttons and joysticks that use analog input values rather than a digital/bool input.

ButtonTypeDescription
AXIS_X1x axisAxis X1 is the horizontal axis of the LEFT analog stick. The analog values are -32768 for fully left and 32767 for fully right.
AXIS_Y1y axisAxis Y1 is the vertical axis of the LEFT analog stick. The analog values are -32768 for fully up and 32767 for fully down.
AXIS_X2x axisAxis X2 is the horizontal axis of the RIGHT analog stick. The analog values are -32768 for fully left and 32767 for fully right.
AXIS_Y2y axisAxis Y2 is the vertical axis of the RIGHT analog stick. The analog values are -32768 for fully up and 32767 for fully down.
AXIS_Z1z axisAxis Z1 is the axis for the LEFT trigger analog. The analog values are -32768 for fully released and 32767 for fully pressed.
AXIS_Z2y axisAxis Z2 is the vertical axis of the RIGHT trigger analog. The analog values are -32768 for fully released and 32767 for fully pressed.

Hat Values

Not all controllers have hats. The hat is basically the DPAD; however, it also has the diagonal directions. We use the hat values based on a number that is returned depending on the direction that is pressed. The typical hat values are as follows:

DirectionHat Value
UP1
RIGHT2
UP RIGHT3
DOWN4
DOWN RIGHT6
LEFT8
UP LEFT9
DOWN LEFT12

Functions:

All of the controller functions take the index of which controller is being used. We are allowed up to 4 controllers in the engine. The index is basically the SLOT of the controller.

FunctionDescription
just_pressed(index, btn)The just pressed function is used when the user only wants the btn to register once per press, and the reset upon released.
just_released(index, btn)This will return true whenever the btn argument has been released.
pressed(index, btn)Returns true whenever the button is held down. Used when the user needs continuous input from the btn.
get_axis_position(index, axis)This will return an analog value for the desired axis. The value returned will be between -32768 and 32767. Please see the Axes for more information.
get_hat_value(index)This will return the current value of the controllers HAT, if the controller has a hat. Please see the Hat Values for more information.

Examples

Left Stick Analog

-- Get current x speed from the analog value local x_analog = Gamepad.get_axis_position(1, AXIS_X1) local x_multiplier = x_analog / 32768 local x_speed = speed * x_multiplier -- Get current y speed from the analog value local y_analog = Gamepad.get_axis_position(1, AXIS_Y1) local y_multiplier = y_analog / 32768 local y_speed = speed * y_multiplier -- Set the linear velocity local physics = entity:get_component(Physics) physics:set_linear_velocity(x_speed, y_speed) -- This does not take into consideration the dead zone of the axis -- The dead zone varies from controller to controller

Using hat values for movement

-- Get the hat value for controller 1 local direction = Gamepad.get_hat_value(1) -- move the player based on the current hat value if direction == 0 then -- TODO: Stop the player elseif direction == 1 then -- TODO: Move the player up elseif direction == 2 then -- TODO: Move the player right elseif direction == 3 then -- TODO: Move the player up to the right elseif direction == 4 then -- TODO: Move the player down elseif direction == 6 then -- TODO: Move the player down to the right elseif direction == 8 then -- TODO: Move the player left elseif direction == 9 then -- TODO: Move the player up to the left elseif direction == 12 then -- TODO: Move the player down to the left end -- This is a simplification of what we could use -- the hat values for.

Using just_pressed for UI

if Gamepad.just_pressed(1, GP_DPAD_UP) then -- Move cursor up elseif Gamepad.just_pressed(1, GP_DPAD_RIGHT) then -- Move cursor right elseif Gamepad.just_pressed(1, GP_DPAD_DOWN) then -- Move cursor down elseif Gamepad.just_pressed(1, GP_DPAD_LEFT) then -- Move cursor left end