Godot Version: 4.5-beta4
Hi! For specific reasons, I would like to develop my game without classes but by extending scripts of scenes.
Basically, instead of:
# player.class.gd
class_name Player
extends Node
# player.gd
extends Player
Do:
# player.class.gd
extends Node
# player.gd
extends "res://player.class.gd"
It works exactly as a class definition except you don’t get to instantiate nodes as your class and you cannot use it as a global name on other scripts, meaning you can’t:
func _on_player_joined(player: Player) -> void:
return player.name
as it would give an error: Error at (1, 30): Could not find type "Player" in the current scope.
Still, you can just remove the assignment:
func _on_player_joined(player: Node) -> void:
return player.name
and it would work just fine because Godot allows duck typing.
My question is: is it possible to define a class separate to the scene that’s actually going to be used, but with the same properties and infer that onto the classless node? As much as I need this feature for my plans to work, I would not like to loose type hinting for it.
From my quick testing, I’ve found out it’s not possible to infer a class type onto a node that does not share the class. Both assigning the type for the variable and inferring it onto the node with the keyword as result in errors.