How to propper pass variables along?

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

Hi, just getting started with Godot.
I saw the posts from KidsCanCode:

And understand that the best way to pass variables down the tree would be directly through node reference and up the tree would be through signals.

But I am building a scene and want to understand the best practice to understand how to work from now on either by passing variables through a specific way or changing my scene construction.

So I have a main Scene.
This Main Scene instantiate a player scene and one or more enemy scenes.
It also instantiates a Score scene (where later on I will build the UI elements).

The players or enemies instantiate bullets scenes. So after all instantiations, at one time I might end up with something like this:

Main Scene:
-----Bullet 1
|-Enemy 2
-----Bullet 2.1
-----Bullet 2.2
|-Enemy 3
-----Bullet 3.1
|-Enemy 4
-----Bullet 4.1
-----Bullet 4.2
-----Bullet 4.3

The Main Scene script handles score counting and sending to Score scene, AI movements and shots.

The Player Scene handles player movement and shots

Score Scene only has the labels, so far. I was building it as to only handle the visual part of the UI, and not have any scripts.

The Bullet scene handles collision and needs to send collision info and points for score update up until the Main Scene

The Main Scene will then calculate the new score and pass it down to the Score Scene to update it visually.

Problem is that passing the collision update and score up the tree thorugh node reference would not be a good idea, as the article mentioned as any change would break everything.

But to send a signal, if I understand correctly the right way would be to have only a “connector” in the Main Scene and that Main Scene pass down the score update to a function inside the Score Scene. But since my Score Scene does not handle scripts I would need to change the way it works to accomodate this.

Question 1. Should I be using emitters in this case? And if so I will then need to attach a script to the Score Scene right?

Question 2. Is the way I am building the whole scene the right way or should I be going about it differently and that is why I am having problems?


:bust_in_silhouette: Reply From: exuin

So what I think you should do is use a signal that connects from the bullet to the Main scene. You don’t need a script on the Score scene, you can just change all the values from the Main scene since it’s a parent scene. But there’s nothing wrong with having a script on the Score scene.

I think you’re doing fine in coding your scene. Remember that “call down, signal up” is just a guideline and you don’t have to follow it completely all the time.

I tried to implement the signal, but I might be loosing something here and the tutorials I found didn’t solve the problem for me.

In the above example there are the ground (it’s a top down shooter). The player and the enemies are instantiated at the Main Scene Process function.

When chasing each other a Bullet scene is instantiated and then moves and checks for collision. When there’s a collision this Bullet instancce should then emit a signal to the Main Scene.

What I am not sure how to do this is that in every tutorial I saw about signals, all the nodes and scenes are already placed and not instanciated. So I always see the connector being written on the Main Scene ready or proccess function. But doing so will not work because at either time these functions are called on the Main Scene,the Bullet node still does not exist.

What am I doing wrong?

zeitgeist | 2021-11-06 22:28