Hello. I have a Project where i load txt-files that have the same name as the variable txtvar, so is txtvar 1 then it loads 1.txt as variable stext, then the richtextlabel called story outputs stext. This works fine. Now i want, that i can use variables in my txt-files, but it takes the whole file as string and dont look for variables. How can i do that?

Code Snippet:

    func _process(delta):
    		var stext = load_text_file("res://txt/u01/"+str(txtvar)+".txt")
    		get_node("story").bbcode_text = stext
    
    func load_text_file(path):
    	var f = File.new()
    	var err = f.open(path, File.READ)
    	if err != OK:
    		printerr("Could not open file, error code ", err)
    		return ""
    	var stext = f.get_as_text()
    	f.close()
    	return stext

It's explained well here: https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html

the example in short: you open a file, then you could store your variables in a dictionary and serialize it in JSON format.

var my_dictionary = {
		"position_x" : position.x,
		"position_y" : position.y
}
f.store_line(to_json(my_dictionary))

then to parse, which is the part you want I think:

f.open("res://txt/u01/"+str(txtvar)+".txt", File.READ)
while f.get_position() < f.get_len():
    var my_loaded_dictionary = parse_json(f.get_line()) 
    # do stuff with your loaded dictionary
    my_object.position = Vector2(my_loaded_dictionary["position_x"], my_loaded_dictionary["position_y"])
f.close()
3 years later