FLOW Control, FUNCTIONS AND CUSTOM EVENTS

And so far what we've been doing has been relatively simple but equally your code gets more complex you lot're going to need more ways to control what the Engine is doing.  We've actually already used a few of the nodes covered here but I'll explain them once again for the sake of abyss.

00a_PlayerInput.JPG

00b_PlayerInput.JPG

Firstly an aside on workflow and testing.  Normally in a game y'all'll have some sort of input that triggers off the event or functionality you're working on.  All the same for testing purposes that tin exist quite fourth dimension consuming, then if y'all click on the Blueprints class defaults it's possible to get the Input to Auto Receive from Player 0 - this allows you to but use the keyboard key nodes in your blueprints and create very quick and hacky interfaces for testing - just remember to take them out again afterwards you're finished!

FOR LOOP

Firstly make certain you lot're using a For Loop and not a For Each Loop - we'll go onto what they're used for later on, when we cover Arrays.  Basically a For Loop will run as many times as the departure between the Kickoff and Last Alphabetize.  And then if we put in values of 0 and 5 nosotros'll go half dozen loops.

Y'all'll observe here we've subtracted i from our Number of Boxes so that if nosotros put v in as our Number of Boxes Variables we get 0:four which is 5 loops, you lot'll run across why this is benign in a second.  Each loop too includes the loop index number as the Index value - and then the start loop is Index 0, second 1 etc. this allows us to exercise something incremental to our logic - in this case we're multiplying the Alphabetize past a Z Offset float (150 by default) which moves the boxes up: 0, 150, 300, 450 etc.

Notation for the first box to not move we need the first index to exist 0, hence why we do 0:4 instead of 1:five.

After the Loop has finished the Completed Execution pin volition fire allowing the rest of your lawmaking to run.

Branch

02_Branch.JPG

Some other one we've already used - the Co-operative node only takes a Boolean input and either does the True or Fake execution pin depending on the Value of the Bool.

(Ofttimes this would be called an If node, and actually, Unreal knows this and will select Branch for you lot if you type IF into the autocomplete search for nodes.)

DELAY

The Delay node does what information technology says - waits as long as the input float and then carries on executing.  The input value is in seconds of existent time.

Note if yous want to make a loop with a Delay you demand to practise something manually equally is shown above.  If yous effort and add a Filibuster to a For Loop each loop will execute in one frame, hit the Delay and then only one execution volition happen once the Delay has expired.  If you practise make a loop with a Delay like this then brand sure you have some logic that breaks the loop - or else information technology volition run repeatedly, which isn't ever advisable.

Lastly Delays and other latent nodes (nodes which depend on time, such as Timelines) can't exist used inside of Functions - this is a design optimisation only tin always be worked around using Custom Events in the Event Graph, just something to remember when you're designing your Blueprints.

SWITCH

We covered Switch on Int earlier - it chooses an output pin based on the Value of the Int.  There are likewise Switches for hundreds of other Variable types simply Int is probably the most common and useful.

DOONCE

Another node that does what information technology says - the DoOnce node will only burn the execution pin the first fourth dimension information technology's triggered, ignoring all other inputs until it receives a Reset input.  Here we're Printing the Index for our loops merely we'll just Print the Once string in one case - endeavour it out in level and see.

SEQUENCE

06_Sequence.JPG

A very useful node for laying out your work and keeping things organised.  The Sequence node just executes all the output pins in order, in the aforementioned frame.  There's no functional difference between the two images above, except the first ane is cleaner and easier to read.  In one case you offset making very circuitous Blueprints yous'll exist glad of any means you can keep thing make clean.

ISVALID

07_IsValid.JPG

The IsValid node isn't technically period control but it works in the same style then we'll comprehend it here.  What this does is checks if the Input Object is a Valid Object and Returns a True/False.  What does an Object mean to be Valid? well it means that information technology is has the right parameters assigned and isn't currently flagged to be deleted.  If yous've ever seen the "Accessed None" error in Blueprints and so this is why - the Variable you're trying to get isn't currently assigned to anything, and so the engine doesn't know what to do.  Once nosotros get on to creating and destroying components in a afterward tutorial nosotros'll need some IsValid checks to brand sure we're not getting these errors.

CUSTOM EVENTS

Then before nosotros talk nigh Custom Events, lets have a expect at the Events that come up ghosted (transparent) by default in the Event Graph:

BeginPlay - we've been using this already, basically this runs once when the game starts - great for setting up initial states and starting other things off.

ActorBeginOverlap -More game design than VFX but still useful - this triggers once when a physics object overlaps the collision of the Blueprint - we'll look at this in more than detail only really useful for triggering off cinematic things like sparks when a player goes through a door etc.

Issue Tick - This triggers every frame - WARNING: this can be very expensive.  Generally near things don't need to update every frame, except animations.  Note hither I've used a Do N node to Print String just 10 times, so it doesn't spam the screen.  Do Due north is the same as DoOnce simply with Due north times instead of only 1.

Custom Result -now we're getting to the powerful stuff - we can create as many Custom Events every bit we need and call them from other parts of the Engine or Blueprint.  Finally i'grand only using a Enter key (with AutoInput fix to Actor 0), to trigger off this Custom Event.

Functions

Finally nosotros get to Functions - these have a lot of similarities with Custom Events, in that they can be chosen from other parts of the Engine or Blueprint.  Both Functions and Custom Events tin likewise have an Input - here our Input is a Float value chosen Input.  Considering they're self contained bits of code Functions tin can likewise have Outputs - here we're just simply taking the Input value and multiplying by 2 then Returning the Result equally our Output.

We also have a Function to Reset our Value to ane and Keyboard Keys to trigger these off (remember the AutoActivate!).

This sort of setup is called Abstraction in coding - keeping each part of the code cocky contained and separated so that it's easier to work on and recollect about.  Using lots of Custom Events and Functions will make your Blueprints easier to work with and empathize and greatly assist if you're working in a team surround and other people will need to edit your work.