old code to gdscript2.0

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

need help converting an old script to gdscript2.0

here it is:

@export_file("*.json") var dialogue_file


func load_dialogue():
var file = File.new()
if file.file_exists(dialogue_file):
	file.open(dialogue_file, file.read)
    return parse_json(file.get_as_text())
:bust_in_silhouette: Reply From: GlitchedCode

I am just going to use a placeholder path and file called myfile.json in the user directory, adjust this for your file and path.

var MyDialogue
    func load_dialogue():
        if FileAccess.file_exists(“user://myfile.json”)
            var f = FileAccess.open(“user://myfile.json”, FileAccess.READ)
            var j = JSON.new()
            var parse_error = j.parse(f.get_as_text())
            if parse_error == 0:
                MyDialogue = j.get_data()

The above code is an example of checking if the file exists so we dont get an error, open it and read the contents. Use a JSON object to parse the data, check if there were any errors parsing the data and if not, get the data and assign it to the MyDialogue variable.