GDScript vs Python Tutorial – Complete Guide

Welcome to our exciting exploration of two potent scripting languages pivotal in game development – GDScript and Python. The aim of this tutorial is to delve into the unique strengths, intricacies, and applications of these languages, giving you a well-rounded understanding and enabling you to make informed decisions in your game development journey.

What is GDScript?

GDScript is a high-level, dynamically typed programming language closely modeled after Python, largely used within the Godot game engine. It is designed with simplicity and ease of use in mind, making it highly intuitive to any game developer with Python knowledge.

What about Python?

Python, on the other hand, is a widely popular, versatile, and easy-to-understand programming language used in a vast array of applications – from web and game development to AI and data analysis – making it a valuable skill in any coder’s toolkit.

What’s This Tutorial For?

This tutorial will serve as a critical guide, illuminating the similarities and differences between GDScript and Python with practical coding examples. Whether you’re just starting out or an experienced coder, understanding these languages paves the way for a robust and versatile game development skill set.

Why Learn GDScript and Python?

Game development often involves choosing the right tools for the job. By learning both GDScript and Python, you’ll be prepared to work within the Godot engine, while also leveraging Python’s far-reaching capabilities outside of it. This knowledge opens the door to a multitude of programming opportunities, leveling up your game creation skills. Remember, greater adaptability lends more power as a developer!

Are You Ready?

Don’t worry if you’re a beginner or an expert coder. This guide is structured to cater to both, presenting concepts in a digestible and engaging way. So, buckle up and let’s embark on this coding adventure together!

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Starting with the Basics: Defining Variables

In both GDScript and Python, the process of declaring variables is quite similar. Let’s look at how to define variables in each language.

GDScript

var name = "Zenva"  
var age = 10  
var is_online = true

In GDScript, variables are declared using the “var” keyword.

Python

name = "Zenva"
age = 10
is_online = True

In Python, there is no need for a specific keyword to declare a variable. The variable type is identified by the value it’s assigned.

Working with Loops

Looping is a crucial concept in any programming language. Let’s check out how each language performs a basic “for” loop.

GDScript

for i in range(0, 10):  
    print(i)

In GDScript, the syntax for “for” loops is very similar to Python. Here, we loop from 0 to 9, printing each number.

Python

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

In Python, this “for” loop does the exact same function. It loops from 0 to 9, printing each number.

If-Else Conditions

Conditional statements serve as the cornerstone of logical programming in both Python and GDScript.

GDScript

if age > 18:  
    print("Adult")
else:
    print("Not an adult")

GDScript uses if-else conditions much like Python.

Python

if age > 18:
    print("Adult")
else:
    print("Not an adult")

Python’s if-else statements share a similarity with GDScript’s syntax, making it easy for developers to migrate between the two.

Defining Functions

Understanding how to define functions is essential as it promotes code reusability and efficiency.

GDScript

func greet():
    print("Hello, world!")

In GDScript, functions are defined with the “func” keyword.

Python

def greet():
    print("Hello, world!")

In Python, the “def” keyword is used to define functions. As you can see, while the terminology is different, the structure of function definitions is strikingly similar in both languages.

Using Lists

Lists, or arrays, are a staple of data manipulation in programming. They are a sequence of elements that can be of any type, allowing for flexible data management.

Python

my_list = ["apple", "banana", "cherry"]
print(my_list[0])  # will print "apple"
print(my_list[-1])  # will print "cherry"

In Python, lists are defined using square brackets and accessed by index. Python also supports negative indexing, where an index of -1 refers to the last element.

GDScript

var my_list = ["apple", "banana", "cherry"]
print(my_list[0])  # will print "apple"
print(my_list[my_list.size() - 1])  # will print "cherry"

In GDScript, lists are also defined using square brackets. Whilst not supporting negative indexing, the last element can still be accessed by using the list’s size.

Looping through Lists

Both languages offer a simplified way to iterate through a list with “for” loops.

Python

for fruit in my_list:
    print(fruit)

In Python, you can iterate directly over the elements of the list.

GDScript

for fruit in my_list:
    print(fruit)

GDScript has adopted this intuitive syntax of iteration as well, making looping through lists a breeze.

List Operations

Let’s quickly look at some common operations on lists, like adding and removing elements.

Python

my_list.append("orange")  # adds "orange" at the end
my_list.remove("apple")  # removes "apple" from the list

In Python, the `append` and `remove` methods are used for adding to the end and removing elements respectively.

GDScript

my_list.append("orange")  # adds "orange" at the end
my_list.erase("apple")  # removes "apple" from the list

GDScript uses similar `append` method to add to the end of the list. For removing elements, `erase` method is used. It’s almost like speaking two similar languages – Python’s widespread use made it a good influence on GDScript’s design!

Dictionaries and Hashmaps

Python uses dictionaries, and GDScript uses dictionaries too, which function similar to Python’s. They are incredibly beneficial for storing and organizing data.

Python

my_dict = {
    "brand": "Zenva",
    "year": 2022
}
print(my_dict["brand"])  # will print "Zenva"

Python uses the “{‘key’: value}” design to form dictionaries, and the value of a key can be accessed directly.

GDScript

var my_dict = {
    "brand": "Zenva",
    "year": 2022
}
print(my_dict["brand"])  # will print "Zenva"

Again, GDScript follows Python’s lead. The use of dictionaries in GDScript mirrors Python’s syntax and usage.

As you can see, both GDScript and Python have quite similar structures and commands, especially for the foundational coding techniques. However, while Python is a general-purpose language with uses beyond gaming, GDScript’s power and simplicity shine within the Godot Engine environment, making it a go-to for game development in Godot. Of course, the choice of language ultimately falls in line with your needs and goals as a developer.

Classes and Objects

One crucial aspect of programming is Object-Oriented Programming (OOP). Both GDScript and Python support this paradigm, allowing you to create reusable components called classes for your game.

Python

class MyClass:
    x = 5

p1 = MyClass()
print(p1.x)  # will print "5"

Python uses the “class” keyword to create classes, and objects can be created from these classes.

GDScript

class MyClass:
    var x = 5

var p1 = MyClass.new()
print(p1.x)  # will print "5"

GDScript again follows a similar approach. Classes are created using the “class” keyword, while objects are created with “ClassName.new()” syntax.

Class Methods

Methods define behavior for a class. Classes in both Python and GDScript can have functions (methods) inside them.

Python

class MyClass:  
    def greet(self):  
        print("Hello, Zenva!")

p1 = MyClass()  
p1.greet()  # will print "Hello, Zenva!"

In Python, class methods are defined like normal functions, but with a mandatory “self” parameter to access instance variables or other methods.

GDScript

class MyClass:
    func greet():  
        print("Hello, Zenva!")

var p1 = MyClass.new()  
p1.greet()  # will print "Hello, Zenva!"

GDScript’s method creation similarly leverages the intuitiveness of Python, but there’s no need for the “self” keyword as it’s implicit.

Instance Variables

Instance variables are those that belong to objects of a class. They hold object-specific data, making classes reusable yet unique as per the created object.

Python

class MyClass:
    def __init__(self, name):
        self.name = name

p1 = MyClass("Zenva")
print(p1.name)  # will print "Zenva"

In Python, instance variables are defined in the “__init__” method with “self.variable_name”.

GDScript

class MyClass:
    var name

    func _init(name):
        self.name = name

var p1 = MyClass.new("Zenva")
print(p1.name)  # will print "Zenva"

GDScript employs the same logic with “_init_” acting as the constructor, and “self.variable_name” being the syntax for instance variables.

Signals (GDScript only)

GDScript has a unique feature called “signals” designed specifically for the Godot game engine. Signals are a way for nodes to communicate with each other, keeping your game elements modular and well-structured.

signal game_over

func end_game():
    emit_signal("game_over")

Here, a signal named “game_over” is defined, and the “emit_signal” function broadcasts that signal.

As we’ve seen, both Python and GDScript share much in common, making it easy for anyone familiar with Python to pick up on GDScript. Whether it’s declaring variables, defining functions, working with lists, creating classes, or using any standard programming syntax, the two languages mirror each other.

However, they do differ in their contexts and use cases. While Python is a versatile, high-level, general-purpose programming language used in diverse fields, GDScript, while also being high-level and versatile, is specifically designed with the Godot game engine in mind, giving it additional features and benefits within that sphere, like Signals.

But no matter the language you choose, you’re now equipped with the knowledge to embrace either one – ready to level up your game development journey with us at Zenva.

Where to Go Next?

Now that you are familiar with GDScript and Python, you may be eager to dive into creating your own games. To help you continue on this exciting journey, we at Zenva recommend checking out our comprehensive Godot Game Development Mini-Degree. This collection of courses is specifically designed to equip you with everything you need to start building games using the lightweight, free, and open-source Godot 4 engine.

The curriculum covers a broad range of topics, from using 2D and 3D assets, GDScript programming, controlling gameplay flow, to developing combat systems and UI systems. Each module is project-based, offering an immersive learning experience that stands you in a good position in the game development industry. What’s more, it caters to diverse skill levels, making it accessible to beginners or those unfamiliar with coding.

Additionally, we have an extensive collection of Godot Courses, that will give you a more in-depth understanding of the engine’s various features and capabilities. Here at Zenva, we empower you to move from beginner to professional at your pace. So, why wait? Jump right into the exciting world of game development with us and take your coding skills to new heights.

Conclusion

Congratulations, you’ve embarked on an epic journey exploring the fascinating world of GDScript and Python, opening up new horizons in your game development adventure. You are now equipped with knowledge that will further enhance your coding skills and versatility as a game developer.

Ready to put this newfound knowledge into action? Dive into Zenva’s comprehensive Godot Game Development Mini-Degree to push the boundaries of your expertise. We’re with you every step of the way, honing your skills and propelling you towards your development dreams. Your game development saga has just begun! Join us at Zenva, and let’s script an exciting future together.

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

FREE COURSES
Python Blog Image

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