Complete Guide to GDScript – Godot’s Scripting Language (original) (raw)

What is the best programming language for Godot? Spoiler: it’s probably GDScript.

With the release of Godot 4, more developers than ever before are flocking to learn this free and open-source engine. However, many come in not knowing what programming language they need to use. While C# is an option (for those familiar with Unity), GDScript is by far the go-to when it comes to scripting in Godot.

What is GDScript, though, and how does it work? In this article, we’re going to cover the fundamentals of GDScript so you can start tackling your Godot game projects with finesse.

If you’re ready, let’s get started!

Table of contents

Introduction to GDScript

Welcome to the world of GDScript! In this section, you’ll learn what GDScript is, why you should use it, and how to set up your development environment so you can start writing code right away.

CTA Small Image - Complete Guide to GDScript - Godot's Scripting Language

FREE COURSES AT ZENVA

LEARN GAME DEVELOPMENT, PYTHON AND MORE

ACCESS FOR FREE

AVAILABLE FOR A LIMITED TIME ONLY

What is GDScript?

GDScript is a scripting language that was specifically designed for the game engine Godot. It’s a high-level language, which means it’s easy to read and write compared to lower-level languages like C++. And best of all, it’s designed with game development in mind, so it has features that make game development faster and easier.

Benefits of using GDScript

Here are a few reasons why GDScript is a great choice for game development:

Setting up the development environment

To start using GDScript, you’ll need to download and install the Godot game engine. You can download it for free from the Godot website. Once you have Godot installed, you’re ready to start writing GDScript code!

In the next section, we’ll dive into the basics of GDScript and start writing some code. Let’s get started!

As we delve deeper into the intricacies of GDScript, you might be interested in Zenva’s Godot 4 Game Development Mini-Degree. This comprehensive collection covers everything from using 2D and 3D assets, controlling gameplay flow, to mastering Godot’s engine tools and its simple programming language, GDScript. It’s a fantastic resource that not only complements our guide effectively, but also expedites your journey from beginner to game developer, allowing for a more practical understanding of how Godot 4 is primed to quicken your game creation process.

2D Platformer made with Godot 4

Variables and Data Types

In this section, you’ll learn how to store and manipulate data in GDScript. You’ll learn about variables and different data types, and how to declare and initialize them.

Introduction to Variables

Variables are containers that store values in your code. Think of them like little containers that hold data that you can use and manipulate in your code. For example, you can store a player’s name in a variable so that you can use it throughout your game.

Declaring and Initializing Variables

To use a variable, you first have to declare it. You do this by giving it a name and specifying what type of data it will store. Here’s an example of declaring a variable in GDScript:

var player_name: String

In this example, we declared a variable named “player_name” that will store a string value. To initialize a variable, you give it a value when you declare it like this:

var player_name: String = "John Doe"

Data Types in GDScript

In GDScript, there are several data types that you can use to store different types of data. Here are the most common data types in GDScript:

var score: int = 10

var price: float = 19.99

var player_name: String = "John Doe"

var game_over: bool = false

var players: Array = [ "John Doe", "Jane Doe", "Jim Doe" ]

var player_scores: Dictionary = { "John Doe": 10, "Jane Doe": 20, "Jim Doe": 30 }

In the next section, we’ll learn about operators and expressions, which are used to manipulate data in your code.

Real-time strategy game made with Godot 4

Operators and Expressions

In this section, you’ll learn how to perform operations and manipulate data using operators and expressions in GDScript. We’ll cover different types of operators and how to use them in your code.

Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and more. Here are the most common arithmetic operators in GDScript:

var result = 2 + 2 # result will be 4

var result = 5 - 2 # result will be 3

var result = 2 * 2 # result will be 4

var result = 4 / 2 # result will be 2

var result = 7 % 3 # result will be 1

Assignment Operators

Assignment operators are used to assign values to variables. Here are the most common assignment operators in GDScript:

var score = 10

var score = 10 score += 5 # score will now be 15

var score = 10 score -= 5 # score will now be 5

var score = 10 score *= 2 # score will now be 20

var score = 10 score /= 2 # score will now be 5

Comparison Operators

Comparison operators are used to compare two values and return a boolean value based on the comparison result. Here are the most common comparison operators in GDScript:

var result = 2 == 2 # result will be true

var result = 2 != 2 # result will be false

var result = 2 < 3 # result will be true

var result = 2 > 3 # result will be false

var result = 2 <= 2 # result will be true

var result = 2 >= 3 # result will be false

Logical Operators

Logical operators are used to perform operations on boolean values and return a boolean result. Here are the most common logical operators in GDScript:

var result = true and true # result will be true

var result = false or true # result will be true

var result = not false # result will be true

3D Platformer made with Godot 4

Control Flow Statements

Control flow statements allow you to control the flow of execution of your code based on certain conditions. Here are the most common control flow statements in GDScript.

If Statement

The if statement allows you to execute a block of code only if a certain condition is met. Here’s an example:

if 2 > 1: print("2 is greater than 1")

