File.get_as_text() is returning garbage sometimes, why?

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

Ryan1Dialogue.tres is a TextFile that says “Success”

I get the file path, then I

file.open(file_path, 1)
file_text = file.get_as_text()

print_debug(file_path)
print_debug(file.is_open())
print_debug(file_text)

This prints

res://Dialogues/Ryan1Dialogue.tres
   At: res://ExampleSpeaker.gd:23:loadfile()
True
   At: res://ExampleSpeaker.gd:24:loadfile()
[gd_resource type="TextFile" format=2]

[resource]


   At: res://ExampleSpeaker.gd:25:loadfile()

But after changing anything in Ryan1Dialogue.tres, changing it back to “Success” and saving it again I instead get

res://Dialogues/Ryan1Dialogue.tres
   At: res://ExampleSpeaker.gd:23:loadfile()
True
   At: res://ExampleSpeaker.gd:24:loadfile()
Success
   At: res://ExampleSpeaker.gd:25:loadfile()

This restarts from the top, giving me garbage, everytime i open godot.
it also happens after exporting it.

Why is this happening?

I’m currently getting around this by using .txt files, which work as I expect, but I’d prefer to use Godot’s TextFiles so they show up in the file system.

Troll_HunterX | 2019-04-12 20:11

.tres files need to follow a specific format (with sections and keys in an INI-like format); it’s not going to work if you just write Success in a .tres file.

Also, as of Godot 3.1, you can open arbitrary text files in the script editor, just set the filter to All Files (*) in the open dialog.

Calinou | 2019-04-12 21:36