Why would pop_front() work but not pop_back()? I cant get it to work to remove last element in array.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PampoenKoekie
func _input(event):
#  Check the keyboard typing and append it to Global variable typedWord
if event is InputEventKey:
	if event.pressed:
		var typing = char(event.unicode).to_upper()
		Globals.typedWordArray.append(typing)
		
	if event.is_action_pressed("ui_text_backspace"):
		Globals.typedWordArray.pop_back()
		print (Globals.typedWordArray)

Edited to format the code. Use the Code Sample button.

Also, please don’t post screenshots of your code. It prevents people from testing your code or even reading it. Post the code as text: paste it in the text box, select it, and click the Code Sample button (it needs to be indented by 1 level to be formatted).

pop_back will only do something if the array is not empty, assuming your if is entered.
What problem are you actually encountering? What do you mean by “pop_back not working”?

Zylann | 2023-07-06 14:04

Thank you for you reply. The array is not empty, I type a word and show what im typing in a label, all works great. Then when I want to erase the last element by pressing backspace, I want to use pop_back(), but it just does nothing, but if I replace it with pop_front() it works as I want but only in reverse. I print the array to console to see what it does and it writes to to the array fine, it reads from the array fine and when I use pop_front(), the array shows removing the first element in console, but as soon as I change it to pop_back(), nothing happens. Thats why Im not understanding. Even if I use clear() method on the array it works.

I could make a video clip aswell showing it all, but thank you for your help.

PampoenKoekie | 2023-07-06 14:13

Thanks for your help, Im new here so still learning.

PampoenKoekie | 2023-07-06 14:20

I don’t know what’s up with this tbh. Did you try testing pop_back in a very simple project or scene to see if it works as intented?
I see no reason for pop_front to work and not pop_back
What kind of array is typedWordArray?

Zylann | 2023-07-06 14:21

Yes, its really weird, all methods work except pop_back(). I will try your advice on a test project and see what’s going on… typedWordArray is a Global String array.

#  Array for seperate letters being typed
   var typedWordArray = []

PampoenKoekie | 2023-07-06 14:30

:bust_in_silhouette: Reply From: Zylann

I think i see what’s going on…

Your code always appends an element to the array when a key is pressed, including Backspace. It enters both if blocks. So when you press Backspace, you try to convert it to unicode (which gives you an empty string I presume) and then you do pop_back, which ends up removing that empty string and the result is as if nothing changed.

You need to not append to the array if it’s not a character key.

Amazing! Thank you ever so much! That’s exactly what it does, thanks for noting that. I appreciate your time and help, have a wonderful day and keep well, maybe we meet again soon.

PampoenKoekie | 2023-07-06 14:55

Just a follow-up, I have gotten it to work wonderfully thanks to your help.

func _input(event):
#  Check the keyboard typing and append it to Global variable typedWord
if event is InputEventKey:
	if event.is_pressed() and not event.is_action_pressed("ui_text_backspace"):
		var typing = char(event.unicode).to_upper()
		Globals.typedWordArray.append(typing)

if event.is_action_pressed("ui_text_backspace"):
	Globals.typedWordArray.pop_back()
	print (Globals.typedWordArray)

PampoenKoekie | 2023-07-06 15:35

Careful, if the user presses any other key such as arrows, Ctrl, Shift, Alt or Fkeys you will also have the same problem.

Zylann | 2023-07-06 15:50