Skip to content

Feature Request: A way to pass data between scenes #37

@GameDev-One

Description

@GameDev-One

Hi there,

I'd like to request a feature since I have been having a great time using this addon for my project.

Passing data between scenes is useful when you want persistent data between scenes without using a global script. In my case, I am creating a multiplayer game and each player obtains information(data) about their lobby. Which is then passed to the next scene as the transition happens. This data is used to initialize other parts of that "next scene", and then never used again.

It's not that pretty but, I have done the following to pass data between the scenes and thought you might know a cleaner way to implement it.

  • Modified all methods that call _change_scene to accept a Dictionary as a parameter. (Defaults to an empty Dictionary)
  • Ensure any scene that I wanted to pass data to has a variable called Msg as a Dictionary
  • Set the message for the new scene after the old scene has been freed

My Project

# my_main_scene.gd
# Lobby Data as a Dictionary
var Msg: Dictionary = {}

# Change to the new scene after it is done loading
func _OnLoadingFinished() -> void:
	SceneManager.change_scene_to_loaded_scene(
				FadeOutOptions, 
				FadeInOptions,
				GeneralOptions,
				{Msg}
                                )
	pass

Scene Manager Addon

# scene_manager.gd
# checks different states of the scene and makes actual transitions happen
func _change_scene(scene, add_to_back: bool, Msg: Dictionary = {}) -> bool:
    #...
    get_tree().get_current_scene().free()

    # Pass the message to the current scene if the scene has a "Msg" variable
    if "Msg" in scene_instance and not Msg.is_empty():
        scene_instance.Msg = Msg

    root.add_child(scene_instance)
    #...
    pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions