Integration With Weapon Systems
Weapon Base Component
To simplify the integration of the inventory with the weapon systems, a special component called "WeaponBase_ISX" was created. It contains only one delegate, which triggers upon weapon equip or unequip, along with useful functions.
There are two ways to use it:
- Inherit from it.
- Add it to the character.
Inheriting from it provides more flexibility, for example, you can override functions like Can Fire and Can Reload if you are not satisfied with their default implementations.
Delegate
When selecting a weapon from a shortcut or equipping it through the context menu in the inventory, the On Equip
delegate
is triggered. It also triggers when you discard the equipped weapon.
Functions
Reload
It deducts the necessary amount of ammunition from the inventory if available and adds it to the weapon's clip.
Remove Ammo
It deducts ammunition from the weapon's magazine. You can use it when you fire.
Hide / Show Weapon
This function either removes the equipped weapon or adds it back. Additionally, when used, the OnEquip delegate is triggered.
Get Equipped Item
It returns the weapon item if it is equipped.
Get Inventory Component
It returns the inventory component. It can be overridden.
Can Reload
It returns true if the clip is not full and there is ammunition in the inventory available for reloading. It can be overridden.
Can Fire
It returns true if there is a sufficient number of ammo in the clip. It can be overridden.
Here's an example of how you can override this function:
Tips
How to get the amount of ammunition for UI
Ammo in a weapon clip:
Total number of ammo in inventory:
How I implemented adding weapons to hands
You can see how I implemented a weapon component (AC_WeaponComponent) using this component.
I have created a data table that contains the inventory item class and the weapon actor class.
In the weapon component, I have created a function that takes the inventory item class as input and returns the weapon actor class.
Upon the triggering of the OnEquip delegate, I compare the inventory item class using this function and obtain the weapon actor class.
Afterward, I spawn the required weapon from this class and attach it to the socket.