在12中,发现角色死亡后没起作用,原因是,
//是否死亡
// ReplicatedUsing 指定值变化时触发的回调函数
UPROPERTY(ReplicatedUsing = OnRep_IsDead)
bool IsDead = false;nst;
// Rep 回调函数声明(引擎固定格式 UFUNCTION())
UFUNCTION()
void OnRep_IsDead();
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Combat|Death")
void OnIsDeadChanged();
virtual void OnIsDeadChanged_Implementation() {}
在蓝图中已经兼容了单机,但是C++中没有,所以回调函数起不了作用,因此要在角色死亡时判断单机还是联机
void AMyPaperZDCharacter::SetIsDead(bool bNewState)
{
if (IsDead == bNewState)
return;
IsDead = bNewState;
// 1. 纯单机:手动触发回调
if (GetNetMode() == NM_Standalone)
{
OnRep_IsDead();
}
// 2. 联机服务端(房主/专属服):直接执行业务逻辑,不走OnRep
else if (HasAuthority())
{
OnIsDeadChanged();
}
// 3. 纯客户端:什么都不做,等待网络包触发OnRep
}
void AMyPaperZDCharacter::OnRep_IsDead()
{
// 客户端统一走这里
OnIsDeadChanged();
}