How do you pass data into a chosen scene

Viewed 162

Assume we have a main menu with multiple buttons, and we have a text file which contains data behind button 1, button 2, etc.... The text data is loaded into an array of dictionaries.

We are loading a scene like this:

# Global singleton provides a data set, i.e.
var mydata = [{...}, { ... }, etc...]

# On the main menu scene we have buttons
func _on_Button1_pressed():
    get_tree().change_scene("res://DataList.tscn")
    newscene???.send_data(mydata[0]) # ????

How do I pass the preloaded dictionary object associated with Button1 into the scene that is about to appear?

I understand that change_scene() is "deferred" so we cant just straight away call a "setter" function for this scene, or can we? I am still learning godot so I am a bit lost as to know how to do this

Note that I am expecting to "build up" and "rebuild" the new scene when the user presses the button, and "teardown" when a user exits the scene.

1 Answers

# Global singleton provides a data set, i.e.

If you already have an autoload (singleton), I would put there the information you want to give the other scene, and have the other scene read it.


I understand that change_scene() is "deferred" so we cant just straight away call a "setter" function for this scene, or can we?

Correct, you can't. For the instant where the new scene is loaded the current one is already unloaded, so it can't really call a method on the new one.

Unless you take control of the process. See Change scenes manually.

Related