How to load text to android from res://

Godot Version

Godot 4.6.2 custom build

Question

Hello. I decided on a simple text download from res:// on Android. As far as I understand, there is no direct way to load a text file on Android. To do this, I used JSON, which is so simple. :

[gd_resource type=“JSON” format=3]

[resource]
data = “some text 10+ kb”

we load it using the function:

func get_text_privacy(conf_path : String) -> String:
	if !ResourceLoader.exists(conf_path):
		return conf_path + " no exists"
	var js = ResourceLoader.load(conf_path)
	if js == null:
		return conf_path + " file is empty"
	return js.get_data()

Everything works in the debug version. Release date - crashes when var js = ResourceLoader.load(conf_path) . What’s wrong. Or how can I download 10+ kb text from res:// to Android?

Make sure that *.json (or the file extension) files are exported in Exporting>Sources, then just use load(“config.json”). It is that simple, no need to use custom Resources.

If that doesn’t work, try FileAccess.

But, it is weird that it crashes. It seems like you have proper error handling. Maybe try to do it in a Thread if it is really a big file?

1 Like

Here is an example I use to read text files. Returns error code + content to be processed later:

static func load_txt_file(file_path : String) → Dictionary:

#var file_path = "res://my_text_file.txt" # Replace with your file's path

if FileAccess.file_exists(file_path):
	var file = FileAccess.open(file_path, FileAccess.READ)
	if file:
		var content = file.get_as_text()
		#print("File content:\n", content)
		file.close() # Important to close the file
		return {"error" : 0, "content" : content}
	else:
		print("Error opening file: ", FileAccess.get_open_error())
		return {"error" : 2}
else:
	print("File does not exist: ", file_path)
	return {"error" : 1}
1 Like

The files are exported. The thing is, when I export the debug version, everything works. And when the debug export is disabled, the game crashes. Godot itself does not issue any errors. Logcat points to /data/app/ru.krocknarock.mur-ALHV9D_D-mvZPVwYE56NMA==/lib/arm64/libgodot_android.so. I do not know what to do about it. Also, when the device crashes, it is suggested to send a report stating that the problem is with rendering or an infinite loop.

The main problem is that FileAccess does not work when loading from res:// on android. FileAccess.get_open_error() returns 0.

1 Like

Just a silly question, you are trying to open an existing file or creating a new one for writing on it?

1 Like

Do you use custom build templates or the Godot ones? Are you sure the problem cannot be anything else, but the file loading?

1 Like

I’m trying to read an existing file.

I use my own template for the build. But if the problem was with the template, then it wouldn’t work either in the debug. Using tracing, I found out that the game crashes on the line var js = ResourceLoader.load(conf_path).

It could be so many things beyond file loading since it’s a dreaded Release vs Debug bug. As you know, you don’t run the same code (let’s forget debug symbols) in debug and release builds : debug usually has no optimizations, release does, possibly at varying levels, in the same app. Stuff that is initialized to zero in debug becomes null pointers in release, racing conditions can be triggered because of a different execution speed and different object code for the same high level code path.

Can you build a minimal app as MRP that does nothing but load that file and still crash ?

And that’s only one avenue, the debug vs release thing.

Sorry can’t help you outright more than that.

1 Like

Oh-oh. Yes, I should have checked it right away on a clean project. Everything works in the Steam version. It looks like I did something wrong with the project. Thanks to all who responded. The topic can be closed.

The problem was that I did not use “gradlew clean” in the platform/android/java source code of the engine when building my own templates.

2 Likes