Hello,
I’m trying to make a Flag (think as a CTF mode of sorts) that, when the player picks it up, it will attach to a socket in the player model. I thought this would be simple enough in theory, but I’m having a hard time to make it work.
Initially, I thought that simple setting it into the server would work. Sadly, it didn’t. Then I tried a naked NetMulticast function. Didn’t work. Then I tried a RPC to server. Didn’t work. Then I combined everything into a big RPC + NetMulticast. Still didn’t work.
It only happens on server side, as if the client never gets aware of the attachment.
I don’t know how to fix it. This is what I have now:
header
//...
UFUNCTION(NetMulticast, Reliable)
void BroadcastAttachFlagToCharacter(class AMyCharacter* Character);
virtual void BroadcastAttachFlagToCharacter_Implementation(class AMyCharacter* Character);
/** Attach the flag to the supplied character */
void AttachFlagToCharacter(AMyCharacter* Character);
/** This set of functions handles (AttachFlagToCharacter) with network support. */
UFUNCTION(Server, Reliable, WithValidation)
void ServerAttachFlagToCharacter(AMyCharacter* Character);
virtual void ServerAttachFlagToCharacter_Implementation(AMyCharacter* Character);
virtual bool ServerAttachFlagToCharacter_Validate(AMyCharacter* Character);
void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
//...
cpp:
//////////////////////////////////////////////////////////////////////////
// Zoc (2015-05-28): Networked implementation of AttachFlagToCharacter(AMyCharacter* Character)
//////////////////////////////////////////////////////////////////////////
void ACTFFlag::AttachFlagToCharacter(AMyCharacter* Character)
{
UE_LOG(MyCriticalErrors, Warning, TEXT("a message"), *(Character->GetName()));
if (Role < ROLE_Authority)
{
ServerAttachFlagToCharacter(Character);
return;
}
UE_LOG(MyCriticalErrors, Warning, TEXT("server message"), *(Character->GetName()));
BroadcastAttachFlagToCharacter(Character);
}
void ACTFFlag::ServerAttachFlagToCharacter_Implementation(AMyCharacter* Character)
{
AttachFlagToCharacter(Character);
}
bool ACTFFlag::ServerAttachFlagToCharacter_Validate(AMyCharacter* Character)
{
return true;
}
void ACTFFlag::BroadcastAttachFlagToCharacter_Implementation(AMyCharacter* Character)
{
if (Role < ROLE_Authority)
{
UE_LOG(MyCriticalErrors, Warning, TEXT("Messed up call"));
}
// DetachRootComponentFromParent();
USkeletalMeshComponent* PawnMesh = Character->GetMesh();
AttachRootComponentTo(PawnMesh, "FlagSocket", EAttachLocation::SnapToTarget);
}
void ACTFFlag::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Replicate to everyone
DOREPLIFETIME(ACTFFlag, FlagMesh);
}
There’s some leftovers there (ue_log) that I won’t bother editing right now (sorry, I’m kinda tired and it’s late!)
One thing that’s good to notice is that the “server message” (debug message in a ue_log) just happens once. so the server is aware of the changes, I can see the flag attached in the listen/play-in-editor window, but can’t see it in the other windows.
Any advice on what I’m doing wrong?
Thanks in advance!