Module 0363: GoDot: Members of a node

Tak Auyeung

1 Getting started

To get started with this project, refer to Module 0358, download the code and install it in GoDot. To refresh your memory of installing a downloaded ZIPped project, refer to module 0357.

2 Getting the text content of a label from a button

Click Script in the middle of the top toolbar to access the scripts. Select DoSomething.gd to view its content.

For this module, you can remove all the indented code of _on_DoSomething_pressed, and replace them with the following (indented):

    print($"../OutputLabel".text)

Then run the app, click the button, and observe the Output panel. The output should be similar to the following screenshot:

Output of accessing a property of a sibling node

3 What just happened?

3.1 What is $"../OutputLabel"

This is a unique notation in GDScript (it does not exist in Python). The notation of $"blah" is the same as the actual function/method call of get_node("blah"). "blah" should be replaced by the actual path to a node.

In other words, the dollar sign $ notation is merely a notation to save some typing. But what exactly is a path?

A “path”, in general, is “a direction to locate something in a tree.” For example, a file path is a path to locate a file within a tree of folders and files. There are several important points:

In this example, the path ../OutputLabel can also be specified as /root/Control/OutputLabel. The latter is called an absolute path. The initial / to begin the path indicates “to start from the root of the scene”.

You may also want to read the official explanation of node paths.

3.2 What is the .text notation?

text is a property of a Label. You can read the specification of the Label class. The single dot (.) has the same meaning as apostrophe-‘s’ in English (possessive). This syntax accesses members (properties and methods) of an object/class.

#what-is-a-property

What is a property?

A property is complicated to explain because it is an abstraction to allow flexibility, but it looks like a simpler concept of “field” (also known as “data member”).

For all practical purposes in this module, you can consider a “property” as some value of a node that can be read (get) or overwritten (set).

3.3 Can a property be changed?

The answer is yes! You can append the following line of code to _on_DoSomething_pressed:

    $("../OutputLabel") += "!"

Run the app again, and click the button several times. Do you see that each time you click a button, an extra exclamation point ! is appended to the label?

The += symbol represents a compound assignment where A += B is the same as A = A + B. A compound assignment is a convenience feature that is commonly used to improve code legibility.

3.4 Download this project

You can download this project.

4 Note to self