Player specific ui menus in two player game

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

In my two player project, I want to have two separate upgrade screens, one for each player. The menus are up at the same time, and a player should only be able to interact with their own respective menu. I’m having a lot of difficulties making it happen since only one item can be in focus at a time (at least that I’m aware of).

:bust_in_silhouette: Reply From: njamster

Just set the “Focus Mode” to “None” and do the input handling yourself via the _input- or _unhandled_input-functions covered in greater detail here.

Here’s a very simple example tree:

- GUI (Node-Type: Control)
  -  ButtonA (Node-Type: Button, Focus Mode = None)
  -  ButtonB (Node-Type: Button, Focus Mode = None)

Then your GUI.gd-script could look like this:

extends Control

func _input(event):
	if event is InputEventKey and event.pressed:
		match event.scancode:
			KEY_A:
				$ButtonA.emit_signal("pressed")
			KEY_B:
				$ButtonB.emit_signal("pressed")

func _on_ButtonA_pressed() -> void:
	print("A")

func _on_ButtonB_pressed() -> void:
	print("B")

When you now run the game, you can press the A-key to activate ButtonA and print “A”, the B-key to activate ButtonB and print “B” or both at the same time.

I hadn’t thought about assigning each button it’s own key, I was focused (hah) on finding some way on being able to switch which button is being selected with a d-pad/joystick.

This works though! Might have to assign a key to toggle multiple menu screens, or to toggle which set of buttons is accepting requests at a time.

TheJollyReaper | 2020-06-22 16:16