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

Exposing text_file.cpp as a Resource with ResourceFormatLoaderText #9077

Closed
JNatheriver opened this issue Feb 12, 2024 · 10 comments
Closed

Exposing text_file.cpp as a Resource with ResourceFormatLoaderText #9077

JNatheriver opened this issue Feb 12, 2024 · 10 comments

Comments

@JNatheriver
Copy link

Describe the project you are working on

Creating a typing game requiring runtime loading of numerous text files.

Describe the problem or limitation you are having in your project

Unable to utilize FileAccess, load, or preload for reading text files in an exported project due to godotengine/godot#21787 (comment)

FileAccess docs:

Note: To access project resources once exported, it is recommended to use ResourceLoader instead of FileAccess, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Expose text_file.cpp as a Resource and implement a ResourceFormatLoaderText to allow straightforward loading of text files using ResourceLoader. This enhancement will simplify text file loading, overcoming the current limitations.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

I've devised a solution to streamline the process of accessing text data within Godot. Initially, I convert text files to JSON format using an external tool. Subsequently, within Godot, the JSON files are loaded using ResourceLoader.load(json_path). However, the current method of converting the JSON data into an array to extract the text from the original text file is proving to be inefficient

Convert Text files to Json in an external tool → Code reads that Json with ResourceLoader.load(json_path) → Custom function extracts the original text from the JSON Resource

Additionally, with the proposed enhancement of ResourceLoaderTextFile, the workflow becomes even more straightforward:

Read textFiles with ResourceLoader.load(txt_path) → text_file.get_text() DONE!!!!!

If this enhancement will not be used often, can it be worked around with a few lines of script?

As previously mentioned, one could bypass the need for this enhancement by converting the text file to an already implemented resource format like JSON. However, this approach is a very slow workflow and necessitates the creation of a custom function to extract the text from the JSON file.

Is there a reason why this should be core and not an add-on in the asset library?

The text_file.cpp already exists as a Resource within Godot, albeit not yet exposed in the engine. Hence, the integration of this enhancement merely involves exposing it and implementing the ResourceFormatLoader abstract class.

@KoBeWi
Copy link
Member

KoBeWi commented Feb 12, 2024

That FileAccess note only applies to resources. Since text files are not resources, they can be safely loaded using the FileAccess methods.

@AThousandShips
Copy link
Member

It'd have to have another name as ResourceFormatLoaderText is already taken, but as explained above this isn't needed, plain text files can and IMO should be accessed by FileAccess

@JNatheriver
Copy link
Author

JNatheriver commented Feb 12, 2024

I appreciate your response and insight into the issue. However, the problem persists even when attempting to load text files using FileAccess, like in issues: godotengine/godot#37646 and godotengine/godot#56737
For example this code works in the editor:

func _ready():
	var text_file = FileAccess.open("res://Text/Hello.txt",FileAccess.READ)
	print(text_file.get_as_text())

But the exported project throws an error indicating a null instance when attempting to call functions such as get_as_text().

SCRIPT ERROR: Attempt to call function 'get_as_text' in base 'null instance' on a null instance.
          at: _ready (res://Node.gd:5)

I proposed the use of ResourceLoader because ResourceLoader does handle these issues under the hood
https://github.com/godotengine/godot/blob/f317cc713aa4dbcee2efa10db764473a56680be7/core/io/resource_loader.cpp#L242C5-L242C13 and that TextFile already inherits from Resource https://github.com/godotengine/godot/blob/f317cc713aa4dbcee2efa10db764473a56680be7/scene/resources/text_file.h#L37

I'm not sure, but perhaps I'm overlooking something. I'm relatively new to this, so I'm unsure.

@KoBeWi
Copy link
Member

KoBeWi commented Feb 12, 2024

When exporting, in the Resources tab you need to specify extensions of all non-Resource files that you want exported:
image
By default unrecognized files are not exported.

@JNatheriver
Copy link
Author

How I missed that? haha. Thank you!!. Perhaps this should be made clearer in the documentation.

@anderlli0053
Copy link

anderlli0053 commented Feb 13, 2024

When exporting, in the Resources tab you need to specify extensions of all non-Resource files that you want exported:
image
By default unrecognized files are not exported.

So I'll ask this now while I have the chance... If I specify *.* wildcard should it include everything and there is no need to explicitly specify separate file extensions? I know that this may not be optimised for speed and file size, but all-in-all does it work?

@KoBeWi
Copy link
Member

KoBeWi commented Feb 13, 2024

You can test it by exporting a ZIP file and inspecting its contents.
The code uses String.matchn() to filter files, so *.* should be including everything.

@anderlli0053
Copy link

@KoBeWi Thank you!

@steffenheinzl
Copy link

Thanks! That works! Have been searching for a while.
+1 for adding this to the documentation (e.g. a link in the FileAccess and ResourceLoader documentation)

@Calinou
Copy link
Member

Calinou commented Oct 6, 2024

Thanks! That works! Have been searching for a while. +1 for adding this to the documentation (e.g. a link in the FileAccess and ResourceLoader documentation)

Done in godotengine/godot#97906. Feel free to take a look at the PR's changes 🙂

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

No branches or pull requests

6 participants