Virtual Function

2025/12/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
Assets
All else
Copyright 2016-2026, GDQuest
Created
2026/02/16
Updated
2025/12/27

Virtual Function

Godot provides some functions that are empty and do nothing by default. They exist only to override them, and they don't do anything otherwise. We call these virtual functions.

For example, the _ready() and _process() functions are virtual. They are defined in the engine and don't do anything by default.

Virtual functions allow Godot to assume that every node has these functions, and the engine can call them without checking if they exist. If a node doesn't have a script in your project, internally, Godot will still call these functions blindly, and nothing will happen. If the node has a script that overrides these functions, your code will run.

Some of the most used virtual functions are:

Virtual functions in GDScript

GDScript doesn't exactly have virtual functions, but you can create your own virtual functions by creating empty functions that you expect to be overridden. For example, here's a class named Unit with a virtual function take_damage():

class_name Unit

var health := 10

func take_damage(_amount: int) -> void:
    pass

In a class that extends the Unit class, you can override the take_damage() function to make it do something. Here, it reduces the health of the unit by the given amount and ensures the health doesn't go below zero:

class_name Player extends Unit

func take_damage(amount: int) -> void:
    health = max(0, health - amount)
Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!