Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UInventoryComponent::UInventoryComponent(const FObjectInitializer& OI)
- {
- // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
- // off to improve performance if you don't need them.
- PrimaryComponentTick.bCanEverTick = false;
- SetIsReplicatedByDefault(true);
- Slots.Items.Empty();
- for (int i = 0; i < NumberOfSlots; i++)
- {
- CreateEmptySlot();
- }
- }
- // Called when the game starts
- void UInventoryComponent::BeginPlay()
- {
- Super::BeginPlay();
- }
- void UInventoryComponent::OnUnregister()
- {
- const AActor* lOwner = GetOwner();
- if (lOwner && lOwner->HasAuthority())
- {
- DestroyAllSlots();
- }
- Super::OnUnregister();
- }
- #if WITH_EDITOR
- void UInventoryComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
- {
- Super::PostEditChangeProperty(PropertyChangedEvent);
- FName PropertyName = (PropertyChangedEvent.Property != NULL) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
- //UE_LOG(LogTemp, Warning, TEXT("%s() - %s"), *FString(__FUNCTION__), *PropertyName.ToString());
- if (PropertyName == TEXT("Items"))
- {
- //Slots.MarkArrayDirty();
- //OnInventoryUpdated.Broadcast();
- }
- if (PropertyName == TEXT("NumberOfSlots"))
- {
- Slots.Items.Empty();
- for (int i = 0; i < NumberOfSlots; i++)
- {
- CreateEmptySlot();
- }
- }
- }
- #endif //WITH_EDITOR
- void UInventoryComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(UInventoryComponent, Slots);
- }
- bool UInventoryComponent::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
- {
- bool bWroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
- return bWroteSomething;
- }
- FInventoryArray UInventoryComponent::GetAllSlots()
- {
- return Slots;
- }
- void UInventoryComponent::CreateEmptySlot()
- {
- if (!GetOwner())
- {
- return;
- }
- if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
- {
- CreateEmptySlot_Server();
- return;
- }
- FInventoryItem CreatedSlot;
- const int32 EmptyIndex = Slots.Items.Add(CreatedSlot);
- Slots.MarkArrayDirty();
- OnInventoryUpdated.Broadcast();
- }
- void UInventoryComponent::CreateEmptySlot_Server_Implementation()
- {
- CreateEmptySlot();
- }
- bool UInventoryComponent::CreateEmptySlot_Server_Validate()
- {
- return true;
- }
- bool UInventoryComponent::DestroySlot(FInventoryItem ItemToDestroy)
- {
- if (!GetOwner())
- {
- return false;
- }
- if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
- {
- DestroySlot_Server(ItemToDestroy);
- return DestroySlot_Server_Validate(ItemToDestroy);
- }
- return true;
- }
- void UInventoryComponent::DestroySlot_Server_Implementation(FInventoryItem ItemToDestroy)
- {
- DestroySlot(ItemToDestroy);
- }
- bool UInventoryComponent::DestroySlot_Server_Validate(FInventoryItem ItemToDestroy)
- {
- return true;
- }
- void UInventoryComponent::DestroyAllSlots()
- {
- if (!GetOwner())
- {
- return;
- }
- if (GetOwner() && GetOwner()->GetLocalRole() == ROLE_AutonomousProxy)
- {
- DestroyAllSlots_Server();
- return;
- }
- Slots.Items.Empty();
- }
- void UInventoryComponent::DestroyAllSlots_Server_Implementation()
- {
- DestroyAllSlots();
- }
- bool UInventoryComponent::DestroyAllSlots_Server_Validate()
- {
- return true;
- }
Add Comment
Please, Sign In to add comment