Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/17 packed scene #19

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
16 changes: 11 additions & 5 deletions addons/scene_manager/SceneManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion demo/test2.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends Node

export var test_scene: PackedScene

func _ready():
yield(SceneManager, "scene_loaded")
Expand All @@ -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}
)
8 changes: 4 additions & 4 deletions demo/test2.tscn
Original file line number Diff line number Diff line change
@@ -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="."]

Expand All @@ -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
Expand Down