Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextFiles have no ResourceFormatLoader and other related Inspector difficulties. #21787

Closed
willnationsdev opened this issue Sep 5, 2018 · 9 comments · Fixed by #53025
Closed

Comments

@willnationsdev
Copy link
Contributor

willnationsdev commented Sep 5, 2018

Godot version:
Godot 3.1

Issue description:
I wanted to use TextFile to create a script for CSVs (with file extension .csv.txt or .csv.tres or something to avoid the translation assumption). However, there is no way to treat a TextFile as a Resource in the Inspector without creating an error. There are a variety of problems that occur.

  1. Can't open it via the inspector (no loader found for resource).
  2. Can't create one from within the Inspector and then save it (if you save a *.txt, you get an "error saving resource!" alert and it fails).

If I can't load it into the Inspector, edit a Resource-compatible property (like the script property), and then save it from the Inspector, it doesn't really "work" right as a Resource type.

Steps to reproduce:

  1. Create a data.csv.txt file in your file system.
  2. Click the "open" button in the Inspector.
  3. No option for text files exists in the available file types for filtering visibility (issue).
  4. If you select All Files and then open the *.txt file, you'll get a No loader found for resource: res://data.csv.txt error.

OR

  1. Click new file in Inspector.
  2. Select TextFile.
  3. Select "Save" or "Save As..." from the tools dropdown in the Inspector.
  4. Save with a *.txt extension.
  5. An alert pops up saying it failed.
@willnationsdev
Copy link
Contributor Author

willnationsdev commented Sep 5, 2018

cc @Paulb23 thoughts?

@willnationsdev
Copy link
Contributor Author

Now that I think about it, there wouldn't really be a way to store Resource-related properties on the TextFile itself. I think you'd have to have a .import file attached to it (or something) just to keep track of that information.

If I can't load it into the Inspector, edit a Resource-compatible property (like the script property), and then save it from the Inspector, it doesn't really "work" right as a Resource type.

We also can't really do this from GDScript or VisualScript, but those are still "resources", so it's not a hard-and-fast rule that you have to be able to attach a script to all resources.

Hmm....just wondering how one might script ones own data types, with a custom structure, by manipulating the TextFile feature...

@willnationsdev
Copy link
Contributor Author

I'm thinking that in order to do something like this, perhaps with a .tsv instead to avoid the confusion, you'd have to create a script extending Resource with name TSVFile and then have that .tres file point to a .tsv in your project, providing utilities for them.

Anyway, that allows me to fulfill my target use-case, but I guess I'll leave this Issue up in case anyone wants to discuss something of this sort as a possible feature for a future version of Godot (since extending TextFile features to enable other file types is way beyond the scope of this Issue, understanding the implications of it - definitely not for 3.1 then).

@Paulb23
Copy link
Member

Paulb23 commented Sep 6, 2018

This is expected, the TextFile is not a "Resource" by traditional means as it is directly and only handled by the ScriptEditorPlugin, thus will not work through normal means aka ResourceLoader.

Currently the only way, to edit, save and create TextFile's is via the script editor menus. I guess the fix is to add more redirects into ScriptEditorPlugin in and around the editor where appropriate.

@aaronfranke
Copy link
Member

Adding the "bug" label since saving these files with the default extension of tres causes the editor to throw errors as it doesn't understand the file, as shown in #37735.

@FeralBytes
Copy link
Contributor

This is not closed and needs to be re-opened. Or I can create a new issue if need be. You still can not load a txt file which is the title of this issue. #53025 simply adds more Editor specific support for txt files but it does not allow you to for instance:

var txt_file = load("res://my_text_file.txt")

@aaronfranke
Copy link
Member

aaronfranke commented Sep 5, 2023

@FeralBytes You would need to open a proposal to suggest further changes. It's not really clear what load is expected to return. It's not a resource, just a text file. If you want to load a text file you can use FileAccess.

@FeralBytes
Copy link
Contributor

FeralBytes commented Sep 5, 2023

@aaronfranke thanks for the quick response. I did try that, but due too Issue #24222 which both you and I are also involved in. What happens is the file does not exist on a export file. I did add it to the files that the export should contain but I think it is probably the old .txt.import file issue.
The below code works in the editor but fails on export.

var dir = DirAccess.open("res://scenes/InGame/A/1/")
    if dir:
        dir.list_dir_begin()
        var file_name = dir.get_next()
        while file_name != "":
            if dir.current_is_dir():
                print("Found directory: " + file_name)
            else:
                print("Found file: " + file_name)
            file_name = dir.get_next()
    else:
        print("An error occurred when trying to access the path.")
    if FileAccess.file_exists("res://scenes/InGame/A/1/shader.txt"):
        print("here 1")
        var file = FileAccess.open("res://scenes/InGame/A/1/shader.txt", 
            FileAccess.READ_WRITE
        )
        var content = file.get_as_text()

Resulting Output.

Found file: first_shader.tres.remap
Found file: first_shader.tscn.remap
Found file: shader.png.import
Found file: shader.txt
here 1
SCRIPT ERROR: Attempt to call function 'get_as_text' in base 'null instance' on a null instance.
          at: deferred_setup (res://scenes/MainMenu/UserInterface.tscn::GDScript_hcxt8:39)

@FeralBytes
Copy link
Contributor

I don't even know what to say. I printed the var file and suddenly it is working. Even on export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants