0 votes

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?

Godot version 3.3.3.stable
in Engine by (12 points)

1 Answer

0 votes

First some generic advice:
1. Don't use variables in outer scope, if you don't use them in outer scope. Make them local.
2. Don't clone constant literals (strings) by copy-paste. This method is prone to errors. Use dedicated identifier for this.
3. Use debug tools. From simple print() to breakpoints, scene-tree (called Remote tree) and variables inspectors. This way you will know what's exactly happening under the hood.

Your approach is basically correct, but the errors tells you don't have a file "Maps/testencounter/TileMap.tscn" in your project tree. For your case I'd use something like this:

func load_scene(var path, var parent : Node) -> Node:
  var result : Node = null
  if ResourceLoader.exists(path) :
    result = ResourceLoader.load(path).instance()
    if result :
      parent.add_child(result)
  return result
# ---
var scene_file : String = "res://path/file.tscn"
if ! load_scene(scene_file, self) :
  print("Error loading scene ", scene_file)
by (1,438 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.