Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Hello World example - Lesson 02

A topic by MDCO created Feb 16, 2024 Views: 58
Viewing posts 1 to 1
Developer (2 edits)

Adding a GDScript

You can add a Godot script by attaching it to a node. The name of the script is from a node name. So, changing a root node name from a generic one is wise. Then, after you click on a node twice, you can rename it. When adding a script, click to micro button scroll-like with the green plus sign to add it. Another option is to press the right button and select the "attach" script. GDScript is a default script language for coding in Godot. Use a template with a simple code part (default) in the beginning, and later you can select a blank or script without comments. Finally, click "create" in the attached node script dialog window.

Hello World example

First, we will learn the command "print" with one string, and we can see the result of this code in the output window. So, after starting the scene, we will see the "hello world" in the output window. After, we will set a variable, an integer, and print it with the print command. Suppose you use a string variable like the variable "text"; you must put it under quotation marks. Then, write code under the "ready" function.

func _ready():
    var text = "Hello World"
    print(text)

To test your coding, press F6 or click once on the "Run Current scene" micro button. The text can be seen in the output window. It is crucial to declare every variable you intend to use when performing mathematical calculations.

var a = 2
var b = 2
var c
c = a + b
print(c)

If you want to connect part of the text, we use the "+" sign. Unfortunately, you cannot print integer variables and strings together till version 4, but with the "str" command, you can change an integer to a string and then print it on the screen. In the Godot 4 use the following syntax.

print("Result is: " , c) 

To print an integer along with a string in the Godot 3.5 example, it is necessary to utilize the "str" function.

print("Result is: " + str(c)) 

Comments

In GDScript, you can write a one-line comment or use symbols for multi-line commenting. For one line, use #, and multi-line, use'''.

# This is a one-line comment
''' This is a
multi-line
comment '''