unitysamples / LyraStarterGame /Source /LyraGame /AbilitySystem /Abilities /LyraAbilityCost_InventoryItem.cpp
| // Copyright Epic Games, Inc. All Rights Reserved. | |
| ULyraAbilityCost_InventoryItem::ULyraAbilityCost_InventoryItem() | |
| { | |
| Quantity.SetValue(1.0f); | |
| } | |
| bool ULyraAbilityCost_InventoryItem::CheckCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, FGameplayTagContainer* OptionalRelevantTags) const | |
| { | |
| if (AController* PC = Ability->GetControllerFromActorInfo()) | |
| { | |
| if (ULyraInventoryManagerComponent* InventoryComponent = PC->GetComponentByClass<ULyraInventoryManagerComponent>()) | |
| { | |
| const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); | |
| const float NumItemsToConsumeReal = Quantity.GetValueAtLevel(AbilityLevel); | |
| const int32 NumItemsToConsume = FMath::TruncToInt(NumItemsToConsumeReal); | |
| return InventoryComponent->GetTotalItemCountByDefinition(ItemDefinition) >= NumItemsToConsume; | |
| } | |
| } | |
| return false; | |
| } | |
| void ULyraAbilityCost_InventoryItem::ApplyCost(const ULyraGameplayAbility* Ability, const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo) | |
| { | |
| if (ActorInfo->IsNetAuthority()) | |
| { | |
| if (AController* PC = Ability->GetControllerFromActorInfo()) | |
| { | |
| if (ULyraInventoryManagerComponent* InventoryComponent = PC->GetComponentByClass<ULyraInventoryManagerComponent>()) | |
| { | |
| const int32 AbilityLevel = Ability->GetAbilityLevel(Handle, ActorInfo); | |
| const float NumItemsToConsumeReal = Quantity.GetValueAtLevel(AbilityLevel); | |
| const int32 NumItemsToConsume = FMath::TruncToInt(NumItemsToConsumeReal); | |
| InventoryComponent->ConsumeItemsByDefinition(ItemDefinition, NumItemsToConsume); | |
| } | |
| } | |
| } | |
| } | |