Description
Describe the project you are working on
Godot core.
Describe the problem or limitation you are having in your project
A first-class types is when you can ask an Object for its type, and then use or interact with that type as if it were an object itself.
Godot is lacking first-class types.
Instead, ClassDB
usually stands in for class-related functionality, such as:
- Whether
A
inherits fromB
(although admittedly,Object
duplicates this behavior). - The parent class name of
A
(get_parent_class_nocheck
) - The class name hierarchy of
A
(get_inheritance_chain_nocheck
) - Whether
A
is virtual (is_virtual
) - Which properties
A
has (get_property_setter
) - Which virtual methods
A
has (add_virtual_method
) - ... and, well, almost everything else type related.
This is unidiomatic and slow:
- It requires the global read lock for every type query, even for trivial questions such as the parent class.
- It requires
HashMap
lookups by class name, even if the type is known statically.
Meanwhile, Object
also offers some of its own class-like functionality as virtual function hacks. For example, is_class
is a virtual function that goes through each subclass, and checks a single StringName
against the input string. It would be much faster if there was a single, static, contiguous list of the class name hierarchy to make a quick check.
- This is a necessary pre-step for Add first-class Type support to GDScript #10115.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
It would be better if class information was stored in first-order Object
types, rather than scattered around ClassDB
hash maps and Object
virtual functions.
First-order could types be accessed by pointer, which can be far faster than the previously mentioned hacks. It can also be more easily reasoned with.
The types can (later) be expanded to be used by Variant
types as well, to de-duplicate functionality.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Object
instances need a pointer to their type (GDType
).
We can start with a simple implementation that stores information such as super type and class name.
Later, we can add other functionality such as method and property infos (basically most of what's stored in ClassDB
right now).
If this enhancement will not be used often, can it be worked around with a few lines of script?
Nope.
Is there a reason why this should be core and not an add-on in the asset library?
It's core.