willnationsdev / log

A log of my ongoing and planned development efforts.
MIT License
0 stars 0 forks source link

Godot 3.2: Implement Extension Methods for GDScript #12

Open willnationsdev opened 5 years ago

willnationsdev commented 5 years ago

C# has "extensions methods" whereby you define a static function that passes in an object parameter as its first parameter. These specially registered extension methods can then be called ON the class in question where all successive parameters assume the role of parameters 0, 1, 2, etc. The static method's first parameter is used in place of the "this". While you only have access to the public data of the extended class, this technique enables you to add new methods to pre-existing types.

My vision for doing this in GDScript is something like this:

# some_script.gd
static Node do():
    print("hello")
# new_node.gd
extends Node
func _ready():
    do() # this is now a "built-in" function for the class

Follow progress on my branch.

willnationsdev commented 5 years ago

Someone mentioned that a future version of Godot might support this. Don't know if they were talking about this implementation or not. Since I haven't had a chance to consult with the engine developers, I need to coordinate with them before continuing on this implementation. Also, I have run into issues identifying the best way of actually processing the GDScriptParser changes that propertly load and call the method from script with extension methods. It is also slightly messy since the scripts don't outright "register" themselves as extension scripts for a class or anything like that.

Honestly, a system-wide abstraction layer that can support the feature for all languages would be.........a lot better. So, need to look into that possibility.