GDScript Class_Name Tutorial – Complete Guide

Are you traversing the vast landscape of game development and curious about Godot’s scripting languages? If yes, get ready to explore one of Godot’s most useful language structures – the GDScript class_name. Diving into it can open up endless possibilities in your game development journey.

What is GDScript class_name?

The class_name keyword in GDScript is a built-in feature that delves into the heart of Object-Oriented Programming (OOP). It allows the creation and assignment of globally accessible class names. A class defines the characteristics and behaviors of an object, acting as a blueprint, and class_name lets you access the methods and properties of this blueprint anywhere in your scripts.

Why Should I Learn It?

Learning to use class_name in GDScript paves the way to cleaner and more structured codes. It encourages code reuse, enhances readability, minimizes errors, and allows efficient game development.

Whether you are an indie developer, a hobbyist, or even a coding newbie, harnessing the power of class_name can significantly ramp up your scripting prowess in Godot. Let’s delve into its nuances and see it in action.

CTA Small Image




Getting Started with GDScript class_name

First things first, to create a class_name in GDScript, we’ll need to open a script file and begin our code with the keyword class_name.

# Class name declaration
class_name Animal

This creates a class called ‘Animal’ which can be accessed globally across all scripts. But this class does not do much until we provide it some properties and methods.

Defining Properties and Methods

Within a class, we define properties (variables) and methods (functions). These allow the class to hold values and perform actions. Let’s give our ‘Animal’ class some properties and methods.

class_name Animal
var name = "unknown"
var age = 0

func greet():
	print("Hello, I'm an animal named " + name + "!")

In this example, our ‘Animal’ class has two properties: name and age. It also has a method ‘greet’ which prints a greeting message.

Creating an Instance of a Class

Now that we have a class, let’s create an instance of it. An instance is an individual object of a class.

var dog = = "Rex"
dog.age = 5

Here, we’ve created a ‘dog’ object of the ‘Animal’ class, assigned some properties, and called its ‘greet’ method. The output in the console will be “Hello, I’m an animal named Rex!”.

Accessing the Class Globally

Due to the global accessibility of class_name, you can also create an instance in another script. For instance:

# In another script
var cat = = "Whiskers"
cat.age = 3

This demonstrates how the ‘class_name’ keyword enables us to use classes throughout our scripts without needing to preload or import them explicitly — simplifying the process and improving our workflow.

GDScript class_name Inheritance

In OOP, a key feature is inheritance – where a class (child) can inherit the properties and methods from another class (parent). Let’s create a ‘Dog’ class that inherits from our ‘Animal’ class.

class_name Dog extends Animal
var breed = "unknown"

func bark():

Here, our ‘Dog’ class extends ‘Animal’, inheriting its properties and methods while specifying an additional property (‘breed’) and method (‘bark’).

Working with the Child Class

Creating an instance of the ‘Dog’ class and using it would work similarly as before:

var myDog = = "Lucky"    # Property inherited from Animal
myDog.age = 2           # Property inherited from Animal
myDog.breed = "beagle"
myDog.greet()           # Method inherited from Animal

The ‘greet’ method from the ‘Animal’ class can be used here because ‘Dog’ is a subclass (or child class) of ‘Animal’ and has inherited its properties and methods. The output will be a greeting from Lucky, followed by a “Woof!”.

Overriding Methods

You can also override methods from the parent class within the child class. Let’s modify our ‘Dog’ class:

class_name Dog extends Animal
var breed = "unknown"

func bark():

# Overriding the greet method
func greet():
	print("Hello, I'm a ", breed, " named ", name, "!")

When we call ‘greet()’ after instantiating ‘Dog’, it will now use the version of ‘greet’ defined in ‘Dog’, not ‘Animal’.

var myDog = = "Lucky"
myDog.breed = "beagle"
myDog.greet()    # Outputs: Hello, I'm a beagle named Lucky!

Learning to use the GDScript class_name in Godot is a game-changing move in your game development journey. It acts as a stepping stone towards efficient OOP in Godot, enhancing your code’s readability, structure, and reusability, simplifying your script interaction, and making the whole development process more adjacent to a professional standard. Onwards and upwards with Godot!

