Combat and Inventory: Fight with Equipped Weapons
Equip a rifle from a backpack, fire it, then reload from an ammo stack. Inventory supplies the weapon actor in the player's hand and counts the reserve rounds. Combat uses that actor to fire and tracks the rounds loaded in its magazine. Start with a working equipment slot and a firearm with shooting and reload abilities.
Weapon Manager
Let equipment supply the active weapon
On the character, keep its
NinjaEquipmentManagerComponentand useNinjaCombatEquipmentAdapterComponentas its only Combat Weapon Manager. Return the adapter from Get Weapon Manager Component (GetWeaponManagerComponent) on the Combat System Interface. Remove the rifle from Combat's default weapon classes if it was previously spawned there.Give the rifle's Item Definition a project tag such as
Item.Weapon.Primary. In its Equipment Fragment, add theInventory.Equipment.State.Equippedstate. Choose Actor as the presence and assign a Blueprint based onNinjaCombatEquipmentWeaponActor. Put the rifle mesh and its Firearm Component on this actor.Let the hand slot accept the item's tag and set its equipped socket. In the combat ability's Weapon Query, require both the item tag and the equipped state. Move the rifle from the backpack into that slot. Combat searches the equipment's tags and returns the first matching weapon actor.
Weapon Actor
If your game already has a weapon actor, keep its Combat Weapon Interface and add a NinjaEquipmentActorComponent to access the equipment and its item. The supplied equipment weapon can use the item's level as its hit effect level. To drive damage from a rolled item stat, read the item's Dynamic Attributes fragment in the weapon's GetAdditionalCombatMagnitudes override and add the value under the damage tag your effect reads. In C++, extend GetAdditionalCombatMagnitudes_Implementation and preserve the parent's magnitudes. An Offhand Position fragment can also supply the support-hand position for a two-handed weapon.
Ammo Storage
Reload from an inventory stack
Add
NinjaCombatInventoryAmmoAdapterComponentto the character as its only ammo storage component. Create an ammunition Item Definition tagged, for example,Inventory.Item.Type.Ammo.Rifle. Add a Stack and Quantity fragment, set a stack limit of at least ten, and enable Can Be Observed.On the adapter, set All Ammo Types Query to include your ammo items. On the rifle's Firearm Definition, set Ammo Query to the rifle ammo tag. The first query tells the adapter which stack changes to watch. The rifle's query determines which rounds it can actually take.
Give the backpack ten reserve rounds. Use a full magazine of at least three, fire three shots, and trigger a magazine reload. At the reload notify the magazine should refill and seven rounds should remain in inventory.
For an ammo counter, read GetAvailableAmmo with the rifle's query when the widget opens. Bind BindToAvailableAmmoChangedDelegate on the ammo storage interface for later changes. Unbind with UnbindFromAvailableAmmoChangedDelegate when the widget closes. Combat calls TakeAmmo during reload, so the widget only displays the result.
Try It in Play
Replace the rifle in the hand slot with a second weapon bearing the same primary tag and a visibly different mesh. Fire again and check which actor supplies the shot. Move it back to the bag and verify the slot no longer supplies a weapon. Finally, try reloading the rifle with only an incompatible ammo stack. Its magazine and that stack should both keep their counts.