Our First Script in Godot
Lets start by loading our Hello World program.
Over on the left side of the editor you see the Scene dock. Click on the + to add a child node to Label.
In the search field that appears, type button.
Click Create.
Now in the Scene dock you should see a Button node added as a child of Label.
Right-Click on the Button node to bring up a menu. Select Attach Script.
Click Create from the pop-up.
A mostly blank script appears. It just says...
extends Button
At the left side of the editor, in the Scene dock, click on the Button to highlight it.
Then at the far right side of the editor you will find the Inspector dock. In the text field, write something like, "Click Me".
Then next to the Inspector tab, click the Node tab.
Right click on the pressed() signal, then click Connect from the pop-up menu.
Then another pop-up appears. Click Connect.
Now we have more code added to our script.
extends Button func _on_pressed() -> void: pass #Replace with function body.
We will delete the last part of the code the starts with pass, and write our own code.
extends Button func _on_pressed() -> void: var label = get_parent() if label.text == "This Worked!": label.text = "Hello World!" else: label.text = "This Worked!"
Our new button is on top of our label. So use the mouse to drag and reposition the button.
Now we are ready to play the scene. When you click on the Click Me button the script works!
Thank you for reading. If you need more practice with GDScript then start with this link.
I hope you had fun.
Comments
Post a Comment