GDScript Basics Tutorial – Complete Guide

Welcome to this tutorial on GDScript basics. As we dive into this powerful scripting language used in the Godot game engine, we’ll uncover the fundamentals and illustrate concepts through engaging examples. Our aim is to make the subject approachable, meaningful, and interactive, no matter where you are on your coding journey.

What is GDScript?

GDScript is a high-level, dynamically typed programming language specifically designed for the Godot game engine. Modeled on Python, it uses a syntax that is easy to read and intuit, lowering the entry barrier for beginner coders.

What is its purpose?

In the context of game development, GDScript allows developers to quickly and efficiently script gameplay mechanics, User Interface (UI), character behaviors, and more within Godot. It’s deeply integrated with the engine, for seamless and powerful functionality.

Why Learn GDScript?

Learning GDScript opens up a world of opportunities in game development. As the primary language of the open-source Godot game engine, proficiency in GDScript enables you to build and prototype games rapidly. Plus, given its python-like syntax, it serves as an excellent stepping stone to other programming languages.

CTA Small Image

Getting Started with GDScript

Let’s dive into some basic examples to help you get familiar with the syntax and structure of GDScript.

1. Variables

In GDScript, variables do not need to be declared before use. You can create a new variable simply by assigning a value to it. Here’s how to do it:

var my_var = 5

In addition, GDScript supports type hinting. If you wished to specify the type of your variable, you could do so like this:

var my_var: int = 5

2. Functions

A function in GDScript is defined using the ‘func’ keyword, followed by the function name and parentheses. Observe how the indentation determines the lines of code that belong to the function. Here’s a simple function that prints a message:

func my_function():
    print("Hello, Zenva!")

Functions can also take arguments and return values:

func add_two_numbers(num1, num2):
    var result = num1 + num2
    return result

3. Conditionals

GDScript uses the ‘if’, ‘elif’, and ‘else’ keywords to handle conditional logic, just like Python. Here’s a simple example of a conditional check:

var my_var = 10

if my_var > 5:
    print("The variable is greater than 5")
    print("The variable is not greater than 5")

4. Loops

GDScript supports both ‘for’ and ‘while’ loops for looping over sequences or repeated tasks. Here’s an example of each:

for i in range(5):

var i = 0

while i < 5:
    i += 1

These are some of the most basic building blocks of GDScript. However, GDScript also supports more complex structures and functionalities that we will dive into next.

More about Functions

In addition to the basic function structure, GDScript supports default argument values, making certain arguments optional.

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

In this function, if no argument is provided when invoking the function, “Stranger” will be used as the default value for ‘name’.

Classes and Instances

Classes in GDScript are defined using the ‘class’ keyword, followed by the class name. Here’s a simple character class with a single property and a method:

class Character:
    var name

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

Once defined, a class can be instantiated to create objects, as follows:

var my_character = = "Bob"

Array and Dictionary

GDScript includes built-in types for arrays and dictionaries. Here’s how to create and manipulate an array:

var my_array = [1, 2, 3, 4]
my_array.append(5)  # Adds 5 to the end of the array
print(my_array[0])  # Prints the first element of the array (1)

And here’s an example for dictionaries:

var my_dict = {"name": "Bob", "age": 30}
print(my_dict["name"])  # Prints “Bob”
my_dict["height"] = 180  # Adds a new key-value pair to the dictionary

Error Handling

GDScript supports error handling through ‘try’ and ‘catch’ blocks. Here’s an example of handling an error that occurs when trying to access a non-existent dictionary key:

var my_dict = {"name": "Bob", "age": 30}

    print("The key 'height' does not exist in the dictionary")

Whether you’re new to game development or seasoned coding warrior, understanding GDScript paves the way to making engaging games in the Godot engine. Armed with these basics, challenge yourself to learn more and start creating your own projects today!

OOP Concepts in GDScript

GDScript follows Object-Oriented Programming (OOP) principles, including inheritance and encapsulation. Let’s delve deeper into these topics.


One class can inherit from another class, acquiring its properties and methods. This is done by extending the parent class. For instance, you might have a “Human” class, and a “Player” class that extends Human:

class Human:
    var name
    var age

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

class Player extends Human:
    var score

    func display_score():
        print('My score is ' + str(score))

Now, any instance of the Player class can use both the greet() method from Human and the display_score() method from Player.


In OOP, encapsulation is the practice of hiding the internal details of objects. In GDScript, you can use underscores to denote variables and methods that should be treated as “private”. While not strictly enforced, it’s a good practice to follow:

class Player:
    var _health  # This should ideally be accessed through methods, not directly

    func get_health():
        return _health

    func set_health(value):
        _health = value

Built-in Functions and Keywords

GDScript includes numerous built-in functions and keywords to make your coding life easier. Here are a few examples:

print() – Prints to the stdout. Used for debugging:

print('Hello, Zenva!')

len() – Returns the length of a string or the size of an array or dictionary:

var my_list = [1, 2, 3]
print(len(my_list))  # Prints 3

str() – Converts a value to a string:

var my_num = 10
print('My favorite number is ' + str(my_num))

‘in’ – Checks if a value is in a sequence or a dictionary:

var my_list = [1, 2, 3]
print(2 in my_list)  # Prints True

As you wield GDScript with aplomb, you will realize that GDScript acts as a bridge to communicate not only with the Godot game engine but, most importantly, with the aspirations of the game developer – You.

Where to Go Next?

Now that you’ve taken your first steps to understanding GDScript and its potential, it’s time to take control of your coding journey. The journey of learning and mastering anything worthwhile is a marathon, not a sprint, and it’s critical to keep pushing your limits and stretching your comfort zone.

At Zenva, we offer a comprehensive range of beginner to professional courses in programming, game development, and AI. With over 250 supported courses, you can learn to code, create games, and even earn certificates. And remember, learning is a continuous journey – we have courses tailored not only for beginners but also for those who have already tackled the basics and want to dive deeper.

To turbocharge your foray into game development, check out our Godot Game Development Mini-Degree. This collection of courses covers diverse aspects of game development, including 2D and 3D constructs, control flow, combat mechanics, UI systems, and various game mechanics, all powered by the flexible, open-source Godot 4 engine. Or for a broader selection, explore our collection of Godot Courses to find the learning path that suits your goals best.

Thanks for joining us in this journey. Remember, coding is not an endpoint, but an ongoing adventure. Stay curious, and happy coding!


Mastering GDScript is like unlocking a new level in your game development journey. With each new concept you grasp and every script you write, you’re that much closer to bringing your game ideas to life in the fantastic Godot game engine. It’s not just about adding a new skill to your coding arsenal, but about empowering yourself with new tools to unleash your creative potential.

Whether you’re just starting your journey or looking to up your game, we at Zenva are by your side. Let’s turn the tables on coding—it’s not just about instructing a computer; it’s about bringing your dreams to digital life. Ready to teleport to your next level? Let’s start coding!

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

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.