Assigning Different Controller IDs to Each Player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KuroDev

I am trying to get my menu to assign a controller to one player if they press the start button. The functionality works if I press start on controller 0 first. If I press start on controller 1 first, it assigns it to both players. How do I fix this?

func _input(event):
if not P1Set and  not P2Set:
	if event is InputEventJoypadButton and event.button_index == JOY_START:
			P1Set = true
			P1InputType = InputEventJoypadButton
			P1InputDevice == event.device
			$Panel/Player1Join.visible = false
			$Panel/Player1CharacterName.visible = true
			print("Player 1 is Ready to Start. Using device:",  event.device)
	elif event is InputEventKey and event.scancode == KEY_ENTER:
			P1Set = true
			P1InputType = InputEventKey
			$Panel/Player1Join.visible = false
			$Panel/Player1CharacterName.visible = true
			print("Player 1 is Ready to Start. Using device:", event.device)
if P1Set and  not P2Set:
	if event is InputEventJoypadButton and event.button_index == JOY_START and event.device != P1InputDevice:
			P2Set = true
			P2InputType = InputEventJoypadButton
			P2InputDevice == event.device
			$Panel/Player2Join.visible = false
			$Panel/Player2CharacterName.visible = true
			print("Player 2 is Ready to Start. Using device:", event.device)
	elif event is InputEventKey and event.scancode == KEY_ENTER and P1InputType == InputEventJoypadButton:
			P2Set = true
			P2InputType = InputEventKey
			$Panel/Player2Join.visible = false
			$Panel/Player2CharacterName.visible = true
			print("Player 2 is Ready to Start. Using device:", event.device)
if P1Set and P2Set:
	print("Both Players are Ready to Start!")
if event is InputEventKey:
	if event.scancode == KEY_ESCAPE:
		get_tree().quit()
if event is InputEventJoypadButton:
	if event.button_index == JOY_SELECT:
		get_tree().quit()
:bust_in_silhouette: Reply From: Merlin1846

First, don’t use scan codes use the built-in input manager available through the menu on the top left. Also when assigning the inputs set player 1 to device 0 p2 to device 1 and so on.

If you had an unknown number of players scan codes might make more sense but seeing as you’re using if’s and not a loop I doubt that’s the case.

Merlin1846 | 2022-12-04 04:34

Makes sense. My main issue is that rather than having player 1 always be device 0 I want it to be able to have any connected controller assigned to it. For instance if I had 4 controllers connected then it could be assigned any of 0-3 as the ID.

KuroDev | 2022-12-04 05:49

I did the same thing in one of my old projects.

  1. Make all player controllers unassigned.
  2. When any controllers button is hit assign that controller to the first unassigned player. What this means is the first player to hit a button will be player one. You may need a lobby like I did in order for this to be usable.

Merlin1846 | 2022-12-04 18:25