#Question about setting a variable equal to a child node

7 messages · Page 1 of 1 (latest)

faint rapids
#

How are we able to change the attributes of that node using the variable we set?

In the lesson titled "Coding the slideshow" in "Learn to code from zero with Godot", we have a slide show, whose child is a VBoxContainer, whose children are a Label and Button node.

To access the button, we set: onready vaar button := $VboxContainer/Button

It's used for connecting the Button node to the Slideshow script and let's us act on when the "pressed" signal is triggered. That makes sense.

But later in the lesson, we program the button's text to change to "Quit" when on the last slide.

We do this with the code: if current_slide == last_slide, button.text = "Quit".

My understanding is that this will only effect our local variable defined in the Slideshow's script.

I don't understand how this variable we set within the script is making changes to the Button child node directly.

humble moon
#

The variable contains what is called a "reference".

It is, essentially, an 'address' to where the button is located in RAM, including all of its properties and its functions.

Then you have the . operator. This is the 'accessor' operator, which accesses all the properties and functions of a reference.

$ in GDScript is short hand for get_node(), and its job is to return a reference to a node that's in the scene tree. In this case, a Button.

Now that we have this out of the way, you need to understand the difference between a value and a reference.

0 is a value, "Hello World" is a value.

var my_a = 0
var my_b = my_a
my_a = 5
print(my_b) # will still be 0!

Values are copied.

Values in GDScript are ints, floats, strings, Vector2/3/4, Plane, Quat, AABB, bool, Callable, Color, Rect2, and some others.

Then you have References. References are, as their name implies, references to some other object.

References are not copied. When two objects hold a reference to the same thing, they are pointing to that thing.

var my_a = $My/Node
my_a.text = "Value"

var my_b = my_a
my_a.text = "Hello!"
print(my_b.text) # will be "Hello!"

References are all objects (nodes, resources, refcounted), Dictionary, and Array.

#

so when you did var button := $VboxContainer/Button, you did not create a local version of the button, but you got a ticket that told the button variable where the button object exists

#

button, as a result, becomes a gateway to all the stuff that's in RAM about that very particular button that Godot exposes

#

including, as the lesson had you do, its label, via the text property

faint rapids
#

Thanks Razoric. I really appreciate you explaining to me the difference between values and references.

I'm visualizing a reference as like a two way street, where variables holding values act as expected.

the . operator makes sense too, since we've used it several times before.

I had no idea about the concept of references before... and that Objects assigned to variables were references. Not sure if that's something I just glossed over in the learn GD Script course.

#

the concept of Objects having references must be suuuuper useful in all of programming.