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.
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:
$"../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:
..
: the double dot notation refers to the “parent”, this is the container of the node where the script is attached to./
: the forward slash is a separator between nodes in order to specify the pathOutputLabel
: this is the name of the node (as shown in the Scene dock)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.
.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?
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).
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.
You can download this project.