godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.11k stars 69 forks source link

Add first-class Type support to GDScript #10115

Open DevSavvySerge opened 2 months ago

DevSavvySerge commented 2 months ago

Describe the project you are working on

I'm working on a Vampire Survivor-like game where I intend to use best practices of Object-Oriented (OO) programming. This includes principles like polymorphism and dependency injection to create a dynamic and maintainable codebase.

Describe the problem or limitation you are having in your project

Describe the problem or limitation you are having in your project

GDScript currently lacks first-class support for types. For instance, there is no Type type, and while there is an enumeration for built-in types, it is insufficient for more complex needs. This limitation prevents using types as dictionary keys, among other issues. As a workaround, I have to manually define type-related constants in each class, such as:


class_name Player
extends CharacterBody2D
const nameOf: StringName = StringName("Player")

This is cumbersome and makes it challenging to create dynamic code handling.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose adding first-class type support to GDScript. This would include introducing a Type type and enhancing the existing type system to allow types to be used as dictionary keys and other similar use cases. This feature would streamline the development process by reducing the need for manual workarounds and enabling more dynamic and flexible code.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Introduce a Type type in GDScript:


var my_type: Type = typeof(Player)

Allow types to be used as dictionary keys:


var type_dict: Dictionary = {}
type_dict[typeof(Player)] = "This is a player type"

If this enhancement will not be used often, can it be worked around with a few lines of script?

The current workaround involves manually defining type-related constants in each class, which is neither efficient nor scalable. While possible, this approach is far from ideal and can lead to code that is harder to maintain and extend.

Is there a reason why this should be core and not an add-on in the asset library?

First-class type support is a fundamental feature that would benefit many developers using GDScript. Implementing it as a core feature would ensure consistency, better integration with the language's existing type system, and improved performance compared to an add-on. It would also foster best practices in OO programming within the Godot engine.

InkONat commented 1 month ago

this would probably be a change only for a future Godot 5, it is a very big change