0 votes

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
in Engine by (12 points)

2 Answers

0 votes

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

by (66 points)
0 votes

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.

by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.