Menu

GDScript Inheritance: extends, super() and Overriding

How inheritance works in GDScript: extend a class, override methods, call the parent with super, pass arguments to the parent constructor, and treat child classes as their parent type.

This page includes runnable editors. Edit, run, and see output instantly.

A class can extends another class. It gets every variable and method of its parent, and can add or replace them:

a knight with 10 hp
7 3

Your scripts already use inheritance: extends Node2D makes the script a Node2D, with position, rotation and every other property of that class.

Calling the Parent with super

An override often adds to the parent's behavior instead of replacing it. super.method() calls the parent's version:

Ana (30 hp), armor 3
Bo (10 hp)
  • super(...) inside _init() runs the parent constructor with arguments. Call it before using the parent's members.
  • super.describe() runs the parent's describe().
  • Plain super() in any method calls the parent's version of that same method.

Godot calls _ready() on a child script's node and, if the parent script defines _ready() too, only the child's runs. Call super() at the start of the child's _ready() when the parent's setup must still happen.

One Type, Many Classes

A child is also its parent type, so one list can hold them all and each object runs its own version of a method:

screech
rumble
...
true true false

This is how a game treats every enemy the same in a loop (move, take damage, die) while each type behaves differently.

Extending Another Script

Across files, extend a class by its class_name, or a script by its path:

# boss.gd
class_name Boss
extends Enemy            # enemy.gd has class_name Enemy

func take_damage(amount: int) -> void:
	super.take_damage(amount / 2)   # bosses take half damage
extends "res://enemies/enemy.gd"

A class has exactly one parent. To share behavior across unrelated classes, add a child node that does the job (composition), which Godot's node system is built for.

Abstract Classes

Since Godot 4.5, @abstract marks a class you only extend and never create, and methods children must provide:

@abstract
class_name Weapon
extends Node2D

@abstract func fire() -> void

func reload() -> void:
	print("Reloading")

Weapon.new() is then an error, and a child that does not define fire() cannot be created either.

Inheritance or Composition?

Godot favors composition: a Player scene with a Health child node, a Hitbox child and a Sprite2D child, each with its own small script, instead of a deep chain of classes. Inherit for real "is a" relations (a Boss is an Enemy). Add a node for "has a" relations (an enemy has health).

Frequently Asked Questions

How do I call the parent function in GDScript?

Use super: super.attack() calls the parent class's attack() from inside the override, and plain super() calls the parent version of the function you are in. In Godot 3 this was written .attack().

How do I pass arguments to the parent constructor?

Call super(...) as the first line of the child's _init(): func _init(): super("Knight", 30). If you do not call it, the parent's _init() runs with no arguments, which fails when it requires some.

Can a GDScript class extend another script?

Yes. Extend a named class (extends Enemy) when it has a class_name, or a script by path: extends "res://enemy.gd". A class extends exactly one parent; GDScript has no multiple inheritance.

Does GDScript have abstract classes?

Yes, since Godot 4.5: write @abstract before class_name to forbid new() on the class, and @abstract func for methods children must define. Before 4.5, a common habit was a parent method that calls push_error("not implemented").

Coddy programming languages illustration

Learn to code with Coddy

Get started