UObject Replication Broken

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));
		}
	}
}

make sure you have SubObjectReplication enabled on the Actor and make sure the UObject is added to that list

Ok I have added this to Character constructor to enable replication of subobjects
bReplicateUsingRegisteredSubObjectList = true;

and override this function to add this to list (if I understand it correctly)

bool AThirdPerson5Character::ReplicateSubobjects(UActorChannel* channel, FOutBunch* bunch, FReplicationFlags* repFlags)
{
	bool WroteSomething = Super::ReplicateSubobjects(channel, bunch, repFlags);

	if (testObject)
	{
		WroteSomething |= channel->ReplicateSubobject(testObject, *bunch, *repFlags);
	}

	return WroteSomething;
}

but unfortunately nothing changes :frowning:

Ok, I have found the problem by my own, in case of using ReplicateSubobjects method, there is no need to set
bReplicateUsingRegisteredSubObjectList = true;

not it works, thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.