Output: 2 is greater than 1

If-Else Statement

The if-else statement allows you to execute a block of code if a certain condition is met, and another block of code if the condition is not met. Here’s an example:

if 2 < 1: print("2 is less than 1") else: print("2 is not less than 1")

Output: 2 is not less than 1

For Loop

The for loop allows you to execute a block of code repeatedly for a specific number of times. Here’s an example:

for i in range(3): print(i)

Output:

0

1

2

While Loop

The while loop allows you to execute a block of code repeatedly as long as a certain condition is met. Here’s an example:

var i = 0 while i < 3: print(i) i += 1

Output:

0

1

2

Survival game made with Godot 4

Functions

Functions allow you to group a set of related code together and reuse it whenever you need it. Functions make your code more organized and readable. Here’s how to define and use functions in GDScript.

Defining Functions

You can define a function using the “func” keyword, followed by the function name, a list of parameters in parenthesis, and a block of code inside curly braces. Here’s an example:

func greet(name): print("Hello, " + name)

greet("John")

Output: Hello, John

Return Statement

The return statement allows you to return a value from a function. Here’s an example:

func square(x): return x * x

result = square(3) print(result)

Output: 9

Optional Parameters

You can specify default values for parameters in case they are not provided when the function is called. Here’s an example:

func greet(name, greeting="Hello"): print(greeting + ", " + name)

greet("John")

Output: Hello, John

greet("Jane", "Hi")

Output: Hi, Jane

Skiing mini-game made with Godot 4

Classes and Objects

Classes and objects are fundamental concepts in object-oriented programming (OOP). They allow you to create complex data structures and provide a way to encapsulate data and behavior. Here’s how to use classes and objects in GDScript.

If you’re a bit lost on Godot’s scripting language GDScript at this point, it’s worth mentioning Zenva’s Godot 4 Game Development Mini-Degree. This comprehensive guide is fully focused on helping you master Godot 4 and GDScript, thus producing stunning, performance-optimized games. It’s an invaluable resource for both beginners and advanced developers who wish to boost their game creation process, and the perfect companion to this article.

Defining Classes

Every script in GDScript is inherently a class by default. If it is not given a name, it will be initialized as an unnamed class, we can give our script/class a name using the class_name keyword followed by the name we want to give it in PascalCase. Here’s an example:

class_name MyClass

var my_class_property = "Hello world"

func my_class_function():
    print(my_class_property)

Defining Inner Classes

You can define an inner class using the “class” keyword, followed by the class name and an indented block. Here’s an example:

class_name MyClass

class Person: var name

func greet(): print("Hello, my name is " + name)

person = Person.new() person.name = "John" person.greet()

Output: Hello, my name is John

Inner classes behave similar to regular classes, but they belong to the class they are declared in. You will only be able to access an inner class directly from the class that defines it. If you want to access it from another completely independent class, You will need to access the inner class on the class_name of the class that defines it. Here’s an example:

MyClass.Person.new()

Constructor

The constructor is a special method that is automatically called when an object is created from a class. You can use the constructor to initialize the object’s properties. Here’s an example:

class Person: var name

func _init(name): self.name = name

func greet(): print("Hello, my name is " + name)

person = Person.new("John") person.greet()

Output: Hello, my name is John

Inheritance

Inheritance allows you to create a new class that is a derived version of an existing class. The derived class inherits all the properties and methods of the base class. Here’s an example:

class Employee extends Person: var company

func work(): print(name + " is working at " + company)

employee = Employee.new("Jane") employee.name = "Jane" employee.company = "Acme Inc." employee.greet() employee.work()

Output:

Hello, my name is Jane

Jane is working at Acme Inc.

Polymorphism

Polymorphism allows you to use an object of a derived class anywhere an object of the base class can be used, because the derived class is considered a subclass of the base class. Here’s an example:

func introduce(person): person.greet()

introduce(person) introduce(employee)

Output:

Hello, my name is John

Hello, my name is Jane

Conclusion

Congratulations, you’ve now learned the basics of GDScript! You can use this knowledge to create your own games and applications in Godot Engine.

However, there is more to learn than just GDScript. Godot offers a ton of fantastic features and tools – from simple tilemap painters to performant rendering options. Plus, these coding principles – while important – aren’t of any use unless you learn how to apply them practically to game projects.

To end this article, we’ve included a bunch of helpful resources below to help you get started on the next steps in your Godot journey. We hope this helps, and we’re looking forward to seeing what you do with Godot 4!

Want a specific recommendation? Zenva’s Godot 4 Game Development Mini-Degree is an excellent resource to elevate your understanding and application of Godot’s scripting language. It’s an intricate course that walks you through creating both 2D and 3D games using Godot 4, with an in-depth focus on GDScript. Not only it aligns with our guide topic, but its thorough curriculum offers a progressive learning path from understanding the foundations of Godot 4 to building your specific game genres.

Primary Resources

Premium Godot Tutorials

Free Godot Tutorials

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

Python Blog Image - Complete Guide to GDScript - Godot's Scripting Language