in this video when I talked a little bit
about while loops now while loops are
something that you're probably not gonna
use a whole lot in blueprint unless you
start writing algorithms but I will give
a kind of an academic demonstration of
how to use one the way a while loop
works is that it is going to constantly
fire the loop body while a certain
condition is true now you have to be a
little bit careful with that because
there they are a very easy way to crash
your game because of that so if we do
just like a simple print string and it
doesn't really matter what it says and
we just set condition to true this means
that this is always going to fire
because while the condition is true it's
going to keep working and if we have
hand coded or hard-coded the condition
to true if we go ahead and jump back
over to the game and hit play the moment
I hit F we are going to freeze the game
have to wait a few moments until it
detects and out of range loop and
suddenly the game dumps right back out
so you generally don't want to do that
that's gonna be a bad idea what I'm
gonna do though is show you how to do
something like store a certain number of
random numbers into an array now again
this is a pretty much just
straightforward academic use of
something like this but for starters
let's make an array so I'll do a
variable and we'll call this my int
array make sure the variable type is set
to end and make sure you hit the array
button I'm going to bring this in as a
getter and we are going to add a value
to it so grab an array add node like so
and we'll do that on the loop body now
what are we gonna add well I'm gonna
drag a wire backwards off the add node
and we'll just say random and we will do
a random integer with a max value of a
thousand so we'll get a random number
between zero and a thousand and we'll
plug that into the array now again this
would still kill right now it wouldn't
do anything if we set this to true that
would just go on through infinity so
what we'll do is on our condition over
here let's grab our interrogate
and I'll hit get and we're going to do a
length test on this
there's the array length that's the
number of elements in the array and
we'll say if this is less than 20 well I
don't know why I would punch the number
in there it's bad habit so while it's
less than 20 then the condition will be
true and the loop will run as soon as
that length hits 20 then the condition
will be false and the loop will stop
running so essentially what a loop what
a while loop is doing is going to run
whatever is connected to the loop body
and then well actually let me let me
rewind it's gonna start by checking the
condition and if the condition is true
then it'll run the loop body then it'll
check the condition again and then
they'll run the loop body if it's true
so it just goes back and forth
eventually the condition will be false
at the moment that happens it will fire
off the completed branch so with us
constantly increasing the size of the
array this will end but we're not going
to see anything meaningful from it it's
just going to store 19 random numbers
and that'll be the end of it so what we
will do is once we hit completed let's
print out the contents of the array so
I'll bring it another copy of the my int
array let's do just a regular for loop
let's do a for each right there and
we'll plug our array into that and then
to the loop body we will print so do a
print string let's take our array
element we'll plug that in that
automatically gets converted so that we
can see the numbers and then when that
is done we will clear the array so it's
going to erase whatever's in the array
so fairly simple setup again it's just
gonna check the condition and then just
continue working so let's jump back and
test so we'll hit play and I hit F and
we get a stack of random numbers hit it
again we get a different stack of random
numbers and so on and so on and so on so
it's really all there is to it you do
have to be very
very careful of the while loop you will
probably not use it very often if really
ever until you start doing things like
writing algorithms and blueprint you
probably won't need to use it very much
in fact even in this case it would be a
lot easier for me to run a for loop or
at least a lot safer and a lot more
difficult for me to run into a potential
infinite loop problem but that's enough
to get you started so that's going to
wrap things up for this video thanks a
lot