0 votes

Hey
How can you convert/add what was found in directory, as a scene/node?
After nodes were detected( 4 files - file.tscn), i cant manage to add them to the scene as s childs to the scene..

Here is the current code..

func load_game():
var dir = Directory.new()
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")
        files = dir.get_next()
in Engine by (227 points)

1 Answer

0 votes

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://".

by (29,118 points)

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)

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.

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()

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()

I had no idea, that you can join strings, paths like that!
Great, Loaded them! Thanks a lot! :)

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 Frequently asked questions and 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 [email protected] with your username.