herculesx1000

UObject property replication problems

Aug 27th, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. Base uobject class that PreseasonSimulator derives from
  2.  
  3. void UNetworkObject::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  4. {
  5. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  6. }
  7.  
  8. bool UNetworkObject::IsSupportedForNetworking() const
  9. {
  10. return true;
  11. }
  12.  
  13. bool UNetworkObject::CallRemoteFunction(UFunction* Function, void* Parameters, struct FOutParmRec* OutParams, FFrame* Stack)
  14. {
  15. check(!HasAnyFlags(RF_ClassDefaultObject));
  16. AActor* Owner = GetOwningActor();
  17. UNetDriver* NetDriver = Owner->GetNetDriver();
  18. if (NetDriver)
  19. {
  20. NetDriver->ProcessRemoteFunction(Owner, Function, Parameters, OutParams, Stack, this);
  21. return true;
  22. }
  23. return false;
  24. }
  25.  
  26. int32 UNetworkObject::GetFunctionCallspace(UFunction* Function, FFrame* Stack)
  27. {
  28. check(GetOuter() != nullptr);
  29. return GetOuter()->GetFunctionCallspace(Function, Stack);
  30. }
  31.  
  32.  
  33. // Derives from UNetworkObject
  34.  
  35. void UPreseasonSimulator::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  36. {
  37. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  38.  
  39. DOREPLIFETIME(UPreseasonSimulator, WeekNumber);
  40. }
  41.  
  42. void UPreseasonSimulator::Server_SimWeek_Implementation()
  43. {
  44. if (!LegacyManager->HasAuthority())
  45. {
  46. return;
  47. }
  48.  
  49. LogNetMode(); // text replacement macro that prints the net mode (prints Listen Server here)
  50.  
  51. WeekNumber += 1; // default is 0
  52. OnRep_WeekNumber(); // Called so it runs on listen server, but I can't get the OnRep to trigger on the client
  53.  
  54. NetMulticast_AdvanceWeek(); // currently implemented to just test printing WeekNumber on server and client
  55. }
  56.  
  57. void UPreseasonSimulator::OnRep_WeekNumber()
  58. {
  59. LogNetMode(); // text replacement macro that prints the net mode
  60.  
  61. UE_LOG(LogTemp, Warning, TEXT("on rep week number"));
  62. }
  63.  
  64. void UPreseasonSimulator::Client_AdvanceWeek_Implementation()
  65. {
  66. LogNetMode(); // text replacement macro that prints the net mode
  67.  
  68. Prints 1 on server, 0 on client
  69. UE_LOG(LogTemp, Warning, TEXT("client advance week. Weeek number: %i"), WeekNumber);
  70. }
  71.  
  72. in PreseasonSimulator .h file
  73. UPROPERTY(ReplicatedUsing="OnRep_WeekNumber",SaveGame)
  74. int32 WeekNumber = 0;
  75.  
  76. UFUNCTION()
  77. void OnRep_WeekNumber();
  78.  
  79. // AActor class that spawns the PreseasonSimulator
  80. ALegacyManager::ALegacyManager()
  81. {
  82. bReplicates = true;
  83. bAlwaysRelevant = true;
  84. }
  85.  
  86. bool ALegacyManager::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
  87. {
  88. bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
  89.  
  90. WroteSomething |= Channel->ReplicateSubobject(PreseasonSimulator, *Bunch, *RepFlags);
  91.  
  92. return WroteSomething;
  93. }
  94.  
  95. void ALegacyManager::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  96. {
  97. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  98.  
  99. DOREPLIFETIME(ALegacyManager, PreseasonSimulator);
  100. }
  101.  
  102. // Where preseason Simulator is spawned
  103. void ALegacyManager::Server_PostInitialize_Implementation()
  104. {
  105. if (!HasAuthority())
  106. {
  107. return;
  108. }
  109.  
  110. PreseasonSimulator = NewObject<UPreseasonSimulator>(this, UPreseasonSimulator::StaticClass());
  111. PreseasonSimulator->Initialize(this);
  112.  
  113. }
  114.  
  115. void ALegacyManager::OnRep_PreseasonSimulator()
  116. {
  117. UE_LOG(LogTemp, Warning,TEXT("Preseason Simulator on rep"));
  118. }
  119.  
  120. .h file of ALegacyManager
  121. UPROPERTY(BlueprintReadOnly, SaveGame, ReplicatedUsing="OnRep_PreseasonSimulator")
  122. UPreseasonSimulator* PreseasonSimulator;
  123.  
  124. UFUNCTION()
  125. void OnRep_PreseasonSimulator();
  126.  
  127.  
Advertisement
Add Comment
Please, Sign In to add comment