|
|
|
|
Reply From: |
Zylann |
You can load a .tscn dynamically by doing this:
var path = "res://somwehere/scene.tscn"
# Instance as child:
var scene_resource = load(path)
var scene = scene_resource.instance()
parent_node.add_child(scene)
# Or, load as complete replacement of current scene:
get_tree().change_scene(path)
Note: I see your path contains “SAVES”, I hope you don’t use this folder to write savegame player data, because “res://” is not writable for exported games. Instead, savegames should be put under “user://”.
I would do so, but the thing is, i want to load All files that the dir.get_next() finds, not a specific one, whichs path i’d write in the code…
So, if it finds that the folder has 5 scenes, for example, itd load all five, adding them to the scene, one after another…
So i want the code to be able to detect files and, when a button is pressed, all files would be loaded up in the scene
I later want to make that, you cant search for a specific keyword in those exported scene’s names and load them(instead of loading all scenes in the folder and then deleting ones i dont need)
Serenade | 2017-03-20 14:07
My code is just an example. The path
variable can be anything you compute at runtime, not necessarily a hardcoded path.
So if you want to load all 5 scenes in your directory, use the example I wrote, but replace path
by the path you get in your loop for every file.
Zylann | 2017-03-20 17:08
I dont know how the code would looks like for var path…
when i get
var files = dir.open(res://)
while files != “”:
…
files.path doesnt work either… how can you add the tscn location to the path variable?
my code so far…:
func load_game():
var dir = Directory.new()
var path = ""
if dir.open("res://SAVES_folder") == OK:
dir.list_dir_begin()
var files = dir.get_next()
while files != "":
if not dir.current_is_dir():
print (files, " B4 LOADING",path)
files = dir.get_next()
Serenade | 2017-03-21 09:57
When you do dir.get_next()
, it returns you a file name, as explained in the doc: http://docs.godotengine.org/en/stable/classes/class_directory.html#class-directory-get-next
So to obtain path
, you have to take your directory path and append the filename for each file. For example, get_next
will get “level1.tscn”, and once you add the directory it will be “res://SAVES_folder/level1.tscn”.
Your code should then look like this (I changed some variables for clarity):
func load_game():
var dir = Directory.new()
var directory_path = "res://levels"
if dir.open(directory_path) == OK:
dir.list_dir_begin()
var filename = dir.get_next()
while filename != "":
if not dir.current_is_dir():
var path = directory_path + "/" + filename
print(path)
# Load here
#...
filename = dir.get_next()
Zylann | 2017-03-21 20:29
I had no idea, that you can join strings, paths like that!
Great, Loaded them! Thanks a lot!
Serenade | 2017-03-22 16:48