소유권
FPS 템플릿의 핵심 기능 중 하나는 소유권 관리입니다. 모든 Quantum 엔티티에는 Owner
컴포넌트가 있으며, 이 컴포넌트는 다른 엔티티(즉, EntityRef
)에 대한 참조를 포함합니다. 런타임에 생성된 엔티티는 Frame.CreateEntity()
메소드의 매개 변수로 소유자 EntityRef
를 갖고 있습니다. 그러나 씬 엔티티는 유니티 에디터의 EntityComponentOwner
에 명시적으로 정의되지 않는 한 EntityRef.None
으로 생성됩니다.
또한 Frame
에는 소유자 엔티티에서 작동하는 몇 가지 메소드와 해당 부분이 포함되어 있습니다:
GetOwnerEntity()
: 소유자의EntityRef
또는EntityRef.None
을 리턴합니다.TryGetComponentInOwner<T>()
: 포인터가 있는 경우 소유자의 모든 타입T
의 Quantum 컴포넌트를 리턴하고, 그렇지 않은 경우null
을 반환합니다. 이 메소드 동작은 유니티GetComponentInParent<T>()
와 유사하며 지정된 엔티티에서 시작하여 소유자 체인에 재귀적으로 반복됩니다.TryGetControllerInOwner<T>()
: 포인터가 있는 경우 소유자의 모든 타입T
의EntityController
를 리턴하고, 그렇지 않은 경우null
을 반환합니다. 이 메소드 동작은 유니티GetComponentInParent<T>()
와 유사하며 지정된 엔티티에서 시작하여 소유자 체인에 재귀적으로 반복됩니다. 인터페이스를 지원합니다.
예제: 맞은 후 플레이어 액터 엔티티 얻기.
C#
private EntityRef ResolvePlayerActorEntity(Frame frame, HitData hitData)
{
// We don't know what caused the hit and cannot assume it was an ability / effect / projectile, ...
// We also don't know exact ownership chain depth
// 1. This solution is fine if the player controls an entity with Agent component, all the time
return frame.TryGetComponentInOwner<Agent>(hitData.Source, true, out EntityRef agentEntity) != null ? agentEntity : EntityRef.None;
// 2. This solution is fine if the player controls entities with different component set and we can assume each EntityController implements IInputController (player controls the entity with input).
// This solution will fail if there is an entity with IInputController in the middle of ownership chain (for example a weapon with controller which can reacts to player input)
return frame.TryGetControllerInOwner<IInputController>(hitData.Source, true, out EntityRef inputControllerEntity) != null ? inputControllerEntity : EntityRef.None;
// 3. This solution is the safest one, but least generic and adds dependency on Player internals
Player* player = frame.TryGetComponentInOwner<Player>(hitData.Source, false, out EntityRef playerEntity);
return player != null ? player->ActorEntity : EntityRef.None;
}
유니티 편집기에서 Entity
컴포넌트의 Require Owner
옵션을 토글 할 수 있습니다.
이 옵션을 선택하면 엔티티가 이 엔티티의 소유자보다 만들어진 후 그리고 파괴되기 전이라는 것을 보장해 줍니다. 예를 들어, 에이전트가 없는 경우 무기 엔티티를 만들 수 없으며 소유자 에이전트가 파괴되기 전에 파괴되어야 합니다.
Back to top