Hello, i created a new project based on the third person template and i created a simple class named ATestActor that derives from AActor:
UCLASS()
class ATestActor : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(Replicated)
bool bIsTest;
};
----------------------
ATestActor::ATestActor(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
bReplicates = true;
}
void ATestActor::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ATestActor, bIsTest);
}
On the character class i added the following:
UPROPERTY(Transient, ReplicatedUsing=OnRep_Test)
class ATestActor * Test;
UFUNCTION()
void OnRep_Test();
----------------------------
void AMyCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyCharacter, Test);
}
void AMyCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
// The following code is just for testing purposes
if (Role == ROLE_Authority)
Test = GetWorld()->SpawnActor<class ATestActor>(ATestActor::StaticClass());
}
void AMyCharacter::OnRep_Test()
{
// It never reaches here
}
When running with 2 clients (1 listen server + 1 pure client) the Test variable is always null on the clients and instanced on the server character instances. Can you tell me what am i missing? Thank you
P.S: What does Transient do and where is it used? I read that it’s when you don’t want to save the variable to disk, but when does that happen? When saving a savegame to disk?
Update:
I tried making a client to server call using the following code:
void AMyCharacter::OnPressedRKey()
{
ATestActor * Test2 = GetWorld()->SpawnActor<class ATestActor>(ATestActor::StaticClass());
ServerSetTest(Test2);
}
bool AMyCharacter::ServerSetTest_Validate(class ATestActor * NewTest)
{
return true;
}
void AMyCharacter::ServerSetTest_Implementation(class ATestActor * NewTest)
{
Test = NewTest;
}
I put a breakpoint before calling the ServerSetTest and inside it. When i press the R key the breakpoint is activated and i see that Test2 is instanced and when i enter ServerSetTest_Implementation NewTest is NULL, so he’s just not being able to transmit this class.