This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

Godot has a resource type named TextFile. It has no docs but i want to read the content of a TextFile in code. Is this possible?

And if you can not manipulate it with code: Why?

in Engine by (29 points)

1 Answer

+2 votes

I really wonder what this TextFile for if it has no property Oo
I would have assumed you could use this import for .txt files in your project, but it's still completely useless. Nothing is exposed:
So I think this resource was created just to allow the Godot Editor to open files as plain text in the script editor, so it's maybe not intended for use in a game:

Anyways, you can load a file as text using a function like this:

func load_text_file(path):
    var f =
    var err =, File.READ)
    if err != OK:
        printerr("Could not open file, error code ", err)
        return ""
    var text = f.get_as_text()
    return text

func save_text_file(text, path):
    var f =
    var err =, File.WRITE)
    if err != OK:
        printerr("Could not write file, error code ", err)

Load from game resources:

var text = load_text_file("res://path/to/file.txt")

Load from player savegame:

var text = load_text_file("user://path/to/file.txt")

Check this out for more info about reading and writing to files:

by (29,510 points)
edited by

I would be useful to know if there's any technical or philosophical reason for not properly exposing TextFile in GDScript. I'm coming from a Unity background, where the TextAsset provides the functionality TextFile would, and I've found it an incredibly useful for creating textual resources (eg dialogue) and things like lookup tables (.csv for purposes other than translation). Obviously it's possible to just read the file in from a res:// path, but given that the resource type already exists, it would be nice not to have to do that.

I doubt it has any philosophical reason. What's more likely is that someone, some day, had a problem and wanted to be able to open and edit text files within the script editor. Then someone made a PR which does only that, was merged, and here we go (

If we want to expose its methods, I guess another PR should be made...

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.