Godot Version
4.6.2 stable
Question
I know this question has a simple answer, but I’m wondering if it has a more complex answer.
I have a simple Entity inheritance system.
I have a Entity class that extends Node3D and all my enemy scenes have their root node as a Node3D and RigidBody and CollisionShape nodes under it.
My question is how can I incorporate this setup with my player. Currently the root node of my play is a CharacterBody3D node - so it cant just extend Entity.
So the simple answer is make the root node of my player a Node3D and put the CharacterBody3D as a child node - yes, I’ll probably do this.
But I rather like having my players root node as a CharacterBody3D.
I know theres a dozen ways to do this, but what would be the easiest way to have my player scene inherent the Entity class and keep its root node as CharacterBody3D, should I just bite the bullet and make it a Node3D? Or is there a nicer way to have the best of both worlds?
Thanks for your help, cheers.
Is… there any reason you’re using Node3D as the base class for entities? CharacterBody3D nodes were meant to be moved by code, so if you ever set the position or velocity of the entity, you should probably use class_name Entity extends CharacterBody3D.
Oohh, I thought having multiple CharacterBody3D’s was not good (I think I heard that in a video somewhere), I’ve been using RigidBody3D’s for every other “enemy” or “npc” or “entity” in the game, I’ve been working under the assumption that you should only have 1 CharacterBody3D Node in a Scene.
So using many multiple CharacterBody3D’s in a Scene is okay? Or preferred even? Are there any draw backs? I do like the sound of turning all my RigidBodies into CharacterBodies!
You can have as many CharacterBodies as you please, there are no draw backs, they may perform better than RigidBodies.
Any physics body will have trouble if it is a child of another physics body, so while you can have plenty of sibling CharacterBodies and RigidBodies you will run into issues if you have RigidBodies as children of other RigidBodies, same to a lesser degree with CharacterBodies; maybe that’s the advice you heard elsewhere.
Aahh I see, thank you for the clear explanation.
Fantastic, so my “better” solution would be. Make my Entity class inherit CharacterBody3D instead of Node3D, remove all the RigidBodie nodes from my other Entites, and turn their root Node3D into a CharacterBody3D, and make all Entity Scenes inherit the new Entity class, so the player and all entities and now at their base, CharacterBody3Ds and can be treated the same?
Is that the gist?
Actually, when defining a class using class_name and extending anything derived from Node, you can add a node whose type is whatever you wrote after class_name. Other than that, you are absolutely correct.
Get rid of the Entity class altogether.