Is there a way to do this better

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

So I’ve been working on a menu system for selecting levels in my game controlled with WASD. (As in you use WASD to navigate through the menu to find the level you want to play)

Just a little bit of an explanation of the system I have in place; I have a Vector2 variable that changes depending on what button you press, then each level has a Vector2 value assigned to themselves.

My point is that the way of detecting the player’s input doesn’t seem very optimized and I wanted to see if anybody could help improve it somehow.

Actual detection: (It runs in the _input() function)

func change_selected_minigame_pos():
    if Input.is_action_just_pressed("right"):
		selected_minigame_pos.x += 1
	if Input.is_action_just_pressed("left"):
		selected_minigame_pos.x -= 1
	if Input.is_action_just_pressed("down"):
		selected_minigame_pos.y += 1
	if Input.is_action_just_pressed("up"):
		selected_minigame_pos.y -= 1

Other things that could potentially be useful:

func _process(delta):

    if Input.is_action_just_pressed("space"):
	print(selected_minigame.minigame_data.minigame_description)

 limit_movement()

 for i in minigame_container.get_children():
	 if i is MinigamePromptComponent:
	 	 if selected_minigame_pos == i.prompt_position:
			 i.select()
			 selected_minigame = i
		 else:
			 i.deselect()

This looks fine, really. But you could try using the built-in focus system if you are using any Control-derived nodes for the menu, which would not even require coding the selection inputs you made. Check this out:

Keyboard/Controller Navigation and Focus — Godot Engine (stable) documentation in English

Ox0zOwra | 2023-06-07 22:56

:bust_in_silhouette: Reply From: godot_dev_

It would appear that rapidly pressing the same button will infinitely increase/decrease an element of one of the vector (e.g. x if you press right multiple times), meaning that if you press the opposing direction, your menu system won’t appear to address this.

For example, I you press

  1. Right
  2. Right
  3. Right
  4. Left

Given you logic, the selected minigame will remain the minigame tied to the right button. You would have to press Left 3 more times in the above example to select the left minigame.
I recommend just setting the value in the vector instead of decreasing/increasing it. For example, selected_minigame_pos.y = 1 instead of selected_minigame_pos.y += 1.

Your logic otherwise makes sense