Local multiplayer device managing

Godot Version

4.5

Question

Hi I am trying to create a local multiplayer game for two players. Following tutorials I was able to use a input map to detect both 1 and 2 devices separatelly. My Issue is that I can’t assure that the first and second devices will be allways the joypads, maybe it is a second keyboard or one joypad that you don’t want to use. I want to adjust the device that something is readed from, or to be able to say which player has done what. I already have a player selection scene that could be used to tell apart both players, but I can’t find an easy way to do it consistently without it being a pain in the ass.

Acording to some AI chatbots the best way is not to use input mapping and use specific input events and add this code to the main gamemanager scene

.
func get_player_from_event(event: InputEvent) → int:
for i in range(player_devices.size()):
if player_devices[i] == event.device:
return i
return -1

func get_device_for_player(player: int) → int:
if player < player_devices.size():
return player_devices[player]
return -1

I have been running through the documentation but I couldn’t find a better way to do that.

LLMs are not helpful in learning how to use Godot. They will lead you wrong more often than not. Case in point, this is stupid advice.

Make two sets of inputs and say what device they come from. Done.

Also, FYI you’re not making a local multiplayer game. You’re making a Co-Op game. Co-Op games are played on the same device with multiple sets of input.

keyboards will use device 0 which will overlap with the first joypad. I usually make a set of “Base” controls that are duplicated once the game runs, the base controls have inputs for every device and the duplicates then remove any non-applicable device for a given player. If player 0 is using a keyboard the duplicate will delete all joypad button and joypad axis events, finally rename each input P0_Forward for example. Joypads do the same deleting all keyboard inputs and setting the device to match. Player scripts can use "P%d_Forward" % my_player_id to get their forward action.

2 Likes

Sorry, I believe you’re mistaken, because that’s not what co-op games are. It means cooperative play, and the number of input devices or screens is irrelevant. There are very popular coop games played continents apart, like Peak or Split Fiction.

You’re thinking split screen games, no ?

1 Like

Yes, I think you’re right. I guess I’d call it a Co-Op Split Screen game.

Damn I feel old.

1 Like