Android loading levels from path doesn't work

Godot Version

Godot 4.3 stable

Question

In my game I have a loading from folder, it takes all the scene from one folder and it creates buttons that connect it to the scene.
Everything works fine when I’m testing it on PC but when I export for android I can’t see any button, meaning that it doesn’t read the directory probably.

func get_level_scenes() -> Array:
	var dir = DirAccess.open(level_scenes_folder)
	if dir == null:
		print_debug("Invalid folder path at ", level_scenes_folder)
		return []

	var scenes = []
	dir.list_dir_begin()
	while true:
		var file = dir.get_next()
		if file == "":
			break
		if file.ends_with(".tscn"):
			scenes.append(level_scenes_folder + file)
	dir.list_dir_end()
	return scenes



func create_level_map(level_scenes : Array):
	var directions = [Vector2.DOWN, Vector2.RIGHT, Vector2.UP, Vector2.RIGHT]
	var current_position = initial_position.global_position
	var current_direction_index = 0
	print(GameData.get_level_completed_number())
	for i in range(level_scenes.size()):
		var button_instance = level_button_scene.instantiate()
		button_instance.name = "Level_" + str(i + 1)
		button_instance.position = current_position
		button_instance.text = str(i + 1)
		button_instance.pressed.connect(_on_test_level_pressed.bind(level_scenes[i]))

		#TODO: LOAD FROM FILE
		#DEBUG, LOAD THE BUTTON FROM FILE
		#current_level = GameData.get_level_completed_number()
		button_instance.disabled = i >= current_level

		button_holder.add_child(button_instance)

		current_position += directions[current_direction_index] * offset
		current_direction_index = (current_direction_index + 1) % directions.size()

that’s my loading code and everything works fine as mentioned on PC, android doesn’t create these buttons

Is there a way to do allow it or should I create it manually instead of dynamically?

If anyone is interested, I solved the problem. Android doesn’t recognise some functions of DirAccess, for example list_dir_begin apparently and get_next() or list_dir_end, I don’t know why. If I use a hard coded path it works.

I did find a solution tho, I made a tool that read all the files in the directory and then creates a JSON. I read the strings from the JSON and I add them into a variable in my map and the trick is done!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.