‫Hello and welcome.
‫In this lecture, we're going to be making it so that when you use up all of your projectiles, your
‫game will automatically restart the current level.
‫After a pause of 5 seconds, let's dive in and see how this is done.
‫So one element of this game that's not particularly fun right now is that when you run out of ammo,
‫it's a dead end.
‫You can't try again.
‫You can't do anything else.
‫You can't move on to another level.
‫Now, the way you can fix this is by allowing us to reload the current level.
‫The way you can do that is using a special node in blueprint.
‫So if you right click in the event graph, you open up your level blueprint and right click in the event
‫graph.
‫We can search for open level.
‫And there's two options here.
‫There's an open level by name and an open level by object reference.
‫We can use the open level by name.
‫So here you got a node that obviously has side effects because it's going to reload the level and it
‫has an input, which is the level name here.
‫Now how do we get hold of the current level name?
‫We could just type it in, but what if we accidentally renamed this level at some point in the future?
‫Well, what we can do is also right click and do a get current level name, which gets us another node
‫that returns the current level name.
‫Interestingly enough, this one isn't a pure function.
‫I have no idea why.
‫Because it looks like it should be.
‫And what we can do is even though these two types are different, one of them returns a string and the
‫other one requires something of type name.
‫It's actually possible to very easily convert between strings and names so we can drag from the return
‫value to the input value, and a conversion node will automatically be created for us.
‫We also need to hook up the execution pin between these two, so we're going to get the current level
‫name convert and then open the level.
‫Now when is it that we want to do this?
‫Currently everything is happening either on a key event or begin play, but there is another option.
‫What we can do is we can have a special node in the root event graph, so not within functions but within
‫the root event graph called a delay node.
‫So what we're going to do is after the out of ammo is pressed, we're going to pull off of the execution
‫pin here and search for delay.
‫And then you can see it brings in a special kind of node with a clock on it.
‫And this means that the output happens not instantly after the function has finished, but it happens
‫after a certain amount of time.
‫So you can see we can input the duration here, and that duration could be 5 seconds, for example.
‫So in my print string, I'm going to update it to say out of ammo restarting in 5 seconds and then dot,
‫dot, dot.
‫And then we can plug in the code that restarts the level into the output, the completed node of the
‫delay node.
‫So if we go ahead and play now, what we're going to see is that I can run out of ammo, finish all
‫of my ammo, I run out and it says I'm restarting in 5 seconds and after 5 seconds has elapsed, boom,
‫the level has just restarted.
‫So small challenge for this lecture is to extract a function from those nodes that we've just created,
‫the get current level, name and open level.
‫And my challenge to you is to just think of a good name for this function.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫Well, I'm going to call it restart level because that's what I think it's doing.
‫So I'm going to right click collapse to function and call it restart level and it's fine with the current
‫inputs.
‫It doesn't need to be pure because it has the side effect of restarting the level.
‫So we wouldn't want it to be pure anyway.
‫So that's it.
‫I'm just going to reorganize my nodes ever so slightly and delete my test node and delete my test function
‫by right clicking on it in the my blueprints and hitting delete.
‫And we've got ourselves a condensed little blueprint graph that actually describes exactly what our
‫game can do just at a very high level.
‫So I'm pleased with the results of that.
‫Feels like we've got a good game loop and I'll see you in the next lecture.