diff --git a/README.md b/README.md index 1ce5b57..7e71841 100644 --- a/README.md +++ b/README.md @@ -53,11 +53,12 @@ Be sure to read the docs down below for a more detailed explanation. ## SceneManager -### `func change_scene(path: String?, options: Dictionary = defaultOptions) -> void` +### `func change_scene(scene: (String || PackedScene)?, options: Dictionary = defaultOptions) -> void` This method lets you easily change scenes with transitions. They're highly customizable and we will consider adding progressive loading if it's requested enough. -The `path` paremeter accepts an absolute file path for your new scene (i.e: 'res://demo/test.tscn'). If `null` is passed as the path, it will reload the current scene, but for ease-of-use we recommend using the `reload_scene(options)` function explained further down. +The `scene` paremeter accepts an absolute file path for your new scene (i.e: 'res://demo/test.tscn') or a `PackedScene`. If `null` is passed as the scene, it will reload the current scene, +but for ease-of-use we recommend using the `reload_scene(options)` function explained further down. You can pass the following options to this function in a dictionary: diff --git a/addons/scene_manager/SceneManager.gd b/addons/scene_manager/SceneManager.gd index c738101..8675dad 100644 --- a/addons/scene_manager/SceneManager.gd +++ b/addons/scene_manager/SceneManager.gd @@ -135,15 +135,15 @@ func _process(_delta: float) -> void: _previous_scene = _tree.current_scene -func change_scene(path, setted_options: Dictionary = {}) -> void: +func change_scene(scene, setted_options: Dictionary = {}) -> void: var options = _get_final_options(setted_options) if not options["skip_fade_out"]: yield(fade_out(setted_options), "completed") if not options["skip_scene_change"]: - if path == null: + if scene == null: _reload_scene() else: - _replace_scene(path) + _replace_scene(scene) yield(_tree.create_timer(options["wait_time"]), "timeout") if not options["skip_fade_in"]: yield(fade_in(setted_options), "completed") @@ -164,16 +164,22 @@ func _reload_scene() -> void: _current_scene = _tree.current_scene -func _replace_scene(path: String) -> void: +func _replace_scene(scene) -> void: _current_scene.queue_free() emit_signal("scene_unloaded") - var following_scene = ResourceLoader.load(path) + var following_scene = _load_scene_resource(scene) _current_scene = following_scene.instance() yield(_tree.create_timer(0.0), "timeout") _root.add_child(_current_scene) _tree.set_current_scene(_current_scene) +func _load_scene_resource(scene) -> Resource: + if scene is PackedScene: + return scene + return ResourceLoader.load(scene) + + func fade_out(setted_options: Dictionary = {}) -> void: var options = _get_final_options(setted_options) is_transitioning = true diff --git a/demo/test2.gd b/demo/test2.gd index 781c0d5..f226fef 100644 --- a/demo/test2.gd +++ b/demo/test2.gd @@ -1,5 +1,6 @@ extends Node +export var test_scene: PackedScene func _ready(): yield(SceneManager, "scene_loaded") @@ -9,6 +10,6 @@ func _ready(): func _on_Button_button_down(): if not SceneManager.is_transitioning: SceneManager.change_scene( - 'res://demo/test.tscn', + test_scene, {"pattern_enter": "diagonal", "pattern_leave": "curtains", "invert_on_leave": false} ) diff --git a/demo/test2.tscn b/demo/test2.tscn index ad0df7b..51a41d5 100644 --- a/demo/test2.tscn +++ b/demo/test2.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://demo/test2.gd" type="Script" id=1] +[ext_resource path="res://demo/test.tscn" type="PackedScene" id=2] [node name="Level2" type="Node"] script = ExtResource( 1 ) +test_scene = ExtResource( 2 ) [node name="CanvasLayer" type="CanvasLayer" parent="."] @@ -15,9 +17,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Button" type="Button" parent="CanvasLayer/ColorRect" groups=[ -"scene_manager_entity_nodes", -]] +[node name="Button" type="Button" parent="CanvasLayer/ColorRect" groups=["scene_manager_entity_nodes"]] anchor_left = 0.4 anchor_top = 0.4 anchor_right = 0.6