IssueRaiser

Add a Trait system for GDScript

### Describe the project you are working on The GDScript implementation. ### Describe the problem or limitation you are having in your project The main to reuse code in GDScript is via inheritance. One can always use composition, either via instances stored in class variables or as nodes in the scene tree, but there's a lot of resistance in adding a new prefix to accessing some functionality in the class, so many go to the inheritance route. That is completely fine if the inheritance tree includes related scripts. However, it's not uncommon for users to create a library of helper functions they want to have available in every script, then proceed to make this the common parent of everything. To be able to attach the sub-classes to any node, they make this common parent extend `Node` (or even `Object`) and rely on the quirk of the system that allows attaching scripts to a sub-class of the type it extends (cf. godotengine/godot#70956). The problem with this usage is that a script gets a bit of "multiple inheritance" which is not really supported by GDScript. One script can now extend another that ultimately extends `Node` but it can also use the methods of, say, a `Sprite2D` when attached to a node of such type, even when not directly on the inheritance line. Here I put a more concrete example to understand the issue:[^self] ```gdscript # base.gd extends Node class_name Base # ------- # sprite_funcs.gd # Attached to a Sprite2D. extends Base func _ready() -> void: # Her

Posted by vnen on Mar 3, 2023, 6:19 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

github (Other)

Add a Trait system for GDScript

vnenMar 3, 2023, 6:19 PM

### Describe the project you are working on The GDScript implementation. ### Describe the problem or limitation you are having in your project The main to reuse code in GDScript is via inheritance. One can always use composition, either via instances stored in class variables or as nodes in the scene tree, but there's a lot of resistance in adding a new prefix to accessing some functionality in the class, so many go to the inheritance route. That is completely fine if the inheritance tree includes related scripts. However, it's not uncommon for users to create a library of helper functions they want to have available in every script, then proceed to make this the common parent of everything. To be able to attach the sub-classes to any node, they make this common parent extend `Node` (or even `Object`) and rely on the quirk of the system that allows attaching scripts to a sub-class of the type it extends (cf. godotengine/godot#70956). The problem with this usage is that a script gets a bit of "multiple inheritance" which is not really supported by GDScript. One script can now extend another that ultimately extends `Node` but it can also use the methods of, say, a `Sprite2D` when attached to a node of such type, even when not directly on the inheritance line. Here I put a more concrete example to understand the issue:[^self] ```gdscript # base.gd extends Node class_name Base # ------- # sprite_funcs.gd # Attached to a Sprite2D. extends Base func _ready() -> void: # Her

Comments (0)

No comments yet. Start the discussion.

Comment on this issue