Hello everybody, I need to replicate my UObject from server to client, what I do wrong? On the server side data is UObject is valid on the client side - not.
Below my replication code: (UObject and AActor)
ItemObject.h
#pragma once
#include "CoreMinimal.h"
#include "Core.h"
#include "Object.h"
#include "ItemObject.generated.h"
UCLASS(Blueprintable)
class MYGAME_API UItemObject : public UObject
{
GENERATED_BODY()
UItemObject(const class FObjectInitializer& ObjectInitializer);
public:
UPROPERTY(Replicated)
uint32 bReplicatedFlag : 1;
virtual bool IsSupportedForNetworking() const override
{
return true;
}
};
ItemObject.cpp
#include "ItemObject.h"
#include "UnrealNetwork.h"
#include "Engine.h"
UItemObject::UItemObject(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}
void UItemObject::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UItemObject, bReplicatedFlag);
}
MyPlayerController.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Core.h"
#include "ItemObject.h"
#include "MyPlayerController.generated.h"
/**
*
*/
UCLASS()
class MYGAME_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
AMyPlayerController(const class FObjectInitializer& ObjectInitializer);
public:
virtual void PostInitializeComponents() override;
virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override;
/** A Replicated Subobject */
UPROPERTY(Replicated)
UItemObject* Subobject;
private:
};
MyPlayerController.cpp
#include "MyPlayerController.h"
#include "UnrealNetwork.h"
#include "Engine/ActorChannel.h"
#include "ItemObject.h"
AMyPlayerController::AMyPlayerController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
bReplicates = true;
}
void AMyPlayerController::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (HasAuthority())
{
Subobject = NewObject<UItemObject>(this);
}
}
bool AMyPlayerController::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags)
{
bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
if (Subobject != nullptr)
{
WroteSomething |= Channel->ReplicateSubobject(Subobject, *Bunch, *RepFlags);
}
return WroteSomething;
}
void AMyPlayerController::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyPlayerController, Subobject);
}