How might I represent polymorphism/inheritance when it comes to Godot objects?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Carcanken

Right now I’m working on a small RPG game to learn more about Godot and have come to the point where I would like to create my own objects to make creating things like enemies easier.

I have a class diagram much along the lines of an Entity class and a Player and Enemy one that inherits Entity. Here it is attached:

Entity has a lot of components such as health, an attack function, etc and these should extend to Player and Enemy. Player has keyboard implemented movement and enemy will have AI in the future.

Right now I have them separated (no inheritance involved) like this:

My question comes as to how to represent these ideas properly with inheritance when considering Godot objects such as nodes, etc. I’m also just learning about instances and I think they could be useful but I’m not sure how to apply it.

One solution I considered was to create a node (and maybe instance of this scene)
called Entity (with an attached script with the functions as stated above) with the child/inheritor being either the player/enemy sprite (with their appropriate functions) and making an instance for each individual enemy type and the player but is this in the spirit of scene instancing?

:bust_in_silhouette: Reply From: Ertain

When a script is attached to a node, usually it extends the properties of that node’s class. As such, the script can use any of the functions and properties that node had implemented and inherited. You can build upon these with your own code and implementations. The script can use objects of other classes and nodes to implement what you want. See the documentation on how to use inheritance in GDscript for a few pointers.

I would put in more examples. But I’m currently on mobile.

Yeah, I was aware that the script normally contains the properties of whatever it is attached to, but I was mostly looking into the most practical or common way of doing what I described.

Thank you for your response!

Carcanken | 2021-03-14 01:53

:bust_in_silhouette: Reply From: AaronWizard

In addition to what Ertain said, you can create a scene that inherits another scene using the menu option “Scene > New Inherited Scene”. This will let you create a new scene with the same script and node structure as a base scene, where you may set properties and add additional nodes.

enter image description here

For example, you would make an Entity scene with all nodes that would be common to players and enemies such as sprites, physics and collision nodes, audio stream players, etc. Then you would create Player and Enemy scenes using the “New Inherited Scene” menu option where those two new scenes would inherit from Entity. Any necessary additions and modifications can now be made to the Player and Enemy scenes.

Note that if your base scene has a script (e.g. “”) then the inherited scenes will also use this script. If you want the inherited scene to have its own script, the script needs to inherit from the script of the base scene as described in the page Ertain linked to. With your inherited scene in the scene editor, select the root node then in the Inspector select the drop-down for the script property and select “Extend Script”.

enter image description here

This will let you create a script that properly extends the original scene script.