🔺
364 Words

Infinite (local) Multiplayer In Godot?!

Okay to explain why someone would EVER want infinite players… I have six siblings and finding games that all of us can play is hard.

Also I have never been satisfied with multiplayer implementations in godot so I made my own!

Theory

The actions have a name and the input associated with them.

1
2
3
Action name
 - Input 1
 - Input 2

The Basic idea is to create a reference InputEvent and copy and replace some data to a new InputEvent.

1
2
Player_Left
 - Left_Stick_Left, All Devices

Then you reference it and replace data.

1
2
Player_Left_1
 - Left_Stick_Left, Device 0

Now we just need to implement that.

Making Inputs

Add your new actions to input map through Project Settings > InputMap.

Just make sure to put at the start of each action name ‘player_’

Now when a player presses the join action you want to use InputMap.get_actions and loop through the result checking for ‘player_’

Store any results with the player_ tag in a array.

Copying the data

Unfortunately there isn’t a duplicate command…

So we just need to put the data in the right place.

Start by getting the player number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var players : Array

func _input(event):
	
	if event is InputEventJoypadButton:
		
		if event.button_index == JOY_SONY_X and !PlayerData.players.has(event.device):
				
			players.append(event.device)
		
		if event.button_index == JOY_SONY_CIRCLE and PlayerData.players.has(event.device):
			
			players.remove(event.device)
			
	if Input.is_action_just_pressed("start"):
		make_actions()

Next Loop through your base action array and for each input do the following.

(For keyboards the ‘button_index’ is called scancode.)

Then put the data where it goes.

1
2
3
4
5
6
7
8
9
10
if base_action is InputEventJoypadButton:
	InputMap.add_action(action_name)
	
	var joypad_button = InputEventJoypadButton.new()
	
	joypad_button.device = device
	joypad_button.button_index = button_index
	
	InputMap.action_add_event(action_name, jopad_button)
	

(you have to do the Joyaxis seperately)

That’s it! thanks for reading.

Some good resources is the documentation for InputEvent, and Pigdevs video on multiplayer.