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.

class Entity

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>()boolTrue if T is attached.
RemoveComponent<T>()voidRemove T if present; no-op otherwise.
IsValid()boolTrue 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)voidSets 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()boolTrue if non-null and tied to a scene.
operator==(Entity)boolIdentity 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

Scene · ScriptableEntity · ECS Model

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.

class Entity

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>()boolTrue, jeśli T jest podpięty.
RemoveComponent<T>()voidUsuwa T, jeśli jest; w przeciwnym razie nic.
IsValid()boolTrue, 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)voidUstawia 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()boolTrue, jeśli niepusta i przypisana do sceny.
operator==(Entity)boolPoró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;

Zobacz też

Scene · ScriptableEntity · Model ECS