Skip to content

Instantly share code, notes, and snippets.

@nkrapivin
Created April 16, 2022 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkrapivin/f4db99ccf62c04a9923b34cc16a93f98 to your computer and use it in GitHub Desktop.
Save nkrapivin/f4db99ccf62c04a9923b34cc16a93f98 to your computer and use it in GitHub Desktop.
Input Switch applet bindings
/// @description A little wrapper around Input and the Switch controllers applet. To be used instead of hot swapping.
/// @param {Real} minPlayersReal Minimum amount of players
/// @param {Real} maxPlayersReal Maximum amount of players
/// @param {Bool} maintainConnectionBool Whether to NOT disconnect controllers during the applet
/// @param {Bool} permitJoyConDualBool Whether to allow dual Joy-Cons in the applet
function input_switch_invoke(minPlayersReal, maxPlayersReal, maintainConnectionBool, permitJoyConDualBool) {
var _spBool = minPlayersReal <= 1 && maxPlayersReal <= 1;
// input_switch_invoke(1, 1, true); // will invoke the singleplayer applet
// input_switch_invoke(1, 2, true); // will invoke the co-op applet
switch_controller_support_set_defaults();
switch_controller_support_set_player_min(minPlayersReal);
switch_controller_support_set_player_max(maxPlayersReal);
switch_controller_support_set_singleplayer_only(_spBool);
switch_controller_support_set_maintain_connections(maintainConnectionBool);
switch_controller_support_set_permit_joycon_dual(permitJoyConDualBool);
switch_controller_support_set_left_justify(true); // ALWAYS shift pad slots to the left whenever possible PLEASE!
var _appletResult = switch_controller_support_show();
if (_appletResult < 0) {
switch (_appletResult) {
case -1: return "failed"; // uh oh
case -2: return "cancelled"; // the game is supposed to handle cancellation in it's own way
case -3: return "unsupported_style"; // uh oh x2
default: return "unknown_" + string(_appletResult); // oh shit
}
}
// applet return values:
// singleplayer: pad slot to listen to
// co-op: amount of players that are connected. 0 means cancelled.
var _appletData = _spBool? switch_controller_support_get_selected_id(): switch_controller_support_get_player_count();
if (_spBool) {
// set Player 0 to our pad slot, set others source to NONE (they are not present at all)
input_player_source_set(INPUT_SOURCE.GAMEPAD, 0);
input_player_gamepad_set(_appletData, 0);
// disconnect all other players
for (var _i = 1; _i < INPUT_MAX_PLAYERS; ++_i) {
input_player_source_set(INPUT_SOURCE.NONE, _i);
}
}
else {
// since pad slots are left shifted, we can do this trickery...
for (var _i = 0; _i < INPUT_MAX_PLAYERS; ++_i) {
if (_i < _appletData /* the actual player count */) {
// this player is below the player count, so inside the applet
input_player_source_set(INPUT_SOURCE.GAMEPAD, _i);
// player 0 is on slot 1
input_player_gamepad_set(1 + _i, _i);
}
else {
// this player was not present in the applet, disconnect them
input_player_source_set(INPUT_SOURCE.NONE, _i);
}
}
}
return "ok";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment