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
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.
@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."
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?