Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | fatcat__25 |
I have a TileMap saved as a scene for each level of my game, at the file path res://Maps/levelname/TileMap.tscn
. I need to instance this correct TileMap under my root node, which hosts everything else that makes the game work. I have tried both of the following variations of code to achieve this with a test map:
var next_map
var loading_map
if File.new().file_exists("res://Maps/testencounter/TileMap.tscn"):
next_map = load("res://Maps/testencounter/TileMap.tscn")
loading_map = next_map.instance()
add_child(loading_map)
else:
pass
&
if ResourceLoader.exists("res://Maps/testencounter/TileMap.tscn"):
next_map = ResourceLoader.load("res://Maps/testencounter/TileMap.tscn")
loading_map = next_map.instance()
add_child(loading_map)
else:
pass
Both of these pieces of code simply pass, or print error if I replace pass with print(“error”). If I remove the if statement, they throw errors about being unable to open the specified file, and failing to load the resource. To see if anything could access the file, I replaced all code with
get_tree().change_scene("aforementioned path")
Right on queue, the scene changed from the root scene to the TileMap.
My question is: What am I doing wrong, and what is the right way to do what I’m trying to?