Godot Version
v4.3.stable.official [77dcf97d8]
Question
I’ve got a script that’s picking a random word from a list of words in a txt file in the game files which works fine when testing in Godot but doesn’t work once exported to Android and tested in the emulator in Android Studio.
When run in Android, instead of displaying the random word, it displays null. I’ve tried exposing the error which says “Can’t open” but without any other information. Do you need OS file permissions for opening a file within the package itself or is there something else I’m missing that changes when exporting for Android?
I’ve made sure that the files are included in the export filter and confirmed they are present in the package.
The following is the script attached to the Node2D
func _ready() -> void:
var random_word = get_random_word_from_file("res://wordLists/wordlist1.txt")
$CanvasLayer/text_main.text = random_word
func get_random_word_from_file(file_path):
var text = load_file(file_path).strip_edges()
var words = text.split("\n")
for i in range(words.size()):
words[i] = words[i].replace(",", "")
words[i] = words[i].replace(".", "")
return words[randi() % words.size()]
func load_file(file_path):
var file = FileAccess.open(file_path, FileAccess.READ)
if file == null:
var error_str: String = error_string(FileAccess.get_open_error())
$CanvasLayer/text_main.text = error_str
var text = file.get_as_text()
return text
Are you sure file is exported, I just tried it and it worked without any error.
Can you reproduce this in any other project?
1 Like
This is a new project and yes, I have confirmed they are present in the APK by unpacking it and verifying they are there.
I just put together a new project, single scene with a label and only the file functions script to send over and it works.
The original project only has a couple of differences so I’ll add them in until it breaks and come back with the result
1 Like
So this is confusing, I set up the remote debugging to try and get to the bottom of it.
If I explicitly define the “file_path” variable inside the “load_file” function, it works. Though in the screenshot, with that commented out, you can see the file_path is being passed into the function correctly and is set to “res://lists/wordList1.txt” but then it doesn’t open and “file” just ends up as null.
Could you send me the project where you’re encountering this error? I’ll be able to test it properly.
1 Like
Here you go, I’ve removed pretty much everything from the project and left it barebones with the error.
https://file.io/Qv4tIZ86gylC
This site can’t be reached
I’m unable to open this link. You can upload your zip file directly here.
1 Like
It won’t let me upload the zip file, maybe too new of an account or something. Here’s a link to grab it from GitHub:
@crimsonfox There is a small typo in your code, after fixing that it worked.
Here you have given wrong filename, var random_word = get_random_word_from_file("res://lists/wordlist1.txt")
.
It should be res://lists/wordList1.txt
.
EDIT: Even if it worked in the editor, you must have been seeing this warning:
'Case mismatch opening requested file 'res://lists/wordlist1.txt', stored as 'res://lists/wordList1.txt' in the filesystem.' This file won't open when exported to other case-sensitive platforms."
Also see case-sensitivity
4 Likes
Ah haha, now that’s embarrassing! Can’t believe I didn’t see that, thank you for so much for taking a look and pointing it out. At least I’m learning about the case sensitivity of platforms now though I guess!
On a side note, I wasn’t getting that error. Should it be showing in the debugger or somewhere else? Or is there some more verbose warnings setting for the editor?
1 Like
Yes, this warning should be displayed in the debugger’s Errors tab when running the project in the editor.
1 Like