Implement Custom Pawn

## How to implement a custom pawn

**FOR version 4.9 and onwards look here**

**4.8 instructions below!**

### Goals

The goals of the tutorial is to implement JcInventory into a already established game. Because JcInventory is made for expansion it uses interfaces so the pawn JcInventory uses is not hard coded into the system. It is recommended you have a good understanding of Ray tracing and Interfaces. For the example we will be implementing the 4.8 version of JcInventory (quick bar release) into the 4.8 Third person example provided by epic. Note that as of 4.9 a third person example is provided with jcinventory as well as the FPS example. If you are starting a new project the easiest way to use JcInventory would be to build a pon those pawns.

**To follow the guide,**

1. create a new third person example project in unreal engine using blueprints,
2. Have JcInventory download and migrate it across to your new third person project.
3. **Have a intermediate knowledge of Blue Print and Unreal Engine Pawns**

### Step one, copy and pasting the RPC/InterfaceFunctions.

As stated earlier, jcInventory uses Interface functions in order to not be hard coded to any on type of pawn. It is recommended that the RPC Graph contained within the JcInventoryCharacter be directly copied over. To do this we need to do the following

1. Open up your pawn you wish to use (in this example we are using the ThirdPersonCharacter pawn, we will be adding the 'JcPlayerInventoryInterface' blueprint interface class to the third person character. Once you have done this save and compile.
2. Make a new Graph in the Third Person Character and call it 'MyPawn RPC Graph'.
3. Open up JcInventoryCharacter located in JcInventory/Demo, Expand its event graph and then select the RPC Graph. See fig 1.
4. Copy all the nodes from the JcInventoryCharacter inside the 'RPC GRAPH' graph and paste them into your newly created graph called 'MyPawn RPC Graph'.
6. Right click on any use of the Varible called JcInventoryCore, and select Create Variable 'JcInventoryCore' - see fig 2
7. Select the Newly created JcInventoryCore varible and make sure its replication is set to replicated. see fig 3
8. Open the Interface function inside the Third Person Character called 'Get Player Inventory Core' and plus the variable JcInventoryCore into the return variable JcInventoryCore. see fig 4
9. Save And Compile

### Step two, adding in the JcInventoryCore to the player

In this step we will be adding a JcInventoryCore actor to the pawn on spawn and setting up the clients HUD.

1. Go to the main event graph and add an authority role node to Begin Play.
2. Drag off from the Authority role execution pin and create a Spawn actor node. Remember that the Spawn Actor needs a transform, 0, 0, 0 will be fine. see fig 5
3. Set the class to a JcPlayerInventory
4. Drag the Actor return pin and set it to our variable JcInventory core, see fig 5
5. Create a new Event called SetUpClientHud and set its replication to Run On Owning Client, then select 'Reliable' and make sure it is set to true.
6. **NOTE: IF YOU ARE USING JCINVENTORY 4.9+ RELEASE YOU CAN SKIP THE FOLLOWING STEPS AND CONTINUE ON STEP 8**
7. 4.8 release has an issue with spawning pawns and possessing them when they are not set to Default pawn. To Fix this issue you can follow this video here. The following instructions will assume you have made the relevant changes to JcInventoryHud.
8. Create a event node overriding Event Possessed, then have it call the function you made called 'SetUpClientHud'
9. From the 'SetUpClientHud' event get the players HUD, casting it to a JcInventoryHUD, then call the command from JcInventoryHud Set Up Jc Inventory Hud. See fig 6

### Step Three, Adding Functionality to our new Pawn

In this step we will be adding functionality to our pawn,This includes adding in key binding to open the inventory, adding in raycasting to pick up objects.

**NOTE: This code is up to you and your game, the examples provided with JcInventory should be treated as Stubbs to examin and learn how to interact with JcInventory. I will provide an *example* of ONE type of method you could use for picking up items but there are many many ways to go about doing this.**

 Creating a controller that feels good for a player in third person is slightly more complicated then first person, as it is harder to 'aim' without a crosshair. For this example we will be using a sphere trace and picking up the object closet to the player, if that object is an Container, we will open the container. This is a simple implementation that does not handle edge cases but should demonstrate the fundamentals of how to bind logic to the actions you want in JcInventory. Because of this implementation the Hand icon is not Appropriate, something along the lines of Tom Loomans Highlight Material would be a nice touch though

#### **This walk though is for setting up Item pick ups and Container windows**.

1. First thing we must do is set up some custom functions to handle Replication. Open the Event Graph of the Third Person Character.
2. Create an Event Called 'Pick Up Items'
3. Create a Event Called 'Server\_Use' set its replication to Run On Server, and set it to reliable.
4. Make a input event for the 'E' key, then check if the role is Authority or Remote, if we are Authority then call 'Pick Up Items' if we are remote call ' Server\_Use'
5. Set the custom event 'Server\_Use' to call the event 'Pick Up Items' - *see fig 7*
6. On 'Pick Up Items' create a Check Authority Role, just to remind us that this function should only fire on the server. Only use the Auth pin of this node.
7. In the Viewport of our Actor set up a Collision sphere called item Collision sphere or something similar, set its Radius to around 150, and set its collision to Trigger. *see fig 8*
8. Make a function Called GetOverlappingItems and have the logic set up to return all overlapping JcInventoryItems and JcItemContainers, *see fig 9 (note the local variable Temp Actors)*
9. Make a Function called Return Closest Actor, and have it use a for each loop to return the closest actor from an array of actors, *see fig 10*
10. Here we can use the functions we have set up to 'pick up' the closest actor! Inside the custom event 'Pick Up Items' add to the authority pin the Get overlappingItems function, followed by the get closest actor function.
11. From here, cast the returned actor to a JcInventoryItem. if the cast fails we know the closest actor is a JcItemContainer.
12. If the cast succeeds we can access the JcItemData and the Items Amount from it directly, then use a RPCAddItemToInventoryInterface, making sure the target jcinventory is our stored variable JcInventoryCore. *see fig 11*
13. Then we should destroy the item to it can not be picked up again! Make sure you do this after you add the item so as not to destroy its ItemData.
14. If the cast fails grab the Actor from the closest actor function and call the function RPCOpenContainerWindowOnClient. *See fig 11*

#### **Setting up the 'I' key to open the inventory**

1. Make a event key for I,
2. On pressed get the players HUD and call the function GetUserInterfaceWidget
3. From the Widget reference returned from GetUserInterfaceWidget call the interface function ShowHideInventory and set the Toggle bool to true, *see fig 12*

#### **Setting up a key to fire a quick bar slot**

1. similar to above, we find the players HUD and then call the interface function RequestQuickBarAction. *See fig 13*