Make a 3D Top Down Shooter with Godot - Part 3.3 Level Transitions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks let's jump in by the end of this video our game will load the next level after we kill all the enemies in all the waves so that we can kill more enemies in more waves yay i love how when i open my project now we get this blackness and you can see the fog until we zoom in closer to our level so let's go into our game script and if we're gonna want to be able to load multiple nav maps or levels we're gonna have to store those somewhere so let's start at the top of our game script and we'll export an array of packed scenes and we can call this var levels oh my gosh i'm calling things level again that's all right and then over here we're going to have an array size of 0. so far we have three levels so let's go down to our level directory and under our level generator where we have the generated levels and i'm just going to move these three levels oh that was cool into the levels array look that we even get a little preview while we're dragging it across the viewport okay now in our game script we can access these levels so we're not going to use an on ready variable anymore let's get rid of that and this the nav map isn't going to refer to the navigation node that's currently in the scene here in fact we're going to delete this one because we're going to load it in our script so that we can swap it out later and we'll probably want another variable that's going to hold the current level we're on so we'll say current level and just for fun why don't we export that so when we load up a level we can just just see if it works that we can load all three of those levels we should probably set this to a default of zero now we're going to have to create that node or we'll probably get tons of errors so in our ready method let's delete all this stuff and instead we'll create a new method called add nav map and under our physics process let's create that function so we're going to add nav map and in here pretty simple we'll get the scene from our levels array so remember levels here is going to be an array so which element in that array do we want well we're going to get the current level which is zero so we'll get the zeroth element in the array and that's actually a scene that's not a node so we're going to want to instance that scene into a node and then we'll add a child to our game okay uh well let's see what happens so in our seat now it's looking kind of bare because everything is being generated programmatically um in scripts but if we run it do we get a level showing up we and it breaks i was clearly trying to move here okay so it breaks in where does it break here we go on function timer timeout in our spawner script so we're actually in our spawner script here if i expand the scripts here we're in spawner script and that's because the spawner is using a variable called nav map which is no longer connected because how does it find the nav map oh look at that it's going to the parent and trying to find the navigation node which no longer exists necessarily when this node is ready because the game is loading this navigation node programmatically so let's remove this as an on ready var we'll just create a variable nav map and let's use a set get we'll create a setter and what the game script can do is set the nav map for the spawner once it's been created so then i like having my setters and getters at the top here we can create a new function for set the nav map we'll pass a new nav map and this will be helpful because we're going to want to reset this every time we change the level because of course they're going to have different navigation meshes that the enemies are going to have to navigate around new map let's say newton app now and we'll just go nav map equals new nav map and now we're just gonna have to use this we'll just have to set the nav map in the game so we'll go here do we have a reference to the spawner yet doesn't look like it but if we're only using it once here we're going to add the nav map we'll add it as a child and then we'll just get the spawner directly it's one of the few things left actually in our tree in our scene when we start so we'll go spawner and then we should be able to get set nav map hopefully there it is and we'll give it that new nav map let's clear this stuff clear these errors and let's run this and see what happens okay so do we crash when and works all right okay so we have level one loading up so now somehow we need to know when the player has killed all the enemies so that we can load up level two so here we're probably going to want to go to the spawner node and down here in our start next wave so currently we check and make sure we haven't gone over uh the number of waves but when it's done it doesn't do anything else the game just stops right so now let's add an else condition here and this is basically means the level is complete anything in here so what's going to happen when the level's complete well we actually want the game to do some things right we want it to destroy the current level like q free it and then we want it to load up the next level in our levels array so what we'll do is we'll have the spawner emit a signal that the game can read so let's we'll create a new signal here at the top let's go signal and let's just call it level complete and then down here we'll emit that signal beauty and an easy shortcut to get that get a method into our game is in our spawner over here in the node oops there we go we our custom level complete signal should appear and we can connect that to our game script and it's going to be a function on spawner level complete perfect and there it is we are back on our game script there and now we can change the level so why don't we since this is a signal receiver here let's just create a new function called new level and anything we need to need to do we can do in there which we can just create right above it but it'll help later on if there's some other reason we want to create new levels we'll just have a one method we can call okay so a few things obviously we're going to want to add one to our current level we'll delete our current nav map should be able to q free that that's not auto completing for us because it has no idea what a nav map is so why don't we tell it it is a navigation map oops give that the type hint just to make sure cue free there we go and now i think we can just call add nav map the method we created earlier because it's going to regenerate that map using the new current level and it'll add it to the scene tree has a child of our game it'll update the spawner telling it it's got a new nav map so that any new enemies that are spawned will be using that maps mesh what's going to happen let's give it a try here okay well why don't we just tweak these waves a little bit so in wave one i'm just gonna make it one enemy and wave two i'm gonna make it uh sure three enemies is that our default and we'll do half a second so this can go really fast so each level will have two waves it'll go really fast and we can see how it goes so the way enemies always spawn in the same location so we should probably like randomize the seed set new seeds oh that worked it changed levels no new enemies okay clearly we got some work to do in our spawner script so let's go to the spawner script here actually let's go to the game script and when we add a new level after we add the nav map we'll want to do a couple things actually we need to move the player back to the middle because the new level might have an object or an obstacle where a planet currently is and we don't want him to get stuck in there or go flying because of some weird collision that happens and then we're also going to reset the spawner so let's start with this one and i'm just going to go we already have a reference to the player here yes player so let's go player reset position this is a function that doesn't exist yet so we'll go and create that on the player in the player script we'll do it between the physics process and the signals so we're going to reset the position that should be pretty easy we'll just go get the global transform dot origin and we'll set its position to vector three we'll go zero on the x zero on the z and then for the y let's do it something like three so we have gravity now so it'll appear at zero zero on our map but a little bit in the air and then kind of like fall down back to the ground uh i'm curious if that's a bit too high maybe if the player's moving around he might be able to somehow end up on top of an obstacle that would that would be neat okay so is the player going to reset let's just make sure that works cool here i was basically standing there but we saw him fall so that's running okay um so the spawner now so let's go back to the game script and we'll kind of do the same thing we'll just go we don't have a variable for the spawner so we'll just go spawner dot reset sure and let's go create that method it doesn't exist yet so in our spawner script we'll do it underneath start next wave and we'll have a reset method so what are the things we need to reset well we'll need to reset the current wave number and if you remember we started the current wave number at negative one because every time you start the next wave it's going to increment it don't totally remember why we did that it's going to increment it by one so it'll actually start at zero but just remember that we're gonna reset it to negative one okay and then i think after that we can just call start next wave and it will do everything we want for us so let's try that um okay you guys get bored of watching me test everything out i should just give them all good good i fell back down we got an enemy spawn and we had an error okay okay so we're slowly making progress uh there's a problem with the path because get simple path on base null instance so nav doesn't exist nav is null so we need to tell the enemies how to find the levels navigation mesh and stuff right so i think that's probably going to be when we spawn an enemy so let's go back to the spawner and where do we spawn an enemy oh uh we don't have a method yet it's still all in this uh signal um so i think i'm going to take this all out because i don't like having all this code in the signals so let's take all this out i'm going to control x that and i'm just going to call uh spawn enemy method that doesn't exist which i will create here i'm just going to create it above all the signal stuff function spawn enemy paste all my code and then go shift tab to pull that in okay so i didn't change anything there we're just so if there's enemies remaining to spawn we'll spawn them and do the other stuff okay so now in our spawn enemy script we instantiate an enemy node we give it a location uh so maybe after we give it a location we're gonna have to set its nav map so how do enemies now let's go let's go to an enemy here let's go to our enemy scene let me go to the enemy script and how does it get the nav map oh it's doing the same thing our spawner was doing it's just going out directly and just just getting it right so we're gonna have to clean we'll fix that up too so now nav is gonna be we can be a little more precise there nav map let's be consistent nav map we'll do the same thing we'll create a setter called set nav map and then here at the very top new nav map nav map equals new colon okay i think that's all we needed to do oh i changed the name so i'm right here in our update path method let's just change that to nav map so we're consistent across our code about what we're referring to here okay i think we can close the enemy because now in our spawner script in the spawn enemy we can go enemy dot update up date nav map why why don't you understand that um i don't like that why are you not recognizing it's called nav map oh it's called set now map spawner script got set dot set what okay um let's stop stuff that's happening in clearer errors okay and i'll minimize that a little bit now this time enemy dot set nav map beauty oh uh and we have a nav map here yes so it will already have been set because it the nav map is set as soon as the game loads a new level and then you know after a bit of time the first enemy will spawn it will get the nav map set before it's even added to the scene and it works we'll find out when did the error happen oh good good um yeah okay it worked because the air didn't happen because not sure why but right now playing this level i just really noticed the smoother movement we we did last time okay so we got one final error to deal with and that's going to be this when we finish the last level so we'll have to add something to oh look at that right here our new level so before we do any of this stuff we only want to do that if there are levels left to be added so we'll go if current level is less than levels dot size levels is our array then do this stuff and if it's not then you win there we go why don't we see if this works in my game i'm gonna set the current level to two and just see if we start at the last level that's the big one that's it and let's see if we can win this game i almost didn't you win nice job
Info
Channel: Age of Asparagus
Views: 325
Rating: undefined out of 5
Keywords: AgeOfAsparagus
Id: YfLOCkzzq0M
Channel Id: undefined
Length: 19min 19sec (1159 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.