Hello, I am wonder what could be mising in a way to make an UObject replicated.
Could somebody help me with this problem?
Here what I have:
.H Ubjoct
class THIRDPERSON5_API UTestObject : public UObject
{
GENERATED_BODY()
public:
virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const override;
virtual bool IsSupportedForNetworking() const override { return true; }
UPROPERTY(Replicated)
int testVar = 1;
};
.CPP UObject
void UTestObject::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UTestObject, testVar);
}
.H Character
virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const override;
UPROPERTY(Replicated)
UTestObject* testObject;
.CPP Character
void AThirdPerson5Character::BeginPlay()
{
// Call the base class
Super::BeginPlay();
//Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
if (HasAuthority())
{
testObject = NewObject<UTestObject>();
testObject->testVar = 2;
}
else
{
if (testObject) //here the UObject is null
{
GLog->Log(testObject->GetName());
}
}
}
void AThirdPerson5Character::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AThirdPerson5Character, testObject);
}
As a result on server I have testObject but on client I have null, what did I missed?
I also checked nex ticks (maybe it requires more time to replicate) so I chacked that in tick
but still no success
void AThirdPerson5Character::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (!HasAuthority())
{
if (testObject) // always null
{
GLog->Log(FString::Printf(TEXT("testVar: %d"), testObject->testVar));
}
}
}