- 멀티 플레이어 게임에서 클래스의 속성을 replicate 하려면
Replicated 또는 ReplicatedUsing 의 metadata specifiers 를 사용해야함.
Replicated 나 ReplicatedUsing 으로 지정된 property가 멀티플레이 중 값이 변경되면 서버는 연결된 각 클라이언트에 업데이트를 전송함.
- 각 클라이언트는 업데이트 된 값을 자신의 로컬 Actor에 적용한다.
Replicate Actor Properties
- Replicated
- 단순 property replicate에 사용
- ReplicatedUsing
- replicate를 할 때 RepNotify 함수를 제공해야 한다.
- 속성이 replicate 될 때마다 RepNotify가 호출된다.
- replicate시 추가 작업을 수행할 때 사용
- NotReplicated
- replicate가 되지 않도록 지정하는 metadata
- Replicated Struct 내 특정 속성을 replicated 되지 않도록 할 때 유용함.
Replicate 과정
// Replicated 속성 추가
UPROPERTY(Replicated)
uint32 Health;
//생성자에서 Actor 가 replicated 가능한 상태임을 명시
생성자
{
bReplicates = true;
}
// GetLifetimeReplicatedProps 함수에서 replicated 할 멤버 정의
void ADerivedActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ADerivedActor, Health);
}
Replicated Using
// ReplicatedUsing 메타데이터 추가
UPROPERTY(ReplicatedUsing=OnRep_HealthUpdate)
uint32 Health;
// RepNotify 함수 추가
UFUNCTION()
void OnRep_HealthUpdate();
// replicate 와 마찬가지로 생성자, GetLifetTimeReplicatedProps 에서 정의
...
void ADerivedActor::OnRep_HealthUpdate()
{
UE_LOG(LogTemp, Log, TEXT("OnRep_HealthUpdate"))
// Add custom OnRep logic
}