Static 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

Static Function

A static function is a function that only runs calculations on its inputs and returns a value. It is bound to a class rather than an object (an instance of that class).

This means that static functions don't have access to the properties of the class, which only exist on instances. This doesn't sound too useful, does it?

Well, the good news is, you can call a static function without creating an instance of the class! They're great for utility functions that don't need to know anything about the class they're in.

You could use static functions for things like:

And more.

To define a static function in GDScript, you use the static keyword before the function definition. Here's an example of a function that finds all the files with a specific file extension in a directory (".png", ".mp3", etc.):

static func find_files_in_directory(directory_path: String, file_extension: String) -> Array[String]:
	var file_names := DirAccess.get_files_at(directory_path)
	var file_paths: Array[String] = []
	for file_name in file_names:
		if file_name.get_extension() != file_extension:
			continue
		file_paths.append(directory_path.path_join(file_name))
	return file_paths
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!