-
Notifications
You must be signed in to change notification settings - Fork 1
Gameplay Containers Setup Guide
Step 1: Download the Gameplay Containers plugin from the Epic Games Launcher for whatever engine version you wish to use from 5.1 and above.
Step 2: After downloading, you can find the plugin in the following directory:
{EngineDirectory}/Plugins/Marketplace/GameplayContainers
See the image below for reference:
Step 3: Make sure your project is closed (the editor is not running), then copy the GameplayContainers plugin folder to your project and paste it inside the Plugins folder.
-
Note: Create the
Pluginsfolder if it does not exist already, and paste theGameplayContainersfolder in there.
See the image below for reference:
Step 4: Open your project in Unreal Editor, and go to Edit > Plugins > Gameplay category. Ensure that the plugin "Gameplay Containers" is enabled. If it's not, click on the enable checkbox to the left, and you might be prompted to restart the editor.
See the image below for reference:
Note: Also, make sure the Gameplay Abilities plugin and Enhanced Input plugins are enabled in your project.
Step 5: Now let's get to the implementation. If your project is a Blueprint project (does not have C++ code), you will need to do this step to make it a C++ project.
Go to Tools > New C++ Class and choose Character, PlayerController, PlayerState, or whatever class you would like to use for the Gameplay Ability System component, which is required to use the Gameplay Containers plugin.
Note: For multiplayer games, it's recommended to put the Gameplay Ability System component in the PlayerState or the Character (I prefer PlayerState). For single-player games, it probably does not matter; you can even put it in the PlayerController class.
I'm going to use the player state in this case.
See the image below for reference:
Step 6: After adding the C++ class for the Gameplay Ability System component (in this case, I used the PlayerState class), let's go ahead and edit the code. You will need to have an IDE set up on your computer to edit and compile the C++ code. Visual Studio Community edition is popular and is what most people use. I personally prefer JetBrains Rider IDE.
Step 7: Now that your project is open in the IDE, navigate to Games > YourProject > Source > YourProject > YourProject.Build.cs file.
See the image below for reference:
Step 8: Now open YourProject.Build.cs and add the following module dependencies: "GameplayAbilities", "EnhancedInput", "GameplayContainers".
See the image below for reference:
Step 9: Click "Save All" in your IDE, then close the IDE and Unreal Engine Editor. Next, navigate to your project folder, right-click your Unreal project file, and choose "Generate Visual Studio project files."
See the image below for reference:
Step 10: Now, open the project in your IDE again by double-clicking on the YourProject file (not the Unreal Engine editor file, but the one for your IDE such as Visual Studio or JetBrains Rider).
Step 11: Now, let's open the class we added—in this case, I added the PlayerState—and set up the Gameplay Ability System component.
See the code below for reference:
Step 12: Now, let's initialize the Gameplay Ability System. To do this, we will need to create a character class (or a pawn class if your project does not use the character class). You will need to compile the current code to open the Unreal Editor and add a Character C++ class to your project. You can press the green triangle button to compile and start the unreal engine editor from your IDE (Or simple click save all and then open the editor from your project folder and it will automatically compile the code)
See the image below for reference:
Step 14: In the Unreal Editor, navigate to Tools, select Character Class, and then click Next. Give it a name and press Create Class.
Step 15: Close the Unreal Engine Editor and return to your IDE. You should see the new Character class added there. In that class, you will need to add some functions needed for Gameplay Ability System initialization.
Now the Gameplay Containers plugin provides 4 default ready-to-use containers:
-
Inventory Component: Used to manage items and their quantities.
-
Hotbar Component: Allows quick access to items or abilities. (Recommended to put it in the Character class, can also work in player conrtoller)
-
Equipment Component: Manages player equipment to manage equipped items. (Recommended to put it in the Character class)
-
Gameplay Container Component: General use container component for any actor class and can be customized in subclasses or child classes inheriting from it.
Note: Initialization of Gameplay Containers requires the Gameplay Ability System to be initialized. Therefore, you will need to add the Gameplay Container components (Inventory, Hotbar, Equipment, etc..) from C++. (Unless you expose the required initialization functions to blueprint, which is beyond the scope of this guide).
Step 16: Now, let's add an Inventory, Hotbar, and Equipment components to our Character. In this example, we will use the Character class for all of them to keep it simple.
-
PossessedBy: Override this function to handle when the character is possessed by a controller.
-
OnRep_PlayerState: Override this function to handle when the PlayerState replicates to the client.
-
Inventory, Hotbar, Equipment Components
Step 17: Now, let's implement the functionality we added to the Character class.
NOTE: You will also need to add and implement this function for the equipment component to work properly PostInitializeComponents In your character class, player controller class, or player state...
Step 18: Everything should now be ready on the C++ side. Let's proceed to set up and configure the editor. Press the green triangle button again to compile the project code and run the Unreal Engine Editor.
Step 20: With Unreal Engine Editor open, go to the Content Browser. Press the settings button on the left and ensure that "Show Plugin Content" is checked.
Step 21: Now, let's open our Character class blueprint (if your project was a blueprint project). Reparent your Character class to inherit from the class you created in C++, MyCharacter in this case.
NOTE: If your project was a c++ project already you might skip this
Choose the class you created in C++
Step 22: After reparenting the Character class, you should see that components have been added from the C++ Character class we created.
Step 23: Now, let's configure the components we added to our Character class.
Starting with the inventory component make sure the container definition for each component is selected correctly.
In the container definition data asset you can specify how many slots the container will have and the UI style and widgets used to represent the container in the user interface. You can modify those settings as you wish
Step 24: Make sure the container definition for every container is properly selected.
NOTE: Startup Items allows you to specify which items should be added to the container initially when the game starts.
Step 25: Now, let's add the input mapping context of the Gameplay Container plugin to the player. I do this in the Begin Play function for this project, adding it after the default player mapping context.
Step 26: Now, let's implement the Hotbar component input actions. By default, the Hotbar has 6 slots, so I've added 6 input actions. You can adjust the number of slots and input actions as needed.
NOTE: the slot index always starts from 0 so for the first slot its 0 and last slot is (N - 1)
Step 27: Go to the input mapping context of the Gameplay Containers plugin and make sure the input buttons for all the actions are properly set.
NOTE: these are the default settings.
Step 28: Go to the Gameplay Containers plugin, navigate to Core > Abilities folder, and verify the abilities settings. Make sure the abilities blueprints inherit from your project's main Gameplay Ability class. If you don't have a main Gameplay Ability class for your project, you can reparent them to use the default Gameplay Ability class.
Make sure every ability have the same settings as in the pictures below:
Step 29: If you want to use Gameplay Effects to modify the container capacity using the CapacitySet attribute set, you need to configure which slot type should be affected by which attribute.
NOTE: By default we only modify the inventory capacity by inventory max capacity attribute from InventoryCapactiySet attribute set. You can add your own if you wish
Go to Edit > Project Settings and under the Plugins category, click on Gameplay Container.
Step 30: Give all abilities from the Gameplay Containers plugin to your player character to enable usage of the inventory, hotbar, equipment components, etc.
Since the gameplay ability system is on the player state in my case I will create a blueprint that inherits from the player state class I created in C++ earlier.
NOTE: If you already have a blueprint for the player state reparent it to use the player state class you created earlier in the C++ code.
Step 32: Create a variable of type Gameplay Ability Class and make it an array. This variable will hold all the abilities that should be added to our player's Ability System component on Begin Play.
Add all the ability from GameplayContainers plugin Core/Abilities in this array of gameplay ability classes
Then setup the blueprint code to grant the abilities to the ability system on begin play.
Don't forget to select the blueprint classes to be used by the game mode here:
Step 33: Now let's setup the input for the attachment menu, and the slot context menu. We will need to create a player controller class in C++ for this to implement the IGameplayTagAssetInterface interface. Create a player controller C++ class and open your IDE to edit the source code
Your C++ PlayerController should look like this, Most importantly add IGameplayTagAssetInterface, and OwnedTags variable, then implement the interface function GetOwnedGameplayTags
NOTE: Also make sure that you add "GameplayTags" module to your project Build.cs file like the image below:
Now you will need to close unreal engine editor, and press button in the IDE to compile the code and run the editor.
Step 34: Reparent your player controller blueprint to use the player controller class created in C++
Now make sure you select your player controller class to be used by the world settings and the game mode :
Now open your player controller blueprint, and add the following blueprint nodes:
Next create 2 boolean varialbes in your player controller blueprint:
bWantsToOpenContextMenu and bWantsToOpenAttachmentsMenu
Next, add the following input actions for handling the context menu and attachment menu:
Now go to GameplayContainers plugin UI > Templates folder and open W_Slot widget:
You will see 2 comments (white boxes with text written above):
- For this one cast the owning player to your player controller blueprint, then get bWantstoOpenAttachmentMenu variable and connect it to the end node as shown in the picture below
- Right click the cast node and click convert to pure cast to remove the execution pins from it.
- For the other one cast the owning player to your player controller blueprint, then get bWantsToOpenContextMenu variable and connect it to the end node as shown in the picture below
Now press compile and save for the slot widget blueprint.
Step 35: Now lets setup the user interface (UI)
For the user interface, you can either replicate/adjust the layout from the W_HUD and W_Inventory_Window widgets to your own widgets, or utilize both of those widgets directly. In GameplayContainers/Content/UI
- Assuming that you don't have any UI setup, here is a simple setup to display the inventory, equipment, hotbar and other UI elements for the gameplay containers plugin
In your PlayerController blueprint create 2 variables of type UserWidget (Object Reference)
On BeginPlay event, add the following blueprint nodes, to create the UI widgets for the HUD and the inventory window.
Then by default the HUD widget should be added to the view port.
Now to show or hide the inventory, I use the button "I", the following blueprint code will show/hide the inventory UI every time you press "I"
After adding this you are pretty much done, now you can press Play and test the gameplay containers plugin features: the inventory system, the hotbar component and the equipment manager.
Check the keybindings in the inventory window to see which keybinds perform which action.