+1 vote

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?

in Engine by (30 points)
edited by

2 Answers

+2 votes
Best answer

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. "Entity.gd") 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.

by (116 points)
edited by
+1 vote

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.

by (1,988 points)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.