How to manage texts ?

NeoDNeoD Posts: 59Member
edited September 2016 in GUI

Hello Godotboys ( and maybe Godotgirls ?).
I have several panels who are detected by an area attached to the player character. These panels provide strings to the player node and they are displayed in a label.
The project is for learning purpose and for the moment I use mainly assets from the official demos.



The exported var of the panels instances, (who could be a NPC also) is not very convenient to store text.
I can type a single line only.



I would rather this kind of property (don't know how to get this on my node).



 But in both case the text can be lost easily. For example if I modify the panel original scene.

How do you resolve these noob problems ? Thanks in advance and sorry for my bad english.


Answers

  • razvanc-rrazvanc-r Posts: 85Member
    I'm not entirely sure what you mean by "But in both case the text can be lost easily". If you have the text in a variable node how can it be lost easily?
  • HooniHooni Posts: 10Member
    Try Having:
    onready var text = "text here"
    And then in _ready() set the labels text property to the text. Should prevent "losing" the labels text.
    I'm on mobile that and tad busy so cant help with multiline textfield (if thats what you want).
  • NeoDNeoD Posts: 59Member
    edited September 2016
    Thanks for your answers.
    Maybe I should say " signs" instead of "panel". It's like the signs in SuperMario 64 for instance.

    The sign scene have an "export var" so I can edit the text for each instance of this scene.

    The property, into the inspector dock, is a bit limited to type texts who are longer than one sentence. And I can't delete these nodes without delete the texts. OK you will ask me " Why the hell you want to delete these instances ?"

    Don't worry about that. I'm not a game developer and I just ask about the " professional" way to store text and to access it, and how to proceed with Godot.

  • HooniHooni Posts: 10Member
    Quick reply:
    Save your texts as json files, there's good info somewhere in the docs. You will be very close then to have a localization on your game, that too can be found in the docs.
  • NeoDNeoD Posts: 59Member
    edited September 2016
    I made some tests to fill a dictionnary with a json file. Like this.
    var PanelText = {}

    func _ready():
    PanelText.parse_json("res://Ress/AdventureRess/panelText")


    It throw an error in the debugger : " Error parsing JSON: Expected ' { ' at line 0 "

    Here is the file

    {
    "Panel_1": "Hello"
    }
     I don't understand what I'm doing wrong. Maybe because I'm a complete noob with this stuff.


  • UnnamedUnnamed Posts: 9Member
    You have to load the text from the file first and pass the text as argument to parse_json.
    There is an example for loading a text file here http://docs.godotengine.org/en/latest/classes/class_file.html#description .
  • NeoDNeoD Posts: 59Member
    edited September 2016
    I'm sorry to be annoying. I'm lost into the File api.

    func save(content):
        var file = File.new()
        file.open("user://save_game.dat", file.WRITE)
        file.store_string(content)
        file.close()
    
    func load():
        var file = File.new()
        file.open("user://save_game.dat", file.READ)
        var content = file.get_as_text()
        file.close()
        return content

    parse_json() need the string from the json as argument ?
    store_string() wait for a string but it's the thing I want to pull from the json.
    get_as_text() is for files who contain something inside. How I should start please ?


  • NeoDNeoD Posts: 59Member

    @NeoD said:
    I'm sorry to be annoying. I'm lost into the File api. func save(content):
    var file = File.new()
    file.open("user://save_game.dat", file.WRITE)
    file.store_string(content)
    file.close()

    func load():
    var file = File.new()
    file.open("user://save_game.dat", file.READ)
    var content = file.get_as_text()
    file.close()
    return content

    parse_json() need the string from the json as argument ?
    store_string() wait for a string but it's the thing I want to pull from the json.
    get_as_text() is for files who contain something inside. How I should start please ?

    I replied to myself on Q&A.

    https://godotengine.org/qa/8291/how-to-parse-a-json-file-i-wrote-myself

  • NeoDNeoD Posts: 59Member

    I clicked on answered by mistake. It still unresolved.

    extends Node
    
    var dict = {}
    
    
    func _ready():
     var file = File.new()
     file.open("res://Ress/panelTextn2.json", file.READ)
     var text = file.get_as_text()
     dict.parse_json(text)
     file.close()
     get_node("Area/Panel/Label").set_text(dict["text_1"])
    

    This code works only with the editor. When exported the app doesn't found the json.
    As you can see the script doesn't create the json file because I want to edit text directly into the file during the game developement.

  • HooniHooni Posts: 10Member
    Try going back to file.open("user://file_path") You must be close to getting it right. Good luck!
  • Shin-NiLShin-NiL Posts: 71Moderator

    Godot doesn't export .json files by default, you need to add a filter for that on "Export" -> "Resources" tab.

  • notonfirenotonfire Posts: 1Member
    Answer ✓

    If you want to keep the text in the scene itself, you can export the variable as a multiline string:

    export(String, MULTILINE) var text
    

    Then you can edit this variable in the editor much like for a label node.

Sign In or Register to comment.