- From the toolbar in the Main Editor view, click the Blueprints button and select Open Level Blueprint.
- In the graph, Right-click and add an Event Begin Play node and a Get Player Character node.
- Drag off the Return Value of the Get Player Character and add a Cast To FirstPersonCharacter node.
Here we are using a Cast node to first get the player character, then get the Character Blueprint used by that player (which in this case is called FirstPersonCharacter and is where we set up our other scripts for the playable character). By using the Cast node, we are able to access our player character's Blueprint and the variables, events, and functions within it.
- Connect the nodes as shown below, then off the Return Value of the Cast To node, select Promote to variable.
Click image for full size.
We are going to create a variable to hold a reference to the FirstPersonCharacter Blueprint the player is using so that we do not have to use a Cast to get to it each time. By doing this upon Event Begin Play, it will save us time later.
- Select the promoted variable, then in the Details panel, rename it to MyCharacter.
- Off the Return Value of the MyCharacter node, add the Assign Get Spawn Location node.
This is the Event Dispatcher we created. When you select this, two additional nodes will be added.
If you do not see this node in the list, make sure that you clicked Compile inside the FirstPersonCharacter Blueprint and try again.
- Make sure you connect the newly added nodes from the Bind Event.
Now, when GetSpawnLocation is called in our Character Blueprint, this new Custom Event will also get called.
- Hold Control and drag in the MyCharacter variable from the MyBlueprint window, then off it add the Set Spawn Location node.
- Return to the Main Editor window and in the World Outliner, search for and select each of the Target Points by holding Ctrl and clicking each one.
- Return to the Level Blueprint and Right-click in the graph and select the Create References to 3 selected Actors.
- Right-click in the graph and add a Make Array node, then connect each of the Target Points to it.
You can add more pins by clicking the Add Pin button. This Array will store a collection of our Target Points which we can access.
- Off the Array pin, add a Get node.
- Off the Get, add a Random Integer in Range node with Min set to "0" and Max set to "2".
Here we are getting a random Target Point for use as our Spawn Point. If you had more spawn locations, you could add them to the array and increase the Max value of the Random Integer in Range node to include them.
- Off the out of the Get node, add a Get Actor Transform node and connect it to the Set Spawn Location node.
Your full script should look similar to above, click for a full view.
We have done a lot on this page! We used a Cast node to access our Character Blueprint and then bound a Custom Event to the Event Dispatcher in it. This allows us to fire off a Custom Event in our Level Blueprint whenever the Event Dispatcher is called. We then randomly are getting one of our spawn locations and kicking it back over to our Character Blueprint as the variable Spawn Location (which is used for the location to spawn our rock at).
In the Next Step, we will allow our rock to be shot and destroyed by using a Bind Event off an Event called OnHit (so when the rock is hit).