Enter name as in "Legend of Zelda"

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

Hi!
I’d like to create an “enter your name” screen as seen in Legend of Zelda.
This name would be composed of 8 characters.
For each character I have set a label (Lab 0 for char 0, Lab 1 for char 1… up to Lab 7 for char 7).
So for the moment I can populate the first label but cannot go further.
I imagine this involves some kind of looping, checking what is the last character choosen and then going to the next label but cannot code it.

Any ideas?

Thanks!

Why not have just one label with the whole name in it? Any time a character button is pressed just add to the label.text.

aXu_AP | 2022-12-09 21:41

:bust_in_silhouette: Reply From: doctorbllk

For this, you could create an empty array as a variable. Then use the array .append() method to add a character to that array, and display each character in your display.
A very simple example is:

var my_array: Array = []
func _on_button_pressed():
     my_array.append("A")

With a button to call the __on_button_pressed() func, each time that button is pressed, it will add “A” to this list, i.e. ["A","A","A"]. You can then set a label to slice for each letter in the array, i.e. my_array[0] will slice for the first “A”.
If you set the .append() method to append based on variable keyboard inputs, or a value returned on a specific button pressed, you can then ensure that any character you pass in to .append() will add to the list next.

For other methods an Array can use in Godot (such as “.pop_back()” which acts like a delete method), click the “Search Help” button on the top right of the script editor view and search for the “Array” Class.

Hi! Thank you for your reply.

I think my question was not clear enough. I also have an image but I am not able to upload it…
I have created a scene with a bunch of characters and an arrow. The player moves this arrow. When he presses “shoot” the function checks the arrow position and put the letter in the first label.
Then the player moves again the arrow, shots and put a letter in the second label.
I have attached the code.

func _process(delta):
get_input()
position += velocity
if Input.is_action_just_pressed("shoot") and $"../1".text == "":
	if position.x == 55 and position.y == 114:
		$"../1".text = "A"
	if position.x == 87 and position.y == 114:
		$"../1".text = "B"
	if position.x == 119 and position.y == 114:
		$"../1".text = "C"
	if position.x == 151 and position.y == 114:
		$"../1".text = "D"
	if position.x == 183 and position.y == 114:
		$"../1".text = "E"

elif Input.is_action_just_pressed("shoot") and $"../2".text == "":
	if position.x == 55 and position.y == 114:
		$"../2".text = "A"
	if position.x == 87 and position.y == 114:
		$"../2".text = "B"
	if position.x == 119 and position.y == 114:
		$"../2".text = "C"
	if position.x == 151 and position.y == 114:
		$"../2".text = "D"
	if position.x == 183 and position.y == 114:
		$"../2".text = "E"

elif Input.is_action_just_pressed("shoot") and $"../3".text == "":
	if position.x == 55 and position.y == 114:
		$"../3".text = "A"
	if position.x == 87 and position.y == 114:
		$"../3".text = "B"
	if position.x == 119 and position.y == 114:
		$"../3".text = "C"
	if position.x == 151 and position.y == 114:
		$"../3".text = "D"
	if position.x == 183 and position.y == 114:
		$"../3".text = "E"

elif Input.is_action_just_pressed("shoot") and $"../4".text == "":
	if position.x == 55 and position.y == 114:
		$"../4".text = "A"
	if position.x == 87 and position.y == 114:
		$"../4".text = "B"
	if position.x == 119 and position.y == 114:
		$"../4".text = "C"
	if position.x == 151 and position.y == 114:
		$"../4".text = "D"
	if position.x == 183 and position.y == 114:
		$"../4".text = "E"

As you can see I need to repeat that 21 times (the letters of the alphabet I’d like to use) and also check the x and y position for the whole alphabet too.
The suitable solution is a loop but I am not able to code it.

Pancotto | 2022-12-10 15:05

This appears to be quite a convoluted way to manage this solution. Read over my original response above and consider how you can use an empty list to add new letters to the list without having to repeat what it is that actually sets that character’s place.

For example:

var list = []
if Player.shoot(Variable_Letter):
     list.append(variable_letter)
     $text/1 = list[0]

Yes there are many ways you can do this, but it seems like you may still be learning! Take some time to understand some more of the fundamentals of things like arrays, and the methods available to you in a range of basic properties.

doctorbllk | 2022-12-14 00:35