[Godot Tutorial] Basic Breakout! [P3] Instancing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to another Godot tutorial this is part three to our dye bricks brick break or breakout whatever you want to call it a series and we're going to get started today we're going to learn how to control the direction that the ball is going using code so that when if you remember where we left off I'm going to run the project here real quick so you can see when the ball hits the paddle there's no way to control where it goes so it doesn't really give the game the game is not very fun because all we can do is just make sure that we keep the ball up and then pray that it goes and hits the bricks and if we don't keep the ball up then nothing really happens right now so anyways let's get started at that if you click on your paddle node right here and click on the script I con it will open up the script for our pattern paddle node now what we need to do inside of here is we need to detect actually we're going to do this inside of the ball so open up the ball node and I'm going to open it up here in my example project and we don't have a lot going on inside of here but we're going to change this so that we can detect a collision with the paddle and it was a loud truck holy cow okay so we're going to add in our fixed process right here where we're detecting collisions with the bodies what we need to do is detect if we collide with the paddle because we're already doing with the bricks so press Enter but make sure you're lined up with the first if statement right here and we're going to do if body don't get name equals paddle now with the bricks we used a group but since there's only one paddle we'll just get its name and this name right here she match up with the name of the node right here so there we go now we're going to say var okay so there's a few things we need to do here first we need to create some constants now a constant is just a variable that doesn't change so it's not a variable it's a constant but it just contains a value and never changes so we're going to create two constants we're going to create one Const speed-up equals 4 and Const max speed equals 300 now these are just variables or constants values that I have found to work very well for this so that the ball increases its speed as we continue to every time it hits something and also so that we can limit the maximum speed to prevent clipping so the ball doesn't clip outside the room and stuff like that so we're going to say first thing we need to do when we collide at the paddle is we need to get our current speed so VAR speed equals get linear velocity dot length okay that gets us the current speed linear velocity returns a vector 2 now if you remember a vector 2 contains two coordinates in it which alts also gives us a direction and that also gives us a magnitude so if the vector 2 was I I think it's I think three four five is a triangle so if we have an x coordinate of 3 and then a y coordinate of 4 then our length which is going to be the which is going to be kind of the distance from the origin to the point is going to be 5 and so we're getting the length right here and this is just a a single number it's not a vector anymore so this gets a vector but then we call dot length get us the number which is the total speed the distance basically from the origin of this vector to to the vector so anyways this gets the speed now what we're going to do is get the direction so far Direction equals and this is actually this is the direction from our paddle node to the ball but what we're going to need to do here and we're going to leave this the way it is we're going to add an anchor to the paddle so come here click on the paddle node and we're going to add a new node to our paddle and we're going to call it anchor so add a new node and this is just going to be a point position yeah position 2d node and you can see that just creates a node here we're going to drag this node oh it wants to snap not how I want it to so undo that so it puts it right back there and then just change its position from 0 0 to 0 X 32 yeah yeah Izzy row 32 so what this does is it gives us a point just a reference below the paddle right here so that we can determine the direction we want the ball to go so for example if the ball hits right here we can go from this point to the ball's position and we can determine that we want the ball to go straight up in the direction that this points towards the ball however if the ball hits clear over here on the corner we're going to the direction that we're going to get is going to be from this point right here to the ball's position over here which will point this direction and so that's the direction that the ball will that the ball will bounce off heading so no matter which way it's going when it hits this corner it's going to hit go in this direction afterwards so you could just use you don't actually need to use a point like this but I thought it was cool to show you guys how you could use something like this to help it to help it be a little bit more visual and so there are lots of different uses uses for points like this references that you can create to a position and so I thought that would be cool so come back into the ball and inside of here we're going to use that point that we just created so the direction is going to be get position so we're getting the balls positioned right and then we're going to do - body and which body right here in this context refers to the paddle right so that's the paddle body get node now we're going to get the anchor except I didn't rename it so come into the paddle it's just called position 2d right now I'm going to name it anchor okay so we rename this node anchor so come back into the ball so body dot get node anchor dot get global position so that will get the global position of the anchor and I actually add them either so that's not right okay there we go now what this will do is when you take one vector and subtract another vector which is what this is going to return it's going to get you a vector which is the difference between the two of them and so because of that this will get us to our direction will actually be a vector that is that we can use as our direction so that's kind of cool now what we're going to do is we're going to get the velocity so we're going to save our velocity equals so we're actually not getting it we're creating the velocity so we're going to use the speed that we had which is basically just it's the it's the it's the distance for this vector right here the magnitude that's the word I was looking for it's the magnitude for this vector which is the distance and then the direction which is the direction that we want our velocity to point in and we're going to combine those to create a new vector so this is a lot of vector math and I really think that I'm going to be doing a tutorial video on vector math and how it works because it's really important if you're going to go into game design so I'm going to do tutorial video I don't have this right now but maybe if you're watching this video a little later I'll link it in the description or in this video also I'm going to link in the description an article that is pretty good talking about how vector math works and so I highly recommend reading that as well but we'll go through this a little bit at a time you can follow along and then maybe afterwards read that article it will help so the velocity is going to be Direction dot normalize normalized so what this does is it takes the direction which is this vector and it turns it into a unit vector I'm throwing all these new things at you so a unit vector is basically it's the same direction but the the length of the vector gets shortened down to one so it's pointing in the same direction that it was before but it's shortened down to 1 instead of however long it was and obviously that's what we want to do because we want to get the direction but we don't want the length anymore we're going to use this length right here so that gets us the normalized one and then we're going to times that by minn speed which is this variable right here Plus speed up which is the constant we created here so that adds to the speed times delta so we're timesing by the Delta to make sure that we get a normal speed and then we're going to do comma max speed times Delta so what this does is it multiplies the direction vector by a new speed and that speed is either going to be the maximum speed times the Delta or the speed that we currently had before plus the speed-up times the Delta that way we can't go past the maximum speed this min function right here but we'll always select the maximum speed if it's lower than this speeds so now that we've got our new velocity we can set it we can just do set linear velocity and just pass in velocity and we should be good so if we're colliding with the paddle we get that we get our previous speed then we get the direction and then we create a new velocity which is a vector using that speed in that direction and then we set that right here so let's test this and make sure it works should be able to control where the ball hits and that's going really really slow so we've messed up something okay I figured out what it was so sometimes in order to prevent these values from being huge values I like to multiply my delta by a hundred now I'm not sure this is exactly the best way to do this you could just multiply these values by hundred so make this 400 and make this thirty thousand I think you could do that instead it seems a little ridiculous to have values that large which is why I just multiplied the Delta by 100 but you can see that gives us the correct kind of speed that we're looking for and gives us the ability to control the direction the ball is going based on where it hits the paddle so you can choose what you want to do you can change these values or you can multiply the Delta by 100 I guess for now I'm just going to change these values multiplied them by 100 and that should give us a decent speed when the ball collides with the paddle so awesome job now what we want to do we want to make it so that the ball because right now if it goes outside the room it's not going to get destroyed and we want to change that because if the ball goes on forever outside the room it could be a bad thing for the game there's no reason to keep computing that when the ball is not even in the game anymore so what we can do is we can come into the fixed process here make sure you tab over only one so you should be lined up with this for loop and we're going to check the ball and see if it's outside the room so we're going to say if get position dot y so if our Y position is greater than get viewport rect dot end dot Y that should get us the bottom of the view then we're going to do Q free and that will just destroy the ball if it goes outside the room so now if we drop the ball and it goes outside the room it should destroy it you can test that by doing a print here so print ball died run it there we go you can see the ball died right as it goes past the bottom of the viewport but there's actually no way to create a new ball so let's learn about that so inside of Godot there's a really neat thing so you can use a scene like we've been doing as a level but you can also use a scene to a seam can be basically any size any collection of nodes it doesn't matter how big or small it is and you actually want to group your game into scenes and this is one of the greatest things about Gowda is the scene system it works I think it's just an amazing way to set up game logic and to set up the structure of the game so we have this scene set up that's our level but we can create a new scene that will be the ball so let me show you how we can do that so save what we have so far I'm just going to do ctrl s and click on the ball and down here at the bottom there's this little kind of movie clip thing this right here when we click this it will create a scene of this node and I'm going to show you what that means okay so we're going to create a scene and then we're going to save this I'm going to create a new folder right here and just call it mini scene how you name this is up to you I just have scenes for scenes for levels and then many are scenes for levels game levels and then many scenes for kind of game objects that would be found inside of that level so another thing is some of the information that you guys have given me in the comments and from other people I've learned that dot XML actually isn't the best extension to use that TSC n is the best extension to use so I'm just going to use dot XML to stay consistent in this project but I would say that in your future projects you should be using TS CN and the reason you can correct me if I'm wrong but to my understanding the reason is that TS CN is also a human readable readable format just like XML but when you compile your project TS CN has the option to basically create a non human readable compressed version of the scene which makes it smaller so TS CN kind of gives you the best of both worlds you get the human readable format in your project but when you compile the game it makes it smaller in a computer computer readable format that makes it basically gives you makes your project smaller so like I said I'm going to use XML for the rest of this just to keep with just to keep consistent with what I started doing because I don't want a mixture of the two but in your future projects TS CN might be the option that you want to choose so we're going to name this ball dot XML and save it now that we've done that you can see we can come down here in our file system and we can go into mini scenes and we can open up ball to XML and it actually adds another tab up here at the top so we've got level one right here and we've got ball dot XML right here and you can see this is just our ball node with the sprite and the collision and the script attached to it so it's its own scene now there's nothing else in the scene now what's the benefit of that well the benefit of that is that we can now instance this scene and I'm going to show you how to do that so come into level one and we can delete the ball so we deleted the ball it's no longer in our level come into the paddle script and inside of here we need to get access to the ball scene so we're going to create a constant Const we're going to name it ball scene and we're going to set it equal to preload rez which we're just going to take this path right here to get to the ball scene so we're going to go res colon slash slash mini scenes space scenes slash ball dot XML and you can see that we shouldn't get any error message that means we have the correct path to access the ball scene now that we've done that we need to make sure that our we need to build a process input because we want to allow the player to left-click in order to create a new ball to instance this scene so we're going to in fact before we do that though actually let's comment this out pretend like you didn't do that we'll comment that out I want to show you how to instance the scene inside of another scene so we have the ball here we can come in to level one it's possible to instance the ball scene that we have now inside of this level and actually it would be a good idea to do this for the bricks so let's do that we'll do it with the bricks so come here and we're going to delete all these other bricks here that we've got set up now the reason is because if you remember when I changed when I wanted to change something on one of these bricks I had to change it on every single one because these are all copies that's kind of a mess right so let's delete all these delete those nodes and we'll take this brick right here and we'll make a scene out of it so create a scene and we're inside the mini scenes folder I'm just going to create this called brick one dot XML and save now that we've done that we can delete this brick okay we still have our bricks node right here so we're going to instance that brick scene that we just created so instead of clicking the new node icon right here this right here is an instance icon and it allows you to instance a scene so we have the bricks selected we click this icon and it has us choose this scene that we want to do come in two mini scenes and do brick one and there we go we just instanced this scene that we've created although it's not showing it in my mini scenes today it actually put it in the wrong spot you'd have to go into your file projects and move that but anyways you could do that you could move it into mini scenes anyways we've got the brick here and you can see it's got this little folder here which you can choose to do editable children and that just allows you to edit the children nodes on this on this scene because this is a scene now and generally if you edit the scene you'll actually want to open it up and edit the brick scene right here so we're going to set the bricks new location click on the brick here you can see its position is 320 by 64 we're just going to change this and hopefully you can see it let me double check that yeah it's just above my head okay now we're going to change it to 0-0 that just puts it up here at the top and we'll save that and close now you can see it edited this scene as well this instance for instance version now if we move this we can put it right where we want it and we can duplicate this scene as well don't know how many we want just enough I guess I do seven yeah I did seven will do one more so Kay let's uh let's move this and we'll duplicate it it gives us seven okay so I've got a little design here now if we want to change something in this brick right here or if we wanted we can we can edit these children right here but if we want to change something in all the bricks we can come into the scene and change this and it will edit all the bricks so that's how you can instance inside of the room just using this little button now I'm going to show you guys how to instance a node inside of code which is what we're originally doing so you can uncomment this and we get access to the ball scene which is in many scenes right there now we need to get access to to the input of our game of the mouse so we're going to set input let's see set process input to true so this allows us to process input similar to the way that set fixed process allows us to run a fixed process every single step every single physics step so as you can probably guess the function is called input and this function gets passed an event just like the fixed process gets passed to Delta so we're going to take that and once we have the input and we have the event we can check some things about this event this event is kind of like a little dictionary so it has properties inside of it so there's a type so we can say if event dot type equals input put event dot mouse button and event dot is pressed so what this does is it says if our event type is equal to the mouse button because there's lots of different event types and the mouse button is pressed so we only get it when it's pressed we're going to instance the ball so we're going to save our ball equals ball scene dot instance okay so what that does is it takes the scene right here and we instance it and we assign it to the ball variable once we've done that we can say ball dot set position and we want to set it equal to the position of the paddle but maybe up a little bit so we're going to do get position minus vector to 0 and 16 which will be up 16 pixels from the paddles current position now you might want to try and run the game right now and you'll see that when we click nothing actually happens why is that well the problem is you can't just instance a scene and then expect it to show up in the room you also have to add it to the scene tree which is this structure over here of nodes right we need to add it to that in order for it to show up so we need to do get tree dot get root which just gets the root node of the tree and then we needed to do dot add child fall now this will add it to the root node of the tree which is a base which is which is technically position our root node is position 0 0 in the world so that's why even though the ball scene is here in the middle of the room like this that it still works so it's a little weird to do it that way but but it works that way I'll show you how you could do it the other way as well so now when we save and we run the game when we click it should create a ball and we should be able to create a whole bunch now you can see the bricks aren't being destroyed anymore so let's figure out why the reason the bricks aren't being destroyed anymore my guess is that they're no longer in a group let's double check yeah so when we made this brick into a scene it realized the group information that it had before so we just need to do that again so add to group bricks close and then ctrl s make sure you save the scene now when you come into here all of these bricks should be in that group yep bricks and bricks yep so like I said you make one change on this scene and it changes all of the instances that you have of that scene now we can run the game and we should be able to destroy the bricks again okay this is looking really good you've got control of the ball you can destroy all the bricks you can create a whole bunch more balls just a little bit overpowered might want to limit this in some way but you've got a working game right here so good job and you've learned how to instance the nodes now one last thing I want to do we've got this scene positioned at 0 0 this is a smart way to set up your scenes it just makes things easier later this ball right here is not at 0-0 it's clear in the middle of our room so we're going to set it at 0-0 so come up go to its position and set the position to 0-0 thank you guys so much for watching if you enjoyed this video be sure and like favorite and subscribe leave a comment in the description I've got the next RPG video coming out soon so state for that thank you guys for watching and I will talk to you guys later
Info
Channel: HeartBeast
Views: 39,004
Rating: undefined out of 5
Keywords: Game Design, Game Development, Indie Game, Indie, Pixel Art, Tutorial, Video Games, Game Builder, Programming, Coding, Art, Godot Engine, Godot, Godot Game Engine, Godot Tutorial, Godot Game Engine Tutorial, Godot Engine Tutorial, Breakout Tutorial, Game Physics Tutorial, Godot Instancing
Id: Kb6JU00DDxk
Channel Id: undefined
Length: 31min 4sec (1864 seconds)
Published: Fri May 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.