Casting in Blueprints

There is a node named Cast To that tries to convert reference variable types to new specified types. To understand casting, it is necessary to remember the concept of inheritance between classes, as we covered in Chapter 3, Actors and the Gameplay Framework.

The following diagram represents a Blueprint called BP_GameModeWithScore. Game Mode Base is the parent class of this Blueprint. Based on the inheritance concept, we can use a variable of the Game Mode Base object reference type to reference an instance of BP_GameModeWithScore. However, this variable is unable to access the variables and Functions that were defined in the BP_GameModeWithScore Blueprint:

Therefore, if we have a Game Mode Base object reference, we can try to cast this reference using the Function Cast To BP_GameModeWithScore. If the instance is of the BP_GameModeWithScore type, then Cast To will succeed and return a BP_GameModeWithScore object reference that we can use to access the variables and Functions of BP_GameModeWithScore.

Let's do this example in the Blueprint Editor:

  1. Create or use an existing project, based on the Third Person template, with the starter content.
  2. Click the Add New button in the Content Browser and choose the Blueprint Class option.
  3. On the next screen, choose Game Mode Base as the parent class.
  4. Name the Blueprint BP_GameModeWithScore and double-click it to open the Blueprint Editor.
  5. In the My Blueprint panel, create a variable named GameScore of type integer and create a Function named AddGameScore.
  6. In the Details panel of the AddGameScore Function, add an Input Parameter named Score of type integer. This Function is used to add points to the GameScore variable.
  1. In the graph of the Function, add the Actions shown in the following screenshot. To add the GET and SET nodes of the Game Score variable, simply drag the variable, drop it into the graph, and choose either GET or SET. The Print String Function is used to display the current value of the Game Score variable on the screen:

The node that is between SET and Print String is a converter. To create it, simply connect the SET output parameter to the In String input parameter of Print String. Because the parameters are of different types, the converter is created automatically.
  1. Compile and save the BP_GameModeWithScore Blueprint. The next step is to set the Level to use BP_GameModeWithScore as Game Mode.
  2. In the Level Editor, click the Settings button and choose World Settings. In the GameMode Override attribute, click the drop-down menu and choose BP_GameModeWithScore, as shown in the following screenshot:

  1. Create a Blueprint and use Actor as the parent class. Name it BP_Collectable and open it in the Blueprint Editor.
  2. Click the Add Component button in the Components panel and choose the Static Mesh Component. In the Details panel, choose the SM_Statue Static Mesh and in Materials, go to Element 0 and choose M_Metal_Gold. Also, change Collision Presets to OverlapAllDynamic, as shown in the following screenshot:

  1. Right-click Event Graph and add Event ActorBeginOverlap. Other Actor is the instance that overlaps the BP_Collectable Blueprint. Drag from the blue pin of Other Actor and drop in the graph in order to open Context Menu. Choose the Cast To ThirdPersonCharacter Action, as shown in the following screenshot. ThirdPersonCharacter is the Blueprint that represents the player in the Third Person template. We are using the Cast To Action to test whether the instance referenced by Other Actor is the player:

  1. Right-click Event Graph and add the Get Game Mode Function. Drag from the blue pin of Return Value and drop it in the graph in order to open Context Menu. Choose the Cast To BP_GameModeWithScore Action. Drag from the blue pin of As BP Game Mode With Score, drop it in the graph, and choose the Add Game Score Action in the Context Menu. Type 50 in the Score input parameter. Right-click Event Graph and add the DestroyActor Function. Connect the white pins of the nodes. The content of Event ActorBeginOverlap is shown in the following screenshot:

The Get Game Mode Function returns a reference to Game Mode used by the current Level. But the type of the return value is Game Mode Base. By using a variable of this type, we were unable to access the Add Game Score Function. Therefore, it was necessary to use Cast To BP_GameModeWithScore.
  1. Compile BP_Collectable. In the Level Editor, drag and drop some instances of BP_Collectable in the Level. Click the Play button to test the Level. Use your character to collect the statues.
The two white connection pins that appear in the previous screenshot are called reroute nodes. They can be added from Context Menu and are used to aid in the organization of the Blueprint.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset