Skip to content

GDScript: Add keyword to access the current class #391

@ChibiDenDen

Description

@ChibiDenDen

Describe the project you are working on:
gdscript

Describe the problem or limitation you are having in your project:
No access to class from the following places:

  1. From static functions when unnamed Can't use new() or reference own class name in static function (fixed in master) godot#27491
    Mostly for factory methods trying to call new

  2. As type from within the same class when unnamed
    For example:

    • As return type for function
    • As variable type
    • etc..
  3. As type from within the same class even when named
    Allow scripts marked with class_name to use their class within the script without causing a cyclic dependency godot#25252

Describe how this feature / enhancement will help you overcome this problem or limitation:
The keyword will allow access to the “static_class” Type, thus allowing access to the class type without giving it a name
Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
Factory function with this_class return type, this_class local variable and access to this_class.new:

func make_with_color(color) -> this_class:
	var obj : this_class = this_class.new()
	obj.color = color
	return obj

Describe implementation detail for your proposal (in code), if possible:
This will be implemented as an additional keyword to the parser and compiler of gdscript
the keyword I used for my POC was "this_class" but I would be more than happy to hear any better suggestions.
See Commit for actual code:
ChibiDenDen/godot@2ff8c1b

If this enhancement will not be used often, can it be worked around with a few lines of script?:
The current “script” solution is to use the following expression to get the static class
load(“res://path_to_script_file.gd”)
Which is a hacky way, creates a dependency on the scripts path and looks bad
Furthermore, this is not documented and very hard for new users to find.

Is there a reason why this should be core and not an add-on in the asset library?:
This is part of gdscript syntax

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions