Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first-class Type support to GDScript #10115

Open
DevSavvySerge opened this issue Jul 4, 2024 · 1 comment
Open

Add first-class Type support to GDScript #10115

DevSavvySerge opened this issue Jul 4, 2024 · 1 comment

Comments

@DevSavvySerge
Copy link

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.

@AThousandShips AThousandShips changed the title Enhancement Request: Add First-Class Type Support in GDScript Add First-Class Type Support in GDScript Jul 4, 2024
@Calinou Calinou changed the title Add First-Class Type Support in GDScript Add first-class Type support to GDScript Jul 4, 2024
@InkONat
Copy link

InkONat commented Jul 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants