Sending a value to an instanced scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rogerdv

In my game, the player must answer a question whenever he/she hits a door or is catched by the enemy. I know how to instantiate the scene containing the question UI, but I cant find a way to tell that scene that it was invoked to open a door or because of the enemy catching the player (each case uses different sources to get the questions). How can I set a parameter or send some value to the scene to indicate the mode it should work?

:bust_in_silhouette: Reply From: njamster

If you have the instance, you can call any of it’s function and set/get all of it’s variables. So if your question-UI-scene has a var invoked_by, you could do:

var new_question = load("<PathToQuestionUiScene").instance()
new_question.invoked_by = "door"
add_child(new_question)

Setting the variable first and then adding the instance to the tree ensure, invoked_by is already set when _ready() is called.

So, the script should be in the top node of the scene, isnt it?

rogerdv | 2020-02-27 18:00

Yes, for my example above I assumed this. However, you also could do:

new_question.get_node("Child/ChildOfChild").invoked_by = "door"

That’s of course assuming your QuestionUiScene has a child-node called “Child”, which itself has a child-node called “ChildOfChild”, which has a script attached, that defines the variable invoked_by. But I think you get the idea. :slight_smile:

njamster | 2020-02-27 18:31