Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Control
- var scenes : Array = []
- var path_to_scenes : String = "res://scenes"
- func _ready():
- scenes = list_files_in_directory(path_to_scenes)
- var v_box_container_instance = VBoxContainer.new()
- add_child(v_box_container_instance)
- for i in 6:
- var button_instance = Button.new()
- button_instance.name = "Button" + str(i)
- button_instance.text = str(i)
- v_box_container_instance.add_child(button_instance)
- button_instance.connect("pressed", self, "_on_button_pressed", [i])
- func load_scene( res ):
- var scene_instance = load( res ).instance()
- get_parent().add_child(scene_instance)
- queue_free() # may or maynot suit your needs
- func list_files_in_directory(path):
- var files = []
- var dir = Directory.new()
- dir.open(path)
- dir.list_dir_begin()
- while true:
- var file = dir.get_next()
- if file == "":
- break
- elif file.ends_with(".tscn"):
- files.append(file)
- dir.list_dir_end()
- return files
- func _on_button_pressed( value):
- #print( value ) # will most likely come in handy knowing which button was pressed
- load_scene( scenes[int(rand_range(0,scenes.size()))])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement