Coffee Shop Sim - Chapter 3 - Picking and dropping items with interactables using C++ & BP


In this new chapter, we are going to continue with the customer's order.

The customer will be waiting at the counter until we deliver each item on their order.

When every item has been received, the customer will go exit the coffee shop.

We will use different interactives to make each item in the customer's order.

You can download the CoffeShopSim_Chapter3.zip file to see the final result. We are using Unreal 4.26.

You can follow along this chapter here:

Interactables

We will use interactables for every kitchen appliance or pickup item.

If you open the CoffeShopSim_Chapter2.zip files from the previous chapter, you will find a basic interactive class called BasicInteractive.


This class has a UBoxComponent Trigger and a reference to the Player.

A reference of the Player is stored when they enter the Trigger. Any child that extends this class will have that reference.

The other basic code for interactives is the Interface Interactable, which contains a BlueprintCallable OnInteract event.

Every interactable object in the map uses both the base class and the interface.

When the Trigger detects the Player, it calls the method OnEnterInteractive and sends a reference to itself to the Player. 

The method OnEnterInteractive checks if the reference implements the interface. If this is the case, a reference to the interface is stored on CurrentInteractiveActor.

When we use the E key or the face button down, if the Player has a reference to the interface (so it's on the Trigger of the interactable), it calls the OnInteract of the CurrentInteractiveActor.

This way, we can use any actor that implements the interface, and the actor will do whatever it needs to do. The PlayerCharacter doesn't care what it does, only that it's going to interact.

The Pickup Interactive

The first interactable we are going to implement is a Pickup. When the player is on this interactable and presses the interact key, they will receive an item.

I created 2 pickups, one that gives you yummy croissants and another a cup of coffee that we can use on the coffee machine.

The project files already contain the class PickupInteractive. If you don't have it, create a new C++ and select BasicInteractive as a parent.


To make this work, the PikcupInteractive must implement the interface IInteractable and the event OnInteract.

We are also going to need a new property FItem called Item, with an ItemID and an actor that we can spawn on the hands of the player.

FItem is a new structure that we need to declare on the Definitions.h

The ItemID FName property will be useful later to check if the player already has something on their hands.

The ItemClass property will be used to spawn an actor on the player's hands to carry.

We will need new functionality on the player to pickup items, but first let's take a look at the implementation of the OnInteract.



We will need new functionality on the Player to pick up items, but first, let's take a look at the implementation of the OnInteract.

The implementation checks if the PlayerCharacter has already on the hands, and if not, it calls the event OnReceiveItem with the Item we have on the Pickup.

We also call ChangeAnimation to put the Player in Idle animation.


Before we continue, let's implement these extra things on the Player.

Adding Pickups to the Player

On the class PlayerCharacter (CoffeeShopSimCharacter.h)

We need the following properties and methods:

  • bHasItemOnHands: Boolean to indicate whether the player already has something on the hands or not.
  • ItemOnHandsID: ID of the Item on the hands.
  • Event OnReceiveItem: To call when we pick up an item, and receive the Item FItem
  • OnGiveItem: Event to call when we remove the item we currently have on the hands.


OnReceiveItem: In this event, we check if we already have items on the Player's hands. If not, we store the itemID and change the boolean bHasItemOnHands to true.

OnGiveItem: This is the opposite method; we check if the Player has something on the hands, and if it has, we remove the references.

Since OnReceiveItem and OnGiveItem are BlueprintNativeEvent we can spawn the object on the Blueprint instead and leave the C++ part behind.

Open the Blueprint BP_MainPlayer and before we do anything else, create a SceneComponent called ItemSpawner. We will use this scene component to spawn the item here.

Create an Actor variable called ItemOnHands, which will be a reference to the Blueprint spawned after pickup.



Spawning items on the player

Add the event OnReceiveItem, and don't forget to add a call to the parent, so the C++ part is executed.

The rest of the function is easy. Use the Item Spawner scene component we created earlier to spawn the Item Class (which will be a blueprint) on the right position and location and attach it.

Save the reference for later.

The event blueprint OnGiveItem simply checks if the Items On Hands is valid and destroys the actor.



Creating pickups on the map and the blueprint item to pickup

To finish this part, we have to place on the map 2 blueprints based on Pickup Interactive. One for the croissant and the other for the coffee. 

Create a new blueprint class and find under All Classes the Pickup Interactive on the content browser, call it BP_PickupInteractive. We can duplicate this one on the map, adjust the positions and settings. (You can also add a pile of croissants as I did, so it's more visually interesting)

Create 2 new actor blueprints for each pickup interactable, which we will use to spawn later. These are simply normal actor blueprints.

I created the BP_CupCoffeeItem and the BP_CroisantItem.

Finally, set up each blueprint Interactable Pickup with the following properties:

If you have done everything correctly, you should be able to approach any of those pickups, and if you press the interact key, you should see the item in the player's hands.


Dropping items on the counter

We need an interactable to drop items, which will be the red counter in front of customers.

Because I decided a customer can only order three items maximum, the drop interactable only allows three items spawned on the counter.

We need a new interactable called DropInteractive. It will extend from BasicInteractive and implement the Interactable interface.

Create this C++ class and add those two elements to it. Don't forget to add the includes.

Add the following properties and methods:

  • MaxItems: Maximum items to drop, which is editable on the editor. It will be 3
  • ItemsOnInteractive: Array of items that the player give the interactable. It will be helpful on the blueprint based on this actor later.
  • OnItemDropped: Event with the player's Item drop, we will use this to know when and what the player drops.
  • OnInteract, OnInteract_Implementation: We must implement these 2, as the Interactive extends the interface IInteractable, and we will use it to do the drop logic C++ portion

The implementation of the OnInteract event is very similar to the pickup one but in reverse.

We check if the player has anything on the hands, and it does; we add it to our ItemsOnInteractive, and we call OnGiveItem on the player.
When the function OnGiveItem is executed on the player, the player blueprint will remove the items from the hands.

We can create now a Blueprint based on the DropInteractive to finish with the logic. In the example, I created the blueprint and added a few static meshes to represent the counter. I also added 3 Scene Component, which I will use to spawn each item.


To spawn each Item, we use the event OnItemDrop, giving us the Item to spawn. To select in which ItemDrop spawn it, we use the array ItemsOnInteractive. Using the length - 1, we can get the current index (if we have 1 item, the index is 0) and know what ItemDrop uses (0,1,2).

This would be very handy if you used an array instead of each element.
We use the switch to check that index, and the node SpawnToActor spawns the class on the Item dropped.



Removing the item from the player

On the player blueprint, we only need to implement the event OnGiveItem. The only action we need to take is to DestroyActor using the ItemOnHands variable you created earlier. (Only if it's valid)

If you test all of this, you should be able to pick items and drop them on the counter (only 3 at max)

And this is the end of the tutorial!

In the next chapters, we will continue with the interactives and with the customers.

Files

CoffeeShopSim_Chapter3.zip 61 MB
Nov 14, 2021

Get Coffee Shop Simulator Tutorial in Unreal Engine 4

Leave a comment

Log in with itch.io to leave a comment.