Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- // Spawning of the child UObjects
- //
- //
- // Spawning of AActor Legacy Manager, which spawns the problem UObject
- // Also spanws the UObject Player Manager, which replicates correctly
- void ACB_LegacyGameState::BeginPlay()
- {
- Super::BeginPlay();
- if (HasAuthority())
- {
- SpawnLegacyManager();
- SpawnPlayerManager();
- }
- }
- // This uboject is replicating as expected
- void ACB_LegacyGameState::SpawnPlayerManager()
- {
- PlayerManager = NewObject<UPlayerManager>(this, UPlayerManager::StaticClass());
- }
- // This is where I spawn the problem UObject that doesn't replicate its variables when they change at runtime (but they do replicate
- // their current values when a client joins)
- void ALegacyManager::Server_PostInitialize_Implementation()
- {
- if (HasAuthority())
- {
- PreseasonSimulator = NewObject<UPreseasonSimulator>(this, UPreseasonSimulator::StaticClass());
- PreseasonSimulator->Initialize(this);
- }
- }
- // Where the legacy manager is spawned by the game state
- void ACB_LegacyGameState::SpawnLegacyManager()
- {
- FActorSpawnParameters spawnParams;
- spawnParams.Owner = this;
- LegacyManager = GetWorld()->SpawnActor<ALegacyManager>(LegacyManagerClass, spawnParams);
- LegacyManager->SetOwner(this);
- LegacyManager->Server_Initialize();
- }
- //
- //
- // ReplicateSubobjects
- //
- //
- bool ACB_LegacyGameState::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
- {
- bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
- WroteSomething |= Channel->ReplicateSubobject(PlayerManager, *Bunch, *RepFlags);
- return WroteSomething;
- }
- bool ALegacyManager::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
- {
- bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
- WroteSomething |= Channel->ReplicateSubobject(PreseasonSimulator, *Bunch, *RepFlags);
- return WroteSomething;
- }
- //
- //
- // GetLifetimeReplicatedProps of Outer AActors
- //
- //
- void ACB_LegacyGameState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(ACB_LegacyGameState, PlayerManager);
- }
- void ALegacyManager::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(ALegacyManager, PreseasonSimulator);
- }
- //
- //
- // GetLifetimeReplicatedProps of UObjects
- //
- //
- void UPlayerManager::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(UPlayerManager, PlayerInfos);
- }
- void UPreseasonSimulator::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(UPreseasonSimulator, WeekNumber);
- }
- //
- //
- // .h file of each
- //
- //
- // .h file of ACB_LegacyGameState
- void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
- bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
- UPROPERTY(SaveGame, ReplicatedUsing="OnRep_PlayerManager")
- UPlayerManager* PlayerManager;
- UFUNCTION()
- virtual void OnRep_PlayerManager();
- // .h file of ALegacyManager
- bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
- void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
- UPROPERTY(BlueprintReadOnly, SaveGame, ReplicatedUsing="OnRep_PreseasonSimulator")
- UPreseasonSimulator* PreseasonSimulator;
- UFUNCTION()
- void OnRep_PreseasonSimulator();
- // .h file of UPlayerManager
- void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
- UPROPERTY(ReplicatedUsing="OnRep_PlayerInfos", SaveGame)
- TArray<FPlayerStateInfo> PlayerInfos; // Custom struct I have for player info
- UFUNCTION()
- void OnRep_PlayerInfos();
- // .h file of UPreseasonSimulator
- void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
- UPROPERTY(ReplicatedUsing="OnRep_WeekNumber",SaveGame)
- int32 WeekNumber = 0;
- UFUNCTION()
- void OnRep_WeekNumber();
- //
- //
- // UNetworkObject that all my UObjects I want replicated derive from
- //
- //
- void UNetworkObject::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- }
- bool UNetworkObject::IsSupportedForNetworking() const
- {
- return true;
- }
- bool UNetworkObject::CallRemoteFunction(UFunction* Function, void* Parameters, struct FOutParmRec* OutParams, FFrame* Stack)
- {
- if (AActor* OuterActor = Cast<AActor>(GetOuter()))
- {
- UNetDriver* NetDriver = OuterActor->GetNetDriver();
- if (NetDriver)
- {
- NetDriver->ProcessRemoteFunction(OuterActor, Function, Parameters, OutParams, Stack, this);
- return true;
- }
- }
- return false;
- }
- int32 UNetworkObject::GetFunctionCallspace(UFunction* Function, FFrame* Stack)
- {
- if (AActor* OuterActor = Cast<AActor>(GetOuter()))
- {
- return OuterActor ->GetFunctionCallspace(Function, Stack);
- }
- return FunctionCallspace::Local;
- }
- //
- //
- // Where I am changing the value of the property in each UObject
- //
- //
- void UPlayerManager::Server_SetPlayerAsReady_Implementation(const FString &inPlayerName)
- {
- LogNetMode(); // custom function that prints the netmode, this return ListenServer
- for (int32 i = 0; i < PlayerInfos.Num(); i++)
- {
- if (inPlayerName == PlayerInfos[i].PlayerName)
- {
- PlayerInfos[i].bPlayerIsReady = true;
- OnRep_PlayerInfos(); // called so server gets the notification about the change
- }
- }
- }
- void UPlayerManager::OnRep_PlayerInfos()
- {
- UE_LOG(LogTemp, Warning, TEXT("player infos replicated"));
- LogNetMode(); // custom function that prints the netmode, this return ListenServer for the server, and client for the client
- }
- void UPreseasonSimulator::Server_SimWeek_Implementation()
- {
- if (!LegacyManager->HasAuthority())
- {
- return;
- }
- //Advance the week
- LogNetMode(); // returns ListenServer
- WeekNumber += 1;
- OnRep_WeekNumber();// so the function is called on the ListenServer
- }
- void UPreseasonSimulator::OnRep_WeekNumber()
- {
- UE_LOG(LogTemp, Warning, TEXT("on rep week number"));
- LogNetMode(); // returns only Listen Server, never called on client
- }
Advertisement
Add Comment
Please, Sign In to add comment