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

Godot 4: Callbacks when changing scenes #11

Closed
jabsatz opened this issue Apr 6, 2022 · 3 comments
Closed

Godot 4: Callbacks when changing scenes #11

jabsatz opened this issue Apr 6, 2022 · 3 comments
Labels
enhancement New feature or request waiting for godot Waiting for responses from the godot team or an engine update

Comments

@jabsatz
Copy link
Collaborator

jabsatz commented Apr 6, 2022

Is your feature request related to a problem? Please describe.
Would like to solve the confusion/issues proposed in #8, without internally setting variables on the new scene in an obscure way.
Also would like to give as much control to the users of this library as possible without bloating it.

Describe the solution you'd like
A "hook" or "callback" sent to SceneManager when the new scene enters the tree and/or becomes ready. It should recieve the new scene and allow for direct modifications there. I'm imagining something like this:

SceneManager.change_scene("res://scene_2.tscn", {
  "on_tree_enter": func(scene): scene.some_variable = "some_value"
})

This would be a great feature to implement when Godot 4 releases, since it makes very good use of the new lambda functions feature.

Additional context
Unfortunately, I don't think there's a very good way to do this in Godot 3.x.
I don't think a FuncRef would work since the original scene that calls this function would most likely already be unloaded, and the logic in a separate static method seems like it would be more of a hassle than just using a regular Autoload or custom solution.
I also considered using "expressions" and evaluating them internally, but that also feels limiting and extremely hacky.

@jabsatz jabsatz added enhancement New feature or request waiting for godot Waiting for responses from the godot team or an engine update labels Apr 6, 2022
@mouring
Copy link
Contributor

mouring commented Apr 7, 2022

Would be nice to extend this for general purpose as your solution only handles load/reload of a scene, and not fade_in_place(). Right now I have the following hack in my tree:

 var default_options := {
 	"speed": 2,
 	"color": Color("#000000"),
[..]
 	"no_scene_change": false,
 	"on_tree_enter": func(scene): null,
 	"on_ready": func(scene): null,
+	"func" : func(): null
 }

[..]
func change_scene(path: Variant, setted_options: Dictionary = {}) -> void:
[..]
 	if not options["no_scene_change"]:
 		_replace_scene(path, options)
+	if options["func"] != null:
+		options["func"].call()
[..]

It isn't critical as #12 provides all the functionality I need in the longer form:

SceneManager.fade_in_place({ "fade_direction": SceneManager.fade_dir.OUT })
 _FuncCall()
SceneManager.fade_in_place({ "fade_direction": SceneManager.fade_dir.IN })

But it would be nice to have the short form of:

SceneManager.fade_in_place({ "func": Callable(self, "_FuncCall") })

@jabsatz
Copy link
Collaborator Author

jabsatz commented Apr 7, 2022

Yes @mouring , that's definitely doable.
I have no problem adding as many "callbacks" as needed here, since they're a very quick and easy solution to add more options for users.

@jabsatz
Copy link
Collaborator Author

jabsatz commented Mar 2, 2023

This has been implemented and is included from version 1.0.0 🎉
Closing since Godot 4 IS FINALLY OUT!!!

@jabsatz jabsatz closed this as completed Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting for godot Waiting for responses from the godot team or an engine update
Projects
None yet
Development

No branches or pull requests

2 participants