The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Why can I extend a node with a class that does not extend that node type?

RattyRatty Posts: 7Member

Hi, I'll ask the basic question first then try to explain why I am asking it.

Using GDScript - Say I have a KinematicBody2D node, call it "theplayerbody" and a class that extends Node2D, call it "playersclass".
I can attach a script to "theplayerbody" and have it extend "playersclass" even though "playersclass" does not extend KinematicBody2D.
I'd have thought this would throw an error. Is it allowed because they both extend Node2D at some level?

It is rather useful for me as it happens as I wanted to be able to have a base "Entity" class that extends Node2D for pretty much anything in the game that is not background or GUI. The Entity class would have sub classes/nodes that extend it, some of which might move about, like players and MOBs, others might be fixed, like chests or furniture. So the Entity class would have to be able to be extended by different classes of nodes, kinematic, basic node2d etc.

Is this a bad approach or is this entirely as expected?

Any thoughts much appreciated.

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 3,141Admin

    @Ratty said:
    I'd have thought this would throw an error. Is it allowed because they both extend Node2D at some level?

    Yup, that's how it works! Because KinematicBody2D extends Node2D internally, you can use a script that extends Node2D on a KinematicBody2D. This will limit you to using any functions that are in Node2D, so KinematicBody2D-specific functions will not be accessible unless the script you are using extends KinematicBody2D (in general, you might be able to work around this through call functions).

    The inverse, however, does not work. You cannot have a script that extends KinematicBody2D and use it on a Node2D node, even if the script doesn't use anything related to KinematicBody2D. This is because by saying extends KinematicBody2D, you are telling GDScript that you want to access KinematicBody2D code, making it where only KinematicBody2D nodes and those that extend it function. If you extend Node2D though, you are telling GDScript that you want to access Node2D code, and because KinematicBody2D extends Node2D and has access to all the same code, it will work without any issues.

    Also, as expected, you cannot use a script that extends KinematicBody2D on a node like RigidBody2D, for the same reason you cannot use a script that extends KinematicBody2D on a Node2D node.

    It is rather useful for me as it happens as I wanted to be able to have a base "Entity" class that extends Node2D for pretty much anything in the game that is not background or GUI. The Entity class would have sub classes/nodes that extend it, some of which might move about, like players and MOBs, others might be fixed, like chests or furniture. So the Entity class would have to be able to be extended by different classes of nodes, kinematic, basic node2d etc.

    Is this a bad approach or is this entirely as expected?

    This would work, and I've done something similar before in Unity. The issue you may run into is not being able to easily access functionality from nodes like KinematicBody2D without needing a "glue" node, a node that has a script that extends KinematicBody2D and then accessing that script. Thinking about it, you may also be able to use casting, like var kinematic_self = self as KinematicBody2D, but I have not tested it myself so I cannot say whether it would work or not.

    Regardless, it is should be doable, you may just have to work around quirks, but that's the case no matter how you approach game development really. :smile:

  • RattyRatty Posts: 7Member

    Excellent answer, thanks.
    I was specifically trying to avoid adding anything that might break later, so I'm allot more sure in what I am doing now.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file