00:00
00:00
3p0ch
If you like hard games try my Daxolissian System series

plasmid @3p0ch

Cat

Scientist

Read the manual & try stuff

So Cal

Joined on 2/13/10

Level:
13
Exp Points:
1,636 / 1,880
Exp Rank:
38,479
Vote Power:
5.48 votes
Audio Scouts
1
Rank:
Portal Security
Global Rank:
23,758
Blams:
50
Saves:
370
B/P Bonus:
8%
Whistle:
Normal
Medals:
4,606
Supporter:
3y 2m 20d

How to use the Gamepad mapper with Godot

Posted by 3p0ch - April 3rd, 2022


Just copy/paste the code below and save it in your project as an AutoLoad -- see the instructions within the code.

extends Node

# Save this in your Godot project as gamepad_autoload.gd
# Set it as an autoload in Project - Project Settings - AutoLoad tab
#   and name it GamepadAutoload, and click the Add button
#
# In the button_list and axis_list variables below, specify which actions you want
#   to be associated with which each button or stick direction
# Don't use the D-pad because it doesn't work on my Windows lappy with a Switch
#   controller and I won't be able to play ur game :(
#
# You can set the gamepad actions in Project Settings - Input Map like normal
#   to be able to play the game locally from Godot.
# When the game is exported and run in HTML, it will remove the gamepad settings
#   that you set up to make it work on your system, and will use the gamepad
#   mapper at the game's hosting site.
#
####### Functions and parameters #######
# GamepadAutoload.is_loaded will be true if a gamepad configuration has been
#   loaded and false otherwise. You can use that to style buttons on the main
#   menu (like "Configure gamepad" vs "Re-configure gamepad", make the default
#   focused button be configuration if it's not already configured, etc.)
#
# open_gamepad_config() will open the page for the gamepad configuration gizmo
#   at the website specified in gamepad_config_url below.
#
# read_gamepad_data() will read the gamepad data that's stored on the player's
#   computer after they run the configuration gizmo. It will automamtically
#   run when the game loads and whenever the game regains focus
#   (like if the player just left to the configuration gizmo page
#   and then came back) so you really shouldn't need to call it manually.

# If you publish your game on another site that also has the gamepad mapper
#   available then you'll need to change this address
var gamepad_config_url = "https://www.newgrounds.com/portal/view/project/1765261"

# Set the "actions" arrays below to be the actions in InputMap to
#   link the buttons/axes to
var button_list = [
	{"name": "ButtonUp",    "actions": ["zoom_in"], "button": null},
	{"name": "ButtonDown",  "actions": ["ui_accept", "jump"], "button": null},
	{"name": "ButtonLeft",  "actions": ["zoom_out"], "button": null},
	{"name": "ButtonRight", "actions": ["camera_mode"], "button": null},
	
	{"name": "DUp",    "actions": ["ui_up"], "button": null},
	{"name": "DDown",  "actions": ["ui_down"], "button": null},
	{"name": "DLeft",  "actions": ["ui_left"], "button": null},
	{"name": "DRight", "actions": ["ui_right"], "button": null},
	
	{"name": "L1", "actions": ["fire_missile"], "button": null},
	{"name": "L2", "actions": ["run"], "button": null},
	{"name": "R1", "actions": ["fire_gun"], "button": null},
	{"name": "R2", "actions": ["change_target"], "button": null},
	
	{"name": "Select", "actions": ["select"], "button": null},
	{"name": "Start",  "actions": ["pause", "ui_accept"], "button": null},
	
	{"name": "LStickPress", "actions": [], "button": null},
	{"name": "RStickPress", "actions": [], "button": null},
]

var axis_list = [
	{"name": "LStickUp",    "actions": ["move_up"], "axis": null, "positive": false},
	{"name": "LStickDown",  "actions": ["move_down"], "axis": null, "positive": false},
	{"name": "LStickLeft",  "actions": ["move_left"], "axis": null, "positive": false},
	{"name": "LStickRight", "actions": ["move_right"], "axis": null, "positive": false},

	{"name": "RStickUp",    "actions": ["camera_up"], "axis": null, "positive": false},
	{"name": "RStickDown",  "actions": ["camera_down"], "axis": null, "positive": false},
	{"name": "RStickLeft",  "actions": ["camera_left"], "axis": null, "positive": false},
	{"name": "RStickRight", "actions": ["camera_right"], "axis": null, "positive": false},
]

# You don't need to modify anything from this point on

var is_loaded:bool = false
var gamepad_data = null
var gamepad_key = "gamepad_configuration"
var javascript_callback = JavaScript.create_callback(self, "_on_focus_change")

# Open the webpage with the gamepad configuration gizmo
func open_gamepad_config():
	if OS.has_feature('JavaScript'):
		JavaScript.eval("window.open(\"" + gamepad_config_url + "\");")

# Read the gamepad configuration when the game starts
func _ready():
	if OS.has_feature('JavaScript'):
		JavaScript.get_interface("document").addEventListener("visibilitychange", javascript_callback)
		read_gamepad_data()

# Read the gamepad configuration whenever the game regains focus
#   for example, after running the page with the configuration gizmo
func _on_focus_change(args):
	read_gamepad_data()

# Read the gamepad data if it has been saved
func read_gamepad_data():
	if OS.has_feature('JavaScript'):
		# First clear out all of the gamepad controls from InputMap
		for current_action in InputMap.get_actions():
			for current_input_event in InputMap.get_action_list(current_action):
				if (current_input_event is InputEventJoypadButton) or (current_input_event is InputEventJoypadMotion):
					InputMap.action_erase_event(current_action, current_input_event)
		
		# Read the gamepad configuration from LocalStorage
		gamepad_data = parse_json(JavaScript.eval("window.localStorage.getItem('" + gamepad_key + "');"))
		
		# If the gamepad data was loaded, assign the actions for each button
		if gamepad_data:
			is_loaded = true
			for current_button in button_list:
				if gamepad_data.has(current_button.name):
					if not gamepad_data[current_button.name] == null:
						var new_button = InputEventJoypadButton.new()
						new_button.button_index = gamepad_data[current_button.name]
						new_button.pressed = true
						for action_name in current_button.actions:
							InputMap.action_add_event(action_name, new_button)
			for current_axis in axis_list:
				if gamepad_data.has(current_axis.name):
					if not gamepad_data[current_axis.name] == null:
						var new_axis = InputEventJoypadMotion.new()
						new_axis.axis = abs(gamepad_data[current_axis.name])
						if gamepad_data[current_axis.name + "_direction"] > 0:
							new_axis.axis_value = 1.0
						else:
							new_axis.axis_value = -1.0
						for action_name in current_axis.actions:
							InputMap.action_add_event(action_name, new_axis)

Tags:

1

Comments

Thanks for sharing this code ⊂(•‿•⊂ )*.✧