void ASCharacter::SetCurrentWeapon(class ASWeapon* NewWeapon, class ASWeapon* LastWeapon) { /* Maintain a reference for visual weapon swapping */ PreviousWeapon = LastWeapon; ASWeapon* LocalLastWeapon = nullptr; if (LastWeapon) { LocalLastWeapon = LastWeapon; } else if (NewWeapon != CurrentWeapon) { LocalLastWeapon = CurrentWeapon; } // UnEquip the current bool bHasPreviousWeapon = false; if (LocalLastWeapon) { LocalLastWeapon->OnUnEquip(); bHasPreviousWeapon = true; } CurrentWeapon = NewWeapon; if (NewWeapon) { NewWeapon->SetOwningPawn(this); /* Only play equip animation when we already hold an item in hands */ NewWeapon->OnEquip(bHasPreviousWeapon); } /* NOTE: If you don't have an equip animation w/ animnotify to swap the meshes halfway through, then uncomment this to immediately swap instead */ //SwapToNewWeaponMesh(); }
void ASCharacter::SetCurrentWeapon(class ASWeapon* NewWeapon, class ASWeapon* LastWeapon) { ASWeapon* LocalLastWeapon = nullptr; if (LastWeapon) { LocalLastWeapon = LastWeapon; } else if (NewWeapon != CurrentWeapon) { LocalLastWeapon = CurrentWeapon; } // UnEquip the current if (LocalLastWeapon) { LocalLastWeapon->OnUnEquip(); } CurrentWeapon = NewWeapon; if (NewWeapon) { NewWeapon->SetOwningPawn(this); NewWeapon->OnEquip(); } }
void ASCharacter::OnEquipSecondaryWeapon() { if (Inventory.Num() >= 2) { /* Find first weapon that uses secondary slot. */ for (int32 i = 0; i < Inventory.Num(); i++) { ASWeapon* Weapon = Inventory[i]; if (Weapon->GetStorageSlot() == EInventorySlot::Secondary) { EquipWeapon(Weapon); } } } }
void ASCharacter::DestroyInventory() { if (Role < ROLE_Authority) { return; } for (int32 i = Inventory.Num() - 1; i >= 0; i--) { ASWeapon* Weapon = Inventory[i]; if (Weapon) { RemoveWeapon(Weapon); Weapon->Destroy(); } } }
bool ASCharacter::WeaponSlotAvailable(EInventorySlot CheckSlot) { /* Iterate all weapons to see if requested slot is occupied */ for (int32 i = 0; i < Inventory.Num(); i++) { ASWeapon* Weapon = Inventory[i]; if (Weapon) { if (Weapon->GetStorageSlot() == CheckSlot) return false; } } return true; /* Special find function as alternative to looping the array and performing if statements the [=] prefix means "capture by value", other options include [] "capture nothing" and [&] "capture by reference" */ //return nullptr == Inventory.FindByPredicate([=](ASWeapon* W){ return W->GetStorageSlot() == CheckSlot; }); }
void ASCharacter::OnEquipSecondaryWeapon() { if (CarriedObjectComp->GetIsCarryingActor()) { CarriedObjectComp->Rotate(-1.0f, 0.0f); return; } if (Inventory.Num() >= 2) { /* Find first weapon that uses secondary slot. */ for (int32 i = 0; i < Inventory.Num(); i++) { ASWeapon* Weapon = Inventory[i]; if (Weapon->GetStorageSlot() == EInventorySlot::Secondary) { EquipWeapon(Weapon); } } } }