Weapon Switcher is a template that allows players to switch between multiple weapons.
The template uses the inventory and item components, as well as data tables. The player will be assigned a new inventory and be given weapons at the start of the game. A UI will be created to display the current weapons the player has in their inventory. The template also includes a binding set of actions for switching weapons.
This template works with the Weapon Spawner template.
Drag and drop the Weapon Switcher template into the Hierarchy.
Preview the project and use “Q”, “E”, “1”, “2”, or “3” to switch weapons.
How to Use
The root object of the template has 3 custom properties.
- Weapon Inventory
A template of an inventory that each player will be assigned to hold the weapon slots.
- Starting Inventory Table
A data table of weapon items that each player will be assigned at the start of the game.
The distance each UI weapon slot will be spaced from each other.
Creating a Weapon Item
- Create a new template for the weapon equipment.
- In Project Content window, create a new item asset.
- Assign it a name and select the item asset to see its Properties.
- Drag and drop the weapon equipment template into the Item Template property.
- Add 3 custom properties to the Item Asset.
- Name: “Slot”, Type: Int, The slot number that this weapon will occupy when equipped.
- Name: “Icon”, Type: Asset Reference, The 2d icon that will display on the UI
- Name: “Ammo”, Type: Int, A reference for the amount of ammo which should equal the starting ammo amount. Do not add if ammo is not required (sword for example).
- Right click the Ammo custom property, and select the Enable Dynamic Property option.
- Open the Starting Weapon Inventory data table and drag the Item Asset into one of the rows.
To change the amount of inventory slots, the Weapon Inventory template needs to be updated.
- From Project Content, drag and drop the Weapon Inventory template into the Hierarchy.
- Select the Weapon Inventory object and open the Properties window.
- Set the Slot Count property to the desired amount.
- Right click the Weapon Inventory object and Update Template From Object.
- Delete the Weapon Inventory object from the Hierarchy.
The server script is connected to an event for the player to equip a new weapon and add the item to the inventory. The syntax is as follows:
Events.Broadcast("AddInventoryWeapon", weapon, item)