Demonstrating the Power of GDScript class_name

Let’s continue to dig deeper into the transformative capabilities of GDScript class_name by looking at the power it provides when working with scenes and nodes, signaling between scenes, and creating more complex programs.

GDScript class_name and Scenes

One of the distinct strengths of Godot is its scene structure, where each scene is its own unique class that can be instanced, inherit, and have properties and methods of its own. By using GDScript class_name in scenes, we can significantly improve our workflow. To bring this into practice, let’s assume we have a scene, “Enemy.tscn”. Assign the “Enemy” class_name to this scene in an attached script:

# Inside, assigned to the Enemy.tscn scene
class_name Enemy
var health = 100

func take_damage(damage):
	health -= damage

Now, we can instance the “Enemy” class from another script, or even from within the same script:

var myEnemy =
myEnemy.take_damage(50)   # Enemy's health is now 50

Signal Between Scenes

Godot’s signal system allows communication between different nodes and scenes. In conjunction with GDScript’s class_name, this makes for a powerful and dynamic toolkit.

For instance, we might want to send a signal from our “Enemy” class when its health reaches zero:

# Inside
class_name Enemy
var health = 100
signal enemy_died

func take_damage(damage):
	health -= damage
	if health <= 0:
		health = 0

With the enemy now emitting a signal on its death, we can detect it in another class by connecting to the signal.

# Inside
class_name OurHero
var score = 0

func _ready():
	var myEnemy =
	myEnemy.connect("enemy_died", self, "_on_enemy_died")

func _on_enemy_died():
	score += 100

Here, in the “OurHero” class, when an enemy dies, the “enemy_died” signal is received and the hero gains 100 points.

Creating a Game Manager Class

As a more advanced example, let’s look at how GDScript class_name can help create a Game Manager class that globally controls game states. This is a common design pattern in game development. By defining a GameManager as a class_name, we can make it accessible across all scripts.

# Inside
class_name GameManager
var score = 0
var level = 1
var lives = 3

func reset_game():
	score = 0
	level = 1
	lives = 3

func advance_level():
	level += 1

func lose_life():
	lives -= 1
	if lives == 0:

This Game Manager class can now be used in any other script :

var gameManager =
gameManager.advance_level()   # Levels up
gameManager.lose_life()       # Loses a life

Implementing class_name in your Godot projects is a step towards a more holistic and organized approach to game development. Dive in and incorporate them into your scripts. It might take a little while to get the hang of it, but once mastered, the potential of class_name knows no bounds!

Whetting your appetite with GDScript’s class_name is just the first step in a colorful journey towards mastering the Godot engine. Explore further, dig deeper, and keep experimenting. And remember, stumbling blocks are inevitable on the learning curve – treat them as stepping stones, not deterrents to your game development quest.

Feeling ready to rise to another level in your Godot journey? We invite you to visit the Godot Game Development Mini-Degree offered by us at Zenva Academy. This comprehensive collection of courses delves into various facets of building games using the Godot engine. It covers essential aspects like rendering graphics, using GDScript, and creating both 2D and 3D games using various game genres. Each course incorporates the learning-by-doing approach, coupled with expert guidance from experienced game developers and certified coders.

Beyond just this mini-degree, we also provide a wide variety of Godot Courses that cater to different learning goals and preferences. Start writing your game development odyssey with Zenva Academy – the one-stop learning platform where over a million learners have elevated their coding and game development skills. Happy coding!


Understanding and incorporating the GDScript class_name is an impressive addition to your Game Development toolkit. It streamlines the codebase, fosters scalability, and advances the complexity of games you can create using Godot. The power of <strong class_name awaits for you to harness it and create vibrant, interactive games that tell compelling stories.

No journey is complete without an insightful guide, and at Zenva, we celebrate being that guiding presence in your game development journey. We invite you to explore the Godot Game Development Mini-Degree that we offer. Dive into the world of Godot, GDScript, and make amazing strides in your journey from being a novice game developer to a competent GameDev wizard. Are you ready to level up?


Python Blog Image

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