Using Collectables with Objects

Now that we have created our collectables and door, we are going to combine these two elements to create a unlockable door that can be opened when the player has gathered all collectables.

While we could manually set the amount of collectables needed to open the door, we are going to have Unreal Engine automatically count the amount off collectables in the level at game launch instead.

In the Content Drawer search for “BP_ThirdPersonGameMode”. Double-click to enter and open the Event Graph.

As you can see, we are already using Event BeginPlay. So that we can execute multiple functions we need to add a sequence.

Drag and release from Event BeginPlay. Type and select “Sequence”.

As we want to count the amount of collectables in the scene before loading the HUD, connect the previous node chain to “Then 1”. We can now start our new node chain.

Drag and release from “Then 0”. Type and select “Get All Actors Of Class”. Set the actor class of this node to BP_[Item]Collectable (or whatever you named your collectable blueprint).

From the new node, drag and release from “Out Actions”. Type and select “Length”. This allows us to make the number of keys in the scene into a number we can reference later.

From the new Length node, drag and release. Select “Promote to Variable”. Name this variable “[Items]Total” in the Details Panel on the right. Connect the Get All Actors Of Class node to the [Items]Total node as shown.

We can test if we are successfully counting the amount of collectables by dragging and releasing from the top-right arrow of the Set node.

Type and select “Print String” and connect the mint green and magenta open connections. The Event Graph should look like this.

Compile and test. If successful the number of collectables in the scene will appear in light blue will appear in the top left corner immediately after pressing play. We can remove the Print String node after testing.

Head back to the WBP_HUD Event Graph. We will need to add additional nodes to display not only the items collected, but also the amount of collectables total.

From the blue “As BP Third Person Game Mode” pin of the Cast node, drag and release to select “Get Keys Total”.

Right-click the In Text pin of the Set Text node and select “Break This Link”.

From the In Text pin drag and release to select “Format Text”.

In the new node, enter “{X} / {Y}” into the format . This will tell Keys Text how to display multiple data inputs.

Connect Keys Collected from the UpdateKeyCounter node to X and Keys Total to Y of the Format Text node.

Your Event Graph should look like this. Hit Compile and test. Your HUD should now display the both the total amount of collectables in the scene at the start of play and the amount picked up by the player.

Now we can create our unlockable door.

We are going to use the regular door blueprint we created in the last tutorial as a base for our unlockable door. In the Content Drawer, right click the “BP_Door” and select Duplicate. Rename the duplicated door to “BP_LockedDoor” and double-click to enter the Event Graph.

Right-click the pressed output of the “E” Input node and select “Break This Link”. Drag the separated chain to the side, we will need it later.

We need to get information about the items Collected and items Total from different blueprints. Drag from the “Pressed” output and release. Cast to “BP_ThirdPersonGameMode” and add Get Game Mode to the Objects input. The Event Graph should look like the image above.

From the blue, bottom-right arrow of the new Cast node, drag and release. Type and select “Get [Items] Total” and “Get [Items] Picked Up”.

Drag and release from either new node. Type and select “Equal (==)” and connect the other node. This will activate the chain when the total items and items collected are the same amount.

Create a branch node by holding “b” and left-clicking a blank area of the Event Graph. Connect to the Equals node output to the Condition pin of the Branch node, and the Cast to BP_ThirdPersonGameMode node to the Branch node as shown above. We can now reconnect our door opening chain to the “True” output of the new node.

Your Event Graph should look like this. Compile and test. The locked door should only open when all keys have been collected.