Changing target states with branches

Since we want to generate effects that will be triggered by changes in the state applied to our target Cylinder, we have to ensure that this logic is contained within our CylinderTarget Blueprint. Open the Blueprint in your Blueprints folder, and take a look at the node group connected to the Event Hit. Right now, when our projectile hits the Cylinder object, these nodes tell it to swap to a red material. To add the ability to change how the Cylinder behaves when it is shot more than once, we will need to add a check to our Blueprints to count the number of times the Cylinder has been hit, and then trigger a different result, depending on its state.

Let's take a look at a setup that could help us handle this scenario:

To create conditional logic with multiple outcomes in Blueprints, we will take advantage of the Branch node. This node takes a Boolean variable as an input. Since Boolean values can only be either True or False, the Branch node can produce only two outcomes. These two outcomes can be executed by linking additional nodes to the two output execution pins, representing the True path and the False path.

The first step of creating Branch is to determine what will be represented by your Boolean, and what will cause the conditional value to change from False to True. In our case, we want to create a Primed state that shows that the target has been hit and that it could be Destroyed with a second hit. Let's go ahead and create a Primed Boolean variable.

Recall that variables are defined in the My Blueprint panel. You should already see our previously defined variables for Speed and Direction. Click on the + button to add a new variable. Set the Variable Type as Boolean. Give the name Primed to the new variable and check the box labeled Instance Editable to make this value easier to modify externally. Finally, compile and save the Blueprint. Because we do not want our targets to be in a Primed state before they have been hit for the first time, we will leave the default value of our variable as False (represented by an unchecked box).

Now that you have a Primed Boolean variable, drag it from the My Blueprint panel to Event Graph, and select the Get option that appears in the submenu on release. This will grab True or False state data from the variable and enable us to use it to Branch our Blueprints. Click and drag a red wire from the output pin of the new Primed node to empty grid space on Event Graph. Search for and add the Branch node.

Finally, we can add Branch to our Event Hit Blueprint group. Break the connection between the Cast To FirstPersonProjectile and Set Material nodes by holding down the Alt key and clicking on one of the execution pins. Drag the Set Material node out of the way for a moment, and then connect the output execution pin to the input execution pin of the Branch node. This Blueprint will now call the Branch evaluation every time the target Cylinder is hit by a projectile.

Now that we have our Branch node set up for activation, we need to provide the target Cylinder with instructions on what to do in each state. The targets we want to create can be described as being in one of these three states at any time: Default, Primed, or Destroyed. Since a destroyed Actor can't execute any behavior, there is no way to develop any behavior that happens after the target is Destroyed. As a consequence, we really have to concern ourselves with only the primed and the pre-primed Default states.

Let's handle the Default state first. Since this Branch dictates what happens to the Cylinder in each state after it has been hit, we want to execute the Material change that we previously attached to the Event. If the target has not yet been hit, and it has now been hit for the first time, we have to change Material to TargetRed. Additionally, we will also have to set our Primed Boolean variable to True. This way, when the target is hit again, the Branch node will route the behavior to the other execution sequence. The False execution sequence of nodes will look like this:

Drag the Set Material node you moved aside before to the right of the Branch node, and then connect the False output execution pin of the Branch node to the Set Material node's input execution pin. Now, drag the Primed variable from the My Blueprint panel to Event Graph, and select the SET option. Connect this node to the Set Material node's output execution pin, and click on the checkbox next to Primed within the SET node. This will ensure that the next time Target is hit, Branch evaluates to True.

..................Content has been hidden....................

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