Language:
Page Info
Tags:
Engine Version:

3. Spawning the Shootable Rock

Choose your OS:

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.

  1. Inside the FirstPersonCharacter Blueprint, Right-click somewhere in an empty space in the graph and add an F Key Event.

    FKeyEvent.png

  2. Hold B and Left-click in the graph to create a Branch node.

  3. Alt drag in the IsActive variable and check the checkbox then connect as shown below.

    BranchAdded.png

  4. Hold Control and drag in the IsActive and Pulls variables.

  5. Connect the IsActive to a NOT Boolean node (Not true) and the Pulls to a > node (set to 0).

    TwoNodesAdded.png

    In older versions of the engine, the NOT Boolean node is referred to as the != node

  6. Connect both outputs to an And node and feed it into the Condition of the Branch.

    AndAdded.png

    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).

  7. Alt drag in the Pulls variable and connect it following the Set IsActive node.

  8. Control drag in the Pulls variable and connect it to a Int - Int node (set to 1) and connect to Set Pulls.

    PullsSet.png

  9. Click Compile, then off the Set Pulls node add the ResetShots Custom Event.

    CallCustomEvent.png

    Your setup should look similar to below.

    ScriptBeforeEventDispatcher.png

    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.

  10. Click the + Event Dispatcher button in the My Blueprint window.

    AddEventDispatcher.png

  11. Click the new Event Dispatcher and press F2 with it selected to rename it and call it GetSpawnLocation.

    RenameEventDispatcher.png

  12. In the graph following the Reset Shots function, add the Call GetSpawnLocation node.

    CallEventDispatcher.png

    The Event Dispatcher will be added which is denoted by the envelope icon in the upper-right corner of the node.

    NewNodeAdded.png

  13. Off the GetSpawnLocation node, add a Spawn Actor from Class node.

    SpawnActorNode.png

  14. On the Spawn Actor node, set the Class to BP_Rock.

  15. Also on the Spawn Actor node, drag-and-drop the SpawnLocation variable onto the SpawnTransform pin.

    SpawnActorComplete.png

    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).

Tags