hey there over the next few minutes
we're gonna talk about four loops in
blueprints so here I have a really
simple blueprint based off of actor
there's currently nothing in it so just
as we can see it in the level I'll go
into add component will drop down a
billboard so we have a little dragon
head and we can go straight over to the
graph so a loop is really just a way for
you to create a portion of your script
that can repeat a certain number of
times a finite number of times because
once you get to infinity you start to
have problems so let's go ahead and set
something up I'm gonna jump over to my
defaults real quick and I've done this
several times in other videos I'm gonna
set my auto receive input to player 0 so
that I can just press keys and make
stuff happen let's do something based
off of the F key I don't know just like
the F key right now and let's create a
loop so if you right-click and type in
the word loop I have a bunch of
different loops you can choose from two
of these are associated strictly with
arrays I'd be the for each and the for
each with break we're not going to mess
with those right now really all I want
to focus on right now is just a plain
Jane regular for loop so the way a loop
works is you give this a first index and
a last index and it runs whatever is
connected to loop body the number of
times it counts from first to last so
let me go ahead and set this up real
quick we'll do a unpressed we'll make
this work and we're just gonna hard code
this for now we're gonna set the first
index to be one and the last index to be
ten and for simplicity sake I just want
to put print some stuff out to the
screen so let's right-click and we'll do
a log which is the old school name for
print string and actually let me slide
that out of the way not not too far out
of the way we'll put it like over here
because I'm actually going to show you
guys a sort of a little hidden trick
we're gonna do a format text and let's
say the loop has run and then we'll open
braces index close braces times
exclamation point and
press enter it automatically grabs
whatever I typed within those braces and
it makes an input for it so I'm gonna
take the current index and plug that
into index and you see that it
automatically converts that for me so we
go from an int to text which is pretty
cool and then we'll take our result and
we will print that out to a string now
the only thing we need to do is make
sure we cover our print string run that
all the way back to the loop body and
then finally we'll take our completed
pin and we will add one more print
string or type log whatever and we'll
set this to the loop is done I wanted to
type the loop the loop the loop is on
fire but I decided not to at the very
last minute I will take the text here
though and I'll change its color to
something red just we can see the
difference in the two strings firing off
okay so let's hit compile and we'll move
this out of the way let's go ahead and
bring a copy of our blueprint into the
scene and we can't see it cuz we're in
game mode so there it is
and we can hit play and like go ahead
reach back there and turn my sound off
because I don't want that to distract
anybody and I feel like it could cuz my
speakers are terrible on this machine
and if I hit the F key you can see the
loop doing its job so I'm gonna come
over here where there shadow so it's
easy to see on the contrast of your
monitor so boom the loop is run one two
three twelve five six seven eight nine
ten and then it says the loop is done no
problem right and when you're doing
something like this where the loop has
only got to run like ten times you know
my computers eating that for breakfast
that's no problem at all but you have to
be a little bit careful when it comes to
very very large loops because your
computer may or may not like that now
I'm gonna show you how to do something
that I'm gonna warn you could cause a
crash that could happen depending on how
many zeros you put in here we're gonna
say one zero let's what do you want to
try you wanna try ten thousand so we're
gonna make our little loop count to
10,000 real quick just to prove a point
and that point is that while you're
playing the game as soon as the loop
starts it is going to pause execution
until the loop is complete watch when I
hit F oh there's a hitch and then
suddenly we get our end result all at
once see that so I just
really show that now I'm gonna add one
more factor observer so I'm gonna do a
hundred thousand now my video will keep
up I'm not necessarily sure I would
recommend you to do this on your end but
you know if you're if you also like to
live dangerously then you can do it so
I'll hit F up everything is stuck it's
not moving what's going on and it's a
little scary for just a minute and then
after X number of seconds boom we get
the loop has run 100,000 times but
that's just the important thing to keep
in mind with loops right they're going
to halt execution until they're done so
generally speaking you want to keep your
loops a little lighter than say 10,000
iterations so we'll set this back to
something like 10 so we're a little less
likely to break so that's really all
there is to four loops now this first
index and that last index you can you
can make those dynamic you can drive
those off variables that change based
off what is going on in here in your
levels so really this is just anything
that you want to have happen a set
number of times so that is going to wrap
things up for this video thanks a lot