Save in variable a text string coming from the keyboard

juangomezjuangomez Posts: 3Member

I'm new to godot and I'm liking it, but
I'm going crazy, I've been looking for two days for how to save a text string, such as my name and surname, entering them from the keyboard,
I can't find anything in the docs or videos

Help ...

Thanks in advance

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,641Admin

    Welcome to the forums @juangomez!

    Are you having trouble getting input from the keyboard, saving the returned string to a file, both? Can you explain a bit more about what you are trying to achieve and the difficulties you are facing? Its hard to know what to suggest without knowing where the issue is.

    As far as saving goes, this page on the documentation covers how to save a dictionary to JSON, and then save that to a file in user://. For retrieving text input, I would suggest using a LineEdit node, and then getting the string using something like save_dictionary["name"] = $NameLineEditNodeHere.text.

  • juangomezjuangomez Posts: 3Member

    Basically I'm looking for a way to fill an array with strings entered from the keyboard.

    I only know the function InputEventKey, read character by character, detect intro and store

  • TwistedTwiglegTwistedTwigleg Posts: 2,641Admin

    How are you retrieving the input? Are you using the _input function?

  • juangomezjuangomez Posts: 3Member
    edited June 27
    extends Label
    var nombre = ""
    
    func _input(event):
              if event is InputEventKey and event.pressed:
                        if event.scancode >= KEY_A && event.scancode <= KEY_Z:
                                  nombre += event.as_text()
                                  set_text (nombre)
                        if event.scancode == KEY_SPACE:
                                  nombre += " "
                                  set_text (nombre)
                        if event.scancode == KEY_BACKSPACE:
                                  nombre = nombre.left(nombre.length()-1)
                                  set_text (nombre)
                        if event.scancode == KEY_ENTER:
                                  print(nombre)
    
  • TwistedTwiglegTwistedTwigleg Posts: 2,641Admin
    edited June 27

    Hmm, that may be the easiest way to get a string from the _input function in Godot, at least without using a UI element. You may be able to simplify the code a bit though (untested):

    extends Label
    var nombre = ""
    
    func _input(event):
        if event is InputEventKey and event.pressed:
            # handle special key codes
            if event.scancode == KEY_BACKSPACE:
                nombre = nombre.left(nombre.length()-1)
            elif event.scancode == KEY_ENTER:
                print (nombre)
            else:
                # all other keycodes, just add them to the string!
                nombre += OS.get_scancode_string(event.scancode)
            # update the label's text
            set_text(nombre)
    

    Edit: Though as a I mentioned, if you just need to get text input, using a node like the LineEdit node would be great for this, as it basically does what the script is doing, but handles it all internally. It also has code so only the LineEdit that is selected will have text appended to it, unlike the script which will grab all input events. That said, if there is a reason that you cannot use a UI node, then something like the script above is probably the best way to handle it.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file