Does a type alias exist for the class_name feature of GDScript?

Godot Version



Hi everyone, I’m really enjoying the engine, no deal breakers whatsoever.

My question is regarding the class_name feature of GDScript.

By declaring a class_name inside a .gd extension script, it seemingly becomes accessible to all other .gd extension script in the same project. Functioning like some kind of global namespace.

This has been very useful for static utility functions as is described in this brief section: Autoloads versus regular nodes — Godot Engine (stable) documentation in English

But I can’t help but wonder…here are my questions:

1) Is there a class_name alias feature similar to either the C++ language’s using x = y or the Python language’s from x import y as z? These kinds of features enable me to write long and descriptive class names for readability when reviewing my own code later while also letting me write calling lines of code that look much more concise by simply adding a few long lines at the beginning of a file.

e.g. C++ (without type alias)

if( someLongClassNameForTheSakeOfBeingObtuseOrIgnorantAboutSomeOtherAlternativeMechanismAlreadyAvailableInTheGDSCriptLanguage::bool_foo1() && someLongClassNameForTheSakeOfBeingObtuseOrIgnorantAboutSomeOtherAlternativeMechanismAlreadyAvailableInTheGDSCriptLanguage::bool_foo2)

e.g. C++ (with type alias)

using x = someLongClassNameForTheSakeOfBeingObtuseOrIgnorantAboutSomeOtherAlternativeMechanismAlreadyAvailableInTheGDSCriptLanguage;

if(bool_foo1() && bool_foo2())

2) If no such feature nor alternative mechanism exists within GDScript, is there a reason or weakness responsible for the absence of such a feature? I’ve found myself regretting calling my utility class “UtilityHexagon” when I have nested and used dozens of constants declared inside of it under the same line of code.

1 Like

Hi there, and welcome to the forum! :gdparty:

There is no alias feature in Godot. However, you can use any script as a type by preloading it into a constant. You don’t need to use class_name to use a script as a type.

Here is an example:

extends Node

# Preload a script into a constant
const Foo := preload("res://path/to/")

# Use as type hint
var foo: Foo

func _ready() -> void:

    # Use static methods or constants from the type

    # Or instantiate it just like any other type
    foo =

You can also do it like this. You don’t need to preload the class as gdscript loads it automatically because of the class_name declaration. Works with inner classes too.

Let’s say you have this class file below,

class_name MyLongUtilsClass

var s:String="123"

static func show(somevar: Variant):

class MyInner extends MyLongUtilsClass:
	const inn:String="inner"
	static func show(somevar: Variant):

In your

extends Control

# Alias class
class U extends MyLongUtilsClass:

# Alias inner class
class Inn extends MyLongUtilsClass.MyInner:

func _ready():"whatever")

In most cases I would not suggest to do this. It creates a new distinct subclass, possibly breaking type checks.

If the purpose is just to access some static members, then sure it’s fine. But as soon as you do a type check of obj is U it will break if the object came from somewhere outside this script and only inherits the parent class.

Or more specifically, an object created with var obj := will return false for obj is U.

So while this works for the specific usecase mentioned in the original question, it is a lot less flexible in general for creating aliases. You need to be aware that this is a fully fledged subclass, coming with all the OOP implications this entails.

Not sure, looks cleaner plus easy to extend classes. But whatever works,

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.