About a Scene Type and Extending a Class in Script

Godot Version

4.3

Question

I am still learning about Godot in general, and I came across this:

We define a node type in the scene, and then we could attach script to the node. Suppose I have a CharacterBody3D node, when I extend it, the script automatically has extends CharacterBody3D. Now, if the part of the this attached script somehow is changed to extends Node3D, then the functions would no longer have any function or attributes of CharacterBody3D. This makes sense.

But then the scene type on the left of Godot Window still shows CharacterBody3D red stickman icon. If my understanding is correct, a Node is an instance of a class, but if the attached script did not extend CharacterBody3D but it extends something else, what would be the meaning of this? As Godot Editor did not notify me any error or warning, is there something else I should be aware of?

Ye, there is only runtime check for node type and what script extends, and you need manually watch of this.
As Godot is OOP based, in theory, you can create script extending Node and attach it for any nodes in editor, but in script you obviosly have base access to Node properties but can determine real type(class) of node and change some behaviour.
It pseudocode for example, I not really checked this:

extends Node
func _ready():
  if self is Node3D:
    var _self: Node3D = self
    # now can access for Node3D properties
  elif self is Node2D:
    var _self: Node2D = self
    # now can access for Node2D properties

But IDK what can be really purpose of this.

Often mistake when you create new scene, like Node3D, call it Player, attach script, it generates extends Node3D and only now you realise you really need CharacterBody3D. You click RMB at Player and select handy menuitem Change Type..., select CharacterBody3D, back to script, forgot what it extends and boom, no needed properties in Code Assist, hints etc…

I think thats all what you need worry about.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.