Entity class in Thread
Lightweight handle to a registry-resident object. Equivalent to Unity's
GameObject. Entities don't store data themselves — they're just an
ID + scene pointer; data lives in components attached through
AddComponent.
Methods
| AddComponent<T>(args…) | T& | Attach a new component of type T and return it. |
| GetComponent<T>() | T& | Reference to the attached T. Asserts if missing. |
| AddOrReplaceComponent<T>(args…) | T& | Adds the component, or replaces it if already present. |
| HasComponent<T>() | bool | True if T is attached. |
| RemoveComponent<T>() | void | Remove T if present; no-op otherwise. |
| IsValid() | bool | True while the registry still has this entity. |
| GetScene() | Scene* | Owning scene pointer. |
| GetName() | const std::string& | The entity's name. Reads "" when it has no TagComponent and for an invalid entity. |
| SetName(name) | void | Sets the name, adding a TagComponent if there is none. No-op on an invalid entity. |
The name is a label, not an identity. GetName/SetName are
a thin wrapper over TagComponent::Tag — there is no second copy of the string. Names are
not unique and nothing enforces that they should be, and renaming an entity changes nothing
else about it.
Anything that has to keep pointing at the same entity uses the UUID instead: that is what Scene::FindByUUID, the serializer and a script's entity-reference fields all do. Reach for a name when a name really is what you have.
Note also that Scene::GetName is a different thing entirely — it is the name of the
scene, not of an entity.
Operators
| operator bool() | bool | True if non-null and tied to a scene. |
| operator==(Entity) | bool | Identity comparison. |
Example
Entity e = GetScene()->CreateEntity("Loot");
e.AddComponent<Thread::PrimitiveComponent>(Thread::PrimitiveType::Sphere);
auto& tc = e.GetComponent<Thread::TransformComponent>();
tc.Translation = { 0.0f, 1.0f, 0.0f };
if (e.HasComponent<Thread::RigidBodyComponent>())
e.GetComponent<Thread::RigidBodyComponent>().Mass = 2.0f;
See also
Entity klasa w Thread
Lekki uchwyt do obiektu w rejestrze. Odpowiednik GameObject z Unity. Encje same nie
przechowują danych — to tylko ID i wskaźnik sceny; dane żyją w komponentach podpiętych
przez AddComponent.
Metody
| AddComponent<T>(args…) | T& | Podpina nowy komponent typu T i go zwraca. |
| GetComponent<T>() | T& | Referencja do podpiętego T. Asercja, jeśli go brak. |
| AddOrReplaceComponent<T>(args…) | T& | Dodaje komponent albo zastępuje istniejący. |
| HasComponent<T>() | bool | True, jeśli T jest podpięty. |
| RemoveComponent<T>() | void | Usuwa T, jeśli jest; w przeciwnym razie nic. |
| IsValid() | bool | True, dopóki rejestr ma tę encję. |
| GetScene() | Scene* | Wskaźnik sceny-właściciela. |
| GetName() | const std::string& | Nazwa encji. "", gdy nie ma TagComponent, i dla nieważnej encji. |
| SetName(name) | void | Ustawia nazwę, dodając TagComponent, jeśli go nie ma. Nic nie robi na nieważnej encji. |
Nazwa to etykieta, nie tożsamość. GetName/SetName to cienka
nakładka na TagComponent::Tag — nie ma drugiej kopii napisu. Nazwy nie są
unikalne i nic tego nie wymusza, a zmiana nazwy niczego innego w encji nie zmienia.
Wszystko, co musi wskazywać ciągle tę samą encję, używa UUID: tak robią Scene::FindByUUID, serializer i pola skryptów wskazujące encje. Po nazwę sięgaj, gdy naprawdę masz tylko nazwę.
Uwaga: Scene::GetName to zupełnie co innego — to nazwa sceny, nie encji.
Operatory
| operator bool() | bool | True, jeśli niepusta i przypisana do sceny. |
| operator==(Entity) | bool | Porównanie tożsamości. |
Przykład
Entity e = GetScene()->CreateEntity("Loot");
e.AddComponent<Thread::PrimitiveComponent>(Thread::PrimitiveType::Sphere);
auto& tc = e.GetComponent<Thread::TransformComponent>();
tc.Translation = { 0.0f, 1.0f, 0.0f };
if (e.HasComponent<Thread::RigidBodyComponent>())
e.GetComponent<Thread::RigidBodyComponent>().Mass = 2.0f;