How to add 1 to a variable everytime a user presses the enter key.

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

Hey everyone,

As expected, I’m new to Godot and I’ve encountered a problem that has gotten me stuck for hours.

In my if statement I want to add one to my variable “enter” each time my user presses the enter key.

What actually occurs is that “enter” is set to one and updates as one consistently instead of adding one.

What gives?

Thank you in advance.

Code:

export (int) var enterKeyCount = 0
var studentName = ""; 

func _sys():
var enter
if Input.is_action_just_pressed('ui_accept'):
	enter = enterKeyCount + 1
	print(enter)
	
	if (enter == 1):
		studentName = get_node("../LineEdit").get_text()
		print(studentName)
		
	
	
	pass
:bust_in_silhouette: Reply From: Salvakiya

everytime _sys() is being called you are defining a new variable called enter and set it to the value of enterKeyCount+1 but you never increase the value of enterKeyCount. enterKeyCount needs to be +=1

:bust_in_silhouette: Reply From: Phischermen

Try this instead:

export (int) var enterKeyCount = 0
var enter;
var studentName = ""; 

func _sys():
if Input.is_action_just_pressed('ui_accept'):
    enter = enterKeyCount + 1
    print(enter)

    if (enter == 1):
        studentName = get_node("../LineEdit").get_text()
        print(studentName)



    pass

The reason why your code was not working was because your variable “enter” was local to the function “_sys().” Everytime you call _sys(), “enter” is reset to 0.