herculesx1000

UObject Property Replication

Aug 30th, 2022 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. //
  2. //
  3. // Spawning of the child UObjects
  4. //
  5. //
  6.  
  7. // Spawning of AActor Legacy Manager, which spawns the problem UObject
  8. // Also spanws the UObject Player Manager, which replicates correctly
  9. void ACB_LegacyGameState::BeginPlay()
  10. {
  11. Super::BeginPlay();
  12.  
  13. if (HasAuthority())
  14. {
  15. SpawnLegacyManager();
  16. SpawnPlayerManager();
  17. }
  18. }
  19.  
  20. // This uboject is replicating as expected
  21. void ACB_LegacyGameState::SpawnPlayerManager()
  22. {
  23. PlayerManager = NewObject<UPlayerManager>(this, UPlayerManager::StaticClass());
  24. }
  25.  
  26. // This is where I spawn the problem UObject that doesn't replicate its variables when they change at runtime (but they do replicate
  27. // their current values when a client joins)
  28. void ALegacyManager::Server_PostInitialize_Implementation()
  29. {
  30. if (HasAuthority())
  31. {
  32. PreseasonSimulator = NewObject<UPreseasonSimulator>(this, UPreseasonSimulator::StaticClass());
  33. PreseasonSimulator->Initialize(this);
  34. }
  35. }
  36.  
  37.  
  38. // Where the legacy manager is spawned by the game state
  39. void ACB_LegacyGameState::SpawnLegacyManager()
  40. {
  41. FActorSpawnParameters spawnParams;
  42. spawnParams.Owner = this;
  43. LegacyManager = GetWorld()->SpawnActor<ALegacyManager>(LegacyManagerClass, spawnParams);
  44. LegacyManager->SetOwner(this);
  45. LegacyManager->Server_Initialize();
  46. }
  47.  
  48. //
  49. //
  50. // ReplicateSubobjects
  51. //
  52. //
  53.  
  54. bool ACB_LegacyGameState::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
  55. {
  56. bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
  57.  
  58. WroteSomething |= Channel->ReplicateSubobject(PlayerManager, *Bunch, *RepFlags);
  59.  
  60. return WroteSomething;
  61. }
  62.  
  63. bool ALegacyManager::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
  64. {
  65. bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
  66.  
  67. WroteSomething |= Channel->ReplicateSubobject(PreseasonSimulator, *Bunch, *RepFlags);
  68.  
  69. return WroteSomething;
  70. }
  71.  
  72. //
  73. //
  74. // GetLifetimeReplicatedProps of Outer AActors
  75. //
  76. //
  77.  
  78. void ACB_LegacyGameState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  79. {
  80. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  81.  
  82. DOREPLIFETIME(ACB_LegacyGameState, PlayerManager);
  83. }
  84.  
  85. void ALegacyManager::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  86. {
  87. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  88.  
  89. DOREPLIFETIME(ALegacyManager, PreseasonSimulator);
  90. }
  91.  
  92. //
  93. //
  94. // GetLifetimeReplicatedProps of UObjects
  95. //
  96. //
  97.  
  98. void UPlayerManager::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  99. {
  100. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  101.  
  102. DOREPLIFETIME(UPlayerManager, PlayerInfos);
  103. }
  104.  
  105. void UPreseasonSimulator::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  106. {
  107. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  108.  
  109. DOREPLIFETIME(UPreseasonSimulator, WeekNumber);
  110. }
  111.  
  112. //
  113. //
  114. // .h file of each
  115. //
  116. //
  117.  
  118. // .h file of ACB_LegacyGameState
  119.  
  120. void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
  121. bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
  122.  
  123. UPROPERTY(SaveGame, ReplicatedUsing="OnRep_PlayerManager")
  124. UPlayerManager* PlayerManager;
  125. UFUNCTION()
  126. virtual void OnRep_PlayerManager();
  127.  
  128. // .h file of ALegacyManager
  129.  
  130. bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
  131. void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
  132.  
  133. UPROPERTY(BlueprintReadOnly, SaveGame, ReplicatedUsing="OnRep_PreseasonSimulator")
  134. UPreseasonSimulator* PreseasonSimulator;
  135. UFUNCTION()
  136. void OnRep_PreseasonSimulator();
  137.  
  138. // .h file of UPlayerManager
  139.  
  140. void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
  141.  
  142. UPROPERTY(ReplicatedUsing="OnRep_PlayerInfos", SaveGame)
  143. TArray<FPlayerStateInfo> PlayerInfos; // Custom struct I have for player info
  144.  
  145. UFUNCTION()
  146. void OnRep_PlayerInfos();
  147.  
  148. // .h file of UPreseasonSimulator
  149.  
  150. void GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const override;
  151.  
  152. UPROPERTY(ReplicatedUsing="OnRep_WeekNumber",SaveGame)
  153. int32 WeekNumber = 0;
  154.  
  155. UFUNCTION()
  156. void OnRep_WeekNumber();
  157.  
  158. //
  159. //
  160. // UNetworkObject that all my UObjects I want replicated derive from
  161. //
  162. //
  163.  
  164. void UNetworkObject::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  165. {
  166. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  167. }
  168.  
  169. bool UNetworkObject::IsSupportedForNetworking() const
  170. {
  171. return true;
  172. }
  173.  
  174. bool UNetworkObject::CallRemoteFunction(UFunction* Function, void* Parameters, struct FOutParmRec* OutParams, FFrame* Stack)
  175. {
  176. if (AActor* OuterActor = Cast<AActor>(GetOuter()))
  177. {
  178. UNetDriver* NetDriver = OuterActor->GetNetDriver();
  179. if (NetDriver)
  180. {
  181. NetDriver->ProcessRemoteFunction(OuterActor, Function, Parameters, OutParams, Stack, this);
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187.  
  188. int32 UNetworkObject::GetFunctionCallspace(UFunction* Function, FFrame* Stack)
  189. {
  190. if (AActor* OuterActor = Cast<AActor>(GetOuter()))
  191. {
  192. return OuterActor ->GetFunctionCallspace(Function, Stack);
  193. }
  194.  
  195. return FunctionCallspace::Local;
  196. }
  197.  
  198. //
  199. //
  200. // Where I am changing the value of the property in each UObject
  201. //
  202. //
  203.  
  204. void UPlayerManager::Server_SetPlayerAsReady_Implementation(const FString &inPlayerName)
  205. {
  206. LogNetMode(); // custom function that prints the netmode, this return ListenServer
  207. for (int32 i = 0; i < PlayerInfos.Num(); i++)
  208. {
  209. if (inPlayerName == PlayerInfos[i].PlayerName)
  210. {
  211. PlayerInfos[i].bPlayerIsReady = true;
  212. OnRep_PlayerInfos(); // called so server gets the notification about the change
  213. }
  214. }
  215. }
  216.  
  217. void UPlayerManager::OnRep_PlayerInfos()
  218. {
  219. UE_LOG(LogTemp, Warning, TEXT("player infos replicated"));
  220. LogNetMode(); // custom function that prints the netmode, this return ListenServer for the server, and client for the client
  221. }
  222.  
  223. void UPreseasonSimulator::Server_SimWeek_Implementation()
  224. {
  225. if (!LegacyManager->HasAuthority())
  226. {
  227. return;
  228. }
  229.  
  230. //Advance the week
  231. LogNetMode(); // returns ListenServer
  232.  
  233. WeekNumber += 1;
  234. OnRep_WeekNumber();// so the function is called on the ListenServer
  235. }
  236.  
  237. void UPreseasonSimulator::OnRep_WeekNumber()
  238. {
  239. UE_LOG(LogTemp, Warning, TEXT("on rep week number"));
  240. LogNetMode(); // returns only Listen Server, never called on client
  241. }
Advertisement
Add Comment
Please, Sign In to add comment