In this step, we will set up the logic for allowing the player to press a button to spawn a shootable rock (providing other conditions are met). In our level, we have defined some spawn points and in order to get the location of one of those, we will use an Event Dispatcher which will fire off an event in our Level Blueprint (which we will create in the next step) to get one of those locations and send it to our Character Blueprint.
First, let us allow the player to spawn the shootable rock by key press.
- Inside the FirstPersonCharacter Blueprint, Right-click somewhere in an empty space in the graph and add an F Key Event.
- Hold B and Left-click in the graph to create a Branch node.
- Alt drag in the IsActive variable and check the checkbox then connect as shown below.
- Hold Control and drag in the IsActive and Pulls variables.
- Connect the IsActive to a NOT Boolean node (Not true) and the Pulls to a > node (set to 0).
In older versions of the engine, the NOT Boolean node is referred to as the != node
- Connect both outputs to an And node and feed it into the Condition of the Branch.
Here, we set up the logic to determine if the player can spawn a rock by checking if a variable called IsActive is not true and if the player's Pulls count is greater than 0. If both those statements are true, we will proceed down the True side of the Branch node and spawn the rock (well, that part of the script still needs to be set up which we will do below).
- Alt drag in the Pulls variable and connect it following the Set IsActive node.
- Control drag in the Pulls variable and connect it to a Int - Int node (set to 1) and connect to Set Pulls.
- Click Compile, then off the Set Pulls node add the ResetShots Custom Event.
Your setup should look similar to below.
Click image for full size.
Our logic for when the player can spawn a rock is set, now we need to communicate with the level to get a spawn location. We will do this by using an Event Dispatcher that is called when F is pressed. The Level Blueprint will listen for the Event Dispatcher and when it is called, will fire its own event that will get the location of one of our spawn points and pass that back to our Character Blueprint.
- Click the + Event Dispatcher button in the My Blueprint window.
- Click the new Event Dispatcher and press F2 with it selected to rename it and call it GetSpawnLocation.
- In the graph following the Reset Shots function, add the Call GetSpawnLocation node.
The Event Dispatcher will be added which is denoted by the envelope icon in the upper-right corner of the node.
- Off the GetSpawnLocation node, add a Spawn Actor from Class node.
- On the Spawn Actor node, set the Class to BP_Rock.
- Also on the Spawn Actor node, drag-and-drop the SpawnLocation variable onto the SpawnTransform pin.
Our Character Blueprint is now set up to spawn a shootable rock and receive a Transform location to spawn the rock at.
In the next step, we will use the Level Blueprint to get the location of one of our spawn points and pass that information to our Character Blueprint when our Character Blueprint's Event Dispatcher is called (so we are going from Character Blueprint to Level Blueprint and back to Character Blueprint, passing information between them).