Guest User

ue4 replicated inventory ffastserializer

a guest
Sep 11th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. UInventoryComponent::UInventoryComponent(const FObjectInitializer& OI)
  2. {
  3. // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
  4. // off to improve performance if you don't need them.
  5. PrimaryComponentTick.bCanEverTick = false;
  6.  
  7. SetIsReplicatedByDefault(true);
  8.  
  9. Slots.Items.Empty();
  10.  
  11. for (int i = 0; i < NumberOfSlots; i++)
  12. {
  13. CreateEmptySlot();
  14. }
  15. }
  16.  
  17. // Called when the game starts
  18. void UInventoryComponent::BeginPlay()
  19. {
  20. Super::BeginPlay();
  21. }
  22.  
  23. void UInventoryComponent::OnUnregister()
  24. {
  25. const AActor* lOwner = GetOwner();
  26. if (lOwner && lOwner->HasAuthority())
  27. {
  28. DestroyAllSlots();
  29. }
  30.  
  31. Super::OnUnregister();
  32. }
  33.  
  34. #if WITH_EDITOR
  35. void UInventoryComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
  36. {
  37. Super::PostEditChangeProperty(PropertyChangedEvent);
  38.  
  39. FName PropertyName = (PropertyChangedEvent.Property != NULL) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
  40.  
  41. //UE_LOG(LogTemp, Warning, TEXT("%s() - %s"), *FString(__FUNCTION__), *PropertyName.ToString());
  42.  
  43. if (PropertyName == TEXT("Items"))
  44. {
  45. //Slots.MarkArrayDirty();
  46. //OnInventoryUpdated.Broadcast();
  47. }
  48.  
  49. if (PropertyName == TEXT("NumberOfSlots"))
  50. {
  51. Slots.Items.Empty();
  52.  
  53. for (int i = 0; i < NumberOfSlots; i++)
  54. {
  55. CreateEmptySlot();
  56. }
  57. }
  58. }
  59. #endif //WITH_EDITOR
  60.  
  61. void UInventoryComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  62. {
  63. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  64.  
  65. DOREPLIFETIME(UInventoryComponent, Slots);
  66. }
  67.  
  68. bool UInventoryComponent::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
  69. {
  70. bool bWroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
  71. return bWroteSomething;
  72. }
  73.  
  74.  
  75. FInventoryArray UInventoryComponent::GetAllSlots()
  76. {
  77. return Slots;
  78. }
  79.  
  80. void UInventoryComponent::CreateEmptySlot()
  81. {
  82. if (!GetOwner())
  83. {
  84. return;
  85. }
  86.  
  87. if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
  88. {
  89. CreateEmptySlot_Server();
  90. return;
  91. }
  92.  
  93. FInventoryItem CreatedSlot;
  94. const int32 EmptyIndex = Slots.Items.Add(CreatedSlot);
  95.  
  96. Slots.MarkArrayDirty();
  97.  
  98. OnInventoryUpdated.Broadcast();
  99. }
  100.  
  101. void UInventoryComponent::CreateEmptySlot_Server_Implementation()
  102. {
  103. CreateEmptySlot();
  104. }
  105.  
  106. bool UInventoryComponent::CreateEmptySlot_Server_Validate()
  107. {
  108. return true;
  109. }
  110.  
  111. bool UInventoryComponent::DestroySlot(FInventoryItem ItemToDestroy)
  112. {
  113. if (!GetOwner())
  114. {
  115. return false;
  116. }
  117.  
  118. if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
  119. {
  120. DestroySlot_Server(ItemToDestroy);
  121. return DestroySlot_Server_Validate(ItemToDestroy);
  122. }
  123.  
  124. return true;
  125. }
  126.  
  127. void UInventoryComponent::DestroySlot_Server_Implementation(FInventoryItem ItemToDestroy)
  128. {
  129. DestroySlot(ItemToDestroy);
  130. }
  131.  
  132. bool UInventoryComponent::DestroySlot_Server_Validate(FInventoryItem ItemToDestroy)
  133. {
  134. return true;
  135. }
  136.  
  137. void UInventoryComponent::DestroyAllSlots()
  138. {
  139. if (!GetOwner())
  140. {
  141. return;
  142. }
  143.  
  144. if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
  145. {
  146. DestroyAllSlots_Server();
  147. return;
  148. }
  149.  
  150. Slots.Items.Empty();
  151. }
  152.  
  153. void UInventoryComponent::DestroyAllSlots_Server_Implementation()
  154. {
  155. DestroyAllSlots();
  156. }
  157.  
  158. bool UInventoryComponent::DestroyAllSlots_Server_Validate()
  159. {
  160. return true;
  161. }
Tags: ue4
Add Comment
Please, Sign In to add comment