Godot 3.2: Let's Build a 2D Platformer!: Part 10 (Coins! + Collision Layers & Masks)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Will check out ASAP!

👍︎︎ 1 👤︎︎ u/VRbandwagon 📅︎︎ Jun 23 2020 🗫︎ replies
Captions
hello and welcome arctan let's create a 2d platformer in the cadeaux game engine my name is Colin and in this tutorial not so many series we'll be creating this 2d platformer video game of course in this game you control the player on screen you can walk run jump and fall you can squash enemies get hurt by enemies and lose lives collect coins shoot fireballs find keys to unlock doors you know how to deep platformer games work so at this point in our project we have a character that can walk run jump and fall he can explore a large level the camera follows us around that level and as of the last video in this mini series our player can now fall off a cliff into the abyss and the game will switch scenes in fact it'll just reload our game scene so you'll restart the level if you fall out of a level off the bottom of the screen if you've not seen that video I'll put a link to that video part 9 in this mini series up on the screen right now but in this video we're gonna start adding coins into our game so in this video we're gonna make a coin object that we can create instances of and as you hopefully know at this point in Godot when you make a new object you want to reuse you make that object out of its own new scene that you create instances of but also in this video we're going to talk about a bigger topic called collision layers and collision masks now these are too not so flashy things that are a big part of Godot but don't skip this part of this mini series because implementing collision layers and collision masks keeps your program organized it prevents objects from being able to collide with one another when they're not supposed to and that will save you from lots of headaches as you're designing this video game and video games in the future so in this video we're gonna start adding coins into our game and when you collect a coin it will disappear out of a level and we'll talk about how to manage all those collisions all the questions in your game in fact using collision layers and collision masks by the way if you've not seen the rest of this whole mini series and how to create a 2d platformer in the Godot game engine this is a part but there have been nine parts so far if you've not seen any of them I'll put a link to this whole playlist with all my katoa three tutorials up on the screen right now go ahead and check that playlist out if you're missing any of the previous parts but let's go ahead and jump into the Godot game engine of course if you like this video or a little something in it please go ahead and click on that like bubbles video it really helps me out and I really appreciate it of course if you want to see more videos like this one in the Godot game engine or in blender or other technology click on that subscribe button as well and click the bell icon to be notified whenever I upload a new tutorial so this is our project as it stands we have our little character if I press the play seam button of course arcane loads and I can control my player moving around my large I was gonna say little my large world and it's wonderful except there's not a whole lot to do so let's go ahead and add coins all around our world of course we're gonna start by creating a new scene we'll get to that in just a moment but we need sprites for our animated coin yes it's gonna be animated so it's gonna be at least a few frames of it spinning in place I'll be giving you these resources I have a coin folder on my desktop these sprites gold one two and three different frames or a rotating coin of course we're made by triple W Kenny dot NL they are not a sponsor of this video I just appreciate the game assets that they make I think that they're all really of high quality so go ahead and check them out if you like but I will be giving you a link to download this coin folder with these three sprites in the description area below this video on YouTube of course for free so you can download that unzip it and I'm going to now copy this folder right-click and copy and I'm gonna go into my project folder I'll double click and I have an assets folder just to keep things organized and in there I'm going to right-click and paste and now I've got my coin folder there with my three sprites so back into the cadeau editor it will very quickly import that folder with those sprites and now over here in my file system dock I've got them right here and now I'm going to create a new scene that is my coin object that we can later create instances of so to create a new scene of course you press a little plus button up on the top to create a new tab it's an empty scene we're not going to add a new note of one of these types because our coin is gonna be an area now you hopefully recognize that word an area it's actually an area 2d because in the last video in this miniseries we add an area 2d as this fall zone below our whole game so that when our player enters this kind of force field zone area it can detect the player coming in to it and if it does then it emits a signal which you can connect to a function that will do pretty much anything you program it to do in your game a coin is gonna be very similar a coin is not gonna be a physics object a coin doesn't need to be bumped into a coin doesn't get pushed around or fall and tumble and bump and fall into other things it's just an area a zone that can detect if the player enters in so let's go ahead and with our new empty scene and press the plus button to add a new note I'm gonna add an area to D so I'll search for area and select area to D there we go that's the root note of the scene I'll rename it to double-click to coin and I'll press ENTER and now this root coin area to D node needs both a collision shape to know what shape and size it is and it needs an enemy it's sprite so I'm gonna select that root node press plus I'm gonna give it a collision shape to D not a collision polygon 2d collision shape tree you might already have that in your recents so I'll double click on question shape to D of course it needs to know a shape that's why it has a little error here we'll worry about that in a moment but I'm gonna add I'll select the root node and add an animated sprite of course the blue 2d version ok so animated sprite right there so those are now both children of the root 2d era 2d known okay so our animated sprite has an error and that's because if I select it it does not have a sprite frames or resource over here so you might recall that in our main game our player our Steve character has an animated sprite as well this is how we created his idle pose or animation his walk pose his climbing pose his in the air kind of jumping pose it's the same thing so our coin it's animated sprite we need to click on this little arrow with it selected and we're gonna make a new sprite frames Godot resource and so there it is if I click on it it will open up this bright frames dock at the bottom of Godot and it gives us a default animation that we can drag sprites into this area I'm gonna double click on this default name and I'm gonna call it spin because our coin is gonna be spinning and so now I can drag from our assets folder in our file system dock gold 1 gold 2 and go 3 in that order it's not a very complex animation it only has 3 frames the spinning isn't that smooth it's okay for this project if I select the enemy it's right over on my seam dock I should enable playing I'll turn playing on so now even in the editor you can see what it looks like if you want to change the speed of this animation from 5 to something higher you can do that I think 5 is okay maybe 6 or 7 is okay that's really up to you and so now I'm gonna give this coin area 2d object its collision shape don't forget that if I select collision shape 2d I'm gonna give it a new shape resource it's gonna be a circle shape and because I added it first it is behind the sprite so if I drag this little red pinky handle out you can see its size and I don't mind making it a little bit bigger than the enemy it's bright and you know what that enemy of sprite is going to quick I think 5 was actually a nicer number okay so this looks pretty good to me before we save this coin seeing number it's unsaved we're gonna add a script to the coin the root node itself the area 2d so I'll select it and press this little script button and it's gonna call the script coin Gd the same name as my root note that's fine I don't want it to have of its template as the default I'm gonna change it to no comments okay that'll make the code cleaner and you'll see that in just a sec I'm gonna press create with coins Gd that's fine that's its name here it is it's much cleaner than the default template there's no extra lines of comments it's just very clean in fact you can get rid of the function ready you don't need done okay but keep that top line of course I'm gonna do a ctrl s to save this will prompt me to save the scene itself the scene will be called coin dot PSD and that's a cadeau scene and I'll press save and so now if I go back from my coin scene with nothing else in it except for the coin go back to level one I'm gonna create an instance of the coin so you're making instances basically copies that refer to the original coin object into your level one to do that I'm gonna select my level one root node and I'm gonna press not the plus number I'm gonna select and press this little link button this is actually called the instance a scene file has a node it creates an inherited scene if no root node exists so I'm gonna press this button and it'll show me all the scenes in my project I'll select coin ASEM and press open when I do that it adds a coin into my game exactly the same as the coin scene although because this coin is an instance of the scene if I update my scene later of course if I change things in my coin which I will it will propagate those changes out to all the coins that I make instances of in my project which is really nice so now if I select my coin instance and I place it somewhere in my level I'm using just my arrow tool of course and I go and play my level one and I walk over and I jump to collect it well nothing happens because we haven't programmed the claim to detect if the player enters it and I want to point out the way that I said that the coin hasn't detected if the player enters it that's the way we're gonna be doing things we're not going to program our character to detect if it's collecting a coin which might seem like the obvious thing to do that's not what we're gonna do we're gonna program or code the coin detect if it detects a character our player entering it and if the coin detects the player entering it it's going to delete itself it'll vanish from the game to make it look like it's being collected so to do this and by the way this is exactly the same pretty much as our fall zone where the fall zone has the code including a signal to detect if the player enters it same thing here I'm not going to edit this instance of the coin I'm not gonna select the coin and go in the in the level one I'm not gonna select this instance and go over and start working with its signals because this would change the signals just on this instance of the coin I'm gonna go back to my coin scene this is very important and in the coin original scene I'm gonna select its root node and now I can go over to the node dock the node tab up on the top right and start working with my signals so this is gonna be almost identical to that Falls oh no we're going to enable our root coin area to DS signal called body entered because this will detect if a physics body like our character which is a kinematic body physics object if a physics body enters the coin it will emit a signal which will be handled by a function a block of code that will run whenever this signal is triggered so I'm going to double click on body entered with the coin root nodes selected I'll double click this window will pop up and it's asking me to pick an object that has a script a node that has a script that I'm going to put that function on that will handle whenever this event the signal is emitted so I'm gonna select the coin itself because the coin is going to delete itself and so it makes sense to put the code on it so I'll select the coin the root node and it's gonna make a function called on coin body entered and so I'll press connect I I'm okay with that name I'll press connect it jumps us over into our script workspace of course I can go back to 2d if I want this script is called coin GD and it had nothing in an accept or that top line a few minutes ago except now it has this function called on coin body entered which we just make by linking be these signal up to it this little icon means that a signal is pointing to this function is it will run this function whenever that signal is emitted and if I go back to the coin I'll click on it and I go back to it's no dock you can see that same little icon is pointing to the name of that function and because there's only a little dot little period here that means that it's pointing to this same object a script so it's the script here ok all this script needs to do or all this function needs to do is at least right now delete the coin itself how do you delete an instance of a coin or an object or a node in your game it's really simple the method call to delete an object itself is called Q free and this is a method call so if I double click I started typing and knew what I was getting at so if I double click on that and it's a method call so it needs round brackets if you're not aware a Q is a line up means you're waiting for something and free in this case means to be free from memory to be freed from your games RAM on your computer your active running memory so this method will line up this object to be freed from memory when it is ready to be free sometimes objects have to wait like one cycle of your game loop or something like that to be free that's what this does but essentially it'll just delete the object right away that's all we need if I do a ctrl s on my keyboard to save and I go back to level 1 if I go back to my 2d workspace and select the coin you will notice that this instance of the coin now has that same signal and this instance of the coins script also now has that we didn't edit this instance we edited the original coin scene and those changes propagated out into my instance of my coin so now if I play this scene and I go over and collect the coin the coin disappears hey it works great but we're not done yet because our character is just 1 possible physics body that might come into contact with our coin in a few videos from now or we're gonna be adding enemies that enemies are gonna be walking along the ground and we don't want our enemies to collect coins so this is what we're gonna get into collision layers and collision masks if I select any object in my game or in my scene dock any node that is an object that can collide with any other object like Steve or my coin and I go over to the inspector with that nodes selected there is a section called collision and in this section there are a bunch of squares and if you're familiar with older versions of blender like blender 2.79 and earlier you'll recognize these squares this grid as layers now these are not visual layers these will not change what kind of layers like in Photoshop or that your item is visible on no this is a way of organizing objects into layer so you can separate out what groups of objects defined by their layer can collide with other objects now in cadeau these layers are visual although you can name them as well if I click on these two little dots next to this grid you can see there's layer names or layer 1 2 3 we can actually name these to make more sense out of it all so I'm gonna go up to my project settings to the project menu project settings and this is a big confusing area but under the first general tab if I scroll all the way down to layer names that section there and I'm working in 2d and I'm interested in 2d physics layers here we have layer 1 and a space to name all these later there one there too etc so what we're gonna be doing here is separating out objects onto different physics layer so that we can make sure that some types of objects can't collide with other types of objects and some can so my first layer I'm gonna dedicate it to my player seems simple there's only gonna be one object on this layer at any one time but it's important so I'm gonna name this first layer player and my second layer platform and I'm gonna keep going my third layer is going to be can you guess it's gonna be fall zone because we don't want any object to be detected by the fall zone so we'll put the fall zone on its own layer let's keep going I'm going to name the next layer item and then the next layer enemy so now we've named our first five layers we have lots more that we could name and we will use more of these in the future I think the next layer I will name a fireball because when we add fireball shooting into our game those should be those fireballs to be on their own layer so here we go we have our layers name if you need to pause the video please do get those down the same as me I suggest to keep yourself sane and I will press close now we have the job of separating out our objects into those different layers and again this is important so don't skip it because as I pointed out in the last video in this miniseries you know if you were to have other objects in your game like maybe you had barrels that were rolling like a mule classic Donkey Kong game and they were physics objects maybe they were tumbling in your game and maybe a barrel happen to come into contact with your falls own well the fall zone right now just you know whatever an a physics body enters it it restarts your game and you don't obviously want if you were to add those barrels you don't want the game to restart whenever a barrel gets to be fall zone okay so it's important to separate out your different objects into different layers what we're gonna do is we're going to select each object that can collide so I'll select my coin and I'll press these two little dots under collision in the inspector and it should now have these names if these don't load right away and you have named your layers up in your project settings you might just need to switch to a different node and then switch back and then they should be named so this coin that I have selected is going to be an item but this coin is an instance of a scene so rather than actually you know specify the collision layer on this instance I'm gonna go back to my coin scene and I'm gonna select the root node and I'm gonna go to collision and layer we're gonna select item and I'm gonna uncheck player so now you can see if I click somewhere else that this item is on this collision layer layer four if you're counting one two three and four so if I go back to my level one scene I'll go back to the coin and do a ctrl s to save if I go back to level one and select that coin hopefully now that change is here I'm gonna select Steve and I'm gonna go to Steve's inspector and his collision and I'm gonna make sure he is on the player collision layer that works he's on layer 1 let's go ahead now and edit the collision layers of our tiles we've got tiles one way and tiles sawed so I'll select each one of those go to collision will select the platform layer and uncheck player for each one of those so that one is now right I'll select tile solid go to collision layer and uncheck player and go to platform that's what I want the parallax background and all of its layers they are not collidable objects they're just in the background so I can collapse that whole branch of my tree and I don't need to worry about it it doesn't collide but my Falls own it does collide so I'll select it and then go over to collision and I will select the falls on layer and uncheck player so now hopefully if I double check the coin is on the item layer that's what I want and so now all the items are separated onto their own layers what does this mean for our gameplay well if I press the play scene button what happens well my player falls and lands and this is working you might think well why is my player now colliding with the floor if they're on separate layers aren't different layers both to isolate themselves from one mother yes but there is another piece to this puzzle if I close my playable game there is the collision mask and if I hover over the description of mask it says the physics layers area this scans for collisions this is not a very clear definition of what masks are and they are a little bit hard to understand essentially when you enable one of the layers in the collision mask section of an object you are telling that object what other layers this object can cool with now I have my Steve and note selected right now and as you can see Steve is on layer one the player collision layer and Steve right now only knows how to or only will detect other objects on his same layer but if I go and select the tile solid node the tile solid are on layer two but they know how to see all these tiles that have static body physics objects essentially they know how to detect or they will detect objects on the collision layer one because I have this enabled if I were to disable the layer one mask and then I were to press the play scene button well Steve would fall away through the platforms because the platforms don't detect Steve gliding with them so as you can see hopefully it gets a little bit confusing because you don't have to have things in two directions I don't have to have Steve be able to detect the platforms on the platform layer I just have to have the platforms be able to detect Steve or perhaps the other way around if I were to select Steve's node and then go over to his collision masks section and enable him to detect anything on the platform collision layer in fact I don't need Steve to detect anything on his own layer that's okay because there are no other objects on his layer and I were to play my scene would this work yes it would so let's go ahead and kind of organize things in terms of collision layers and collision masks I'm gonna start from the top in my scene my tiles one way well hopefully they are on the platform layer that's what I want what can platforms detect or what should they be able to detect they should detect anything that is a player and because enemies are also going to be in our game and they'll also fall with gravity I want the platforms to detect players and enemies and perhaps even fireballs as well because fireballs will bounce in our game the same layers or mask layer should be selected for my tiles sold of course so I'll select tile solid go over to mask it will detect players enemies and fireballs I think that's the same that I had before I will just double-check yes that's right but the player should be able to detect lots of things so I'm going to enable a platform that's right fall zone that's right if I don't have this enabled and I don't have the fall zone be able to detect items in the player layer well that wouldn't work the fall zone wouldn't cause the scene to reset like we want so that's important the player should definitely be able to detect things that are items and enemies and fireballs mm probably not so much because the player is gonna be shooting fireballs and so we'll leave that unselected so I think that's right the fall zone well the fall zone is going to be on the fall zone layer and definitely it should be able to detect players but again it should not detect enemies or items or other fall zones or platforms so that's what we want last but not least the coin but again we're not going to edit the collision layer and masks of this instance of the coin that is very important don't start editing these you'll welcome yourself to a whole bunch of frustration if you do only edit the original coin scenes collision layer and collision masks okay the coin is an item so it should be on the item layer although right now we actually don't need to even put the coin on any collision layer because no other object is actually going to be looking for the coin and so it might save your game a little bit of computer resources like CPU processes and RAM and things like that if you don't put this object on anything because it only needs to detect the player and no other object needs to detect it so it actually doesn't need to go on any layer but it does need to be able to detect the player and only the player and that's what we want I will go ahead and save my scene the coin scene I'll go back level one and save it as well let's go ahead and see if things work if I press play scene the game will load and I fall and I collide with the ground that's nice I can jump up through one-way platforms Mike Wayne was collected I can keep on wandering around and if I fall off the edge of the cliff nothing happens so something isn't quite right there I'm going to pause this video and I'll come back when I've solved it and the problem seemed to be that I had actually commented out the line of code that actually makes us change scenes when our player collides with the fall zone so if I go to Steve's script you can see in the function at the bottom called on falls own body entered I had added a pound sign or a hash tag to the beginning of our line of code that actually makes the scene change I don't know why I come into that out but I will bring that back and I'll get rid of this past line and I'll save I'll go back to my 2d workspace and I'll press the play scene button and now hopefully of course I can walk in collide with platforms I can collect coins and I can fall off the edge of my ground and my scene it will restart last but not least in this video we don't want just one coin we want lots of coins if I select my coin in my scene dock I can double click on it and name it coin one that'll make things easier and if I right-click on it I can say duplicate it'll make a copy and of course with the arrow tool I can move that coin around I'll put a few coins up on this upper platform so I'll duplicate this one and then move it over and as you can see it's really as easy as that if I press the play scene button now I've got all those coins in my level and I can collect them all we are not yet counting the coins and we'll handle that in the next video in this main series we'll also in that video make a little coin animation so the coin bounces and then after it finishes this little bounce animation then it'll disappear and we're going to enable us be able to count coins and if you collect a certain number that you can specify well then you'll win and we'll change scenes and we'll reload our game level so we'll cover those things in the next video but that will be it for this video of course if you like this video for enough to mean it please go ahead and click on that like Pablo's video it really helps out me and my channel if you want see more videos like this one in the Godot game engine or in blender or leather technology click on that subscribe button as well and click the bell icon to be notified whenever I upload a tutorial check out my Facebook page in my Instagram page in those two places I post sneak peeks and previews of what I'm working on next it's where I communicate with you guys the most but that'll wait for this video thanks for watching we'll see you the next one [Music] you
Info
Channel: BornCG
Views: 25,864
Rating: undefined out of 5
Keywords: lesson, beginner, godot, engine, game, sprite, collect, coin, collision, platform, platformer, learn, code, function, signal
Id: AyzMkCWaB1c
Channel Id: undefined
Length: 29min 33sec (1773 seconds)
Published: Sun Jun 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.