Static Function
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
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_pathsDon'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.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…