Godot 3.1: Creating a Simple 3D Game: Part 8 (Collecting Coins, HUD, Counter, Timers) #GodotEngine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Lots of interesting contents in these tutorials. Choices I probably wouldn’t make but still useful as they could be used in different situations. GDscript and Godot really give you a lot of different techniques to do what you want to do, it is part of its beauty and possibly why it gets so confusing for newcomers.

👍︎︎ 1 👤︎︎ u/minouTV 📅︎︎ Jul 27 2019 🗫︎ replies
Captions
hello and block of our eight accreting a simple moving dodge game using the Godot game engine in this miniseries we're creating this simple moving dodge game it's a game where you're a ball and you move around a 3d maze by rolling the ball around with arrow keys or WASD keys on your keyboard you collect coins you avoid enemies if you collect all the coins you win the game and if you hit an enemy you go to the game over screen if you've not seen the first seven videos in this series I'll put a link to my playlist this playlist on the screen right now but in this video we're going to be adding coins and coin counting into our game we are going to number one create a coin scene based on a model that we're taking from blender 2.8 and works porting it into Godot using a gltf file you've seen this many times in this mini series so far next up we're gonna create a heads up display or HUD that keeps track of our coins in other words one of those little coin counters at the top right of the screen that has a little number on it and it'll count up every time we collect a coin next up we're gonna make the coin spin and we're also gonna make it delete itself when it detects a collision with Steve our main ball character in our game and next we're actually going to make our counter be able to count up so at first we're gonna create it just a static version of the heads-up display and we're gonna program it to be able to count up and number five what would be a coin without it actually reacting to being collected so we're gonna animate the coin bouncing up and then falling back down again just like any game that you might have seen in the past when the coin is collected and number six finally or we're going to very quickly make a you win screen and we're gonna make it so that when you collect a certain number of coins and however many you have in your level and we'll duplicate those you win the game but of course if you liked this video over learn something go ahead and click on that like button below this video it really helps me and my channel out I really appreciate it if you want to see more videos like this one in Godot or blunder or technology click on that subscribe button and like on the bell icon to get notified whenever I upload a new tutorial so let's go ahead and jump in to Godot in the last video we made a game over screen along with our level and our menu because in our level we now have enemies and if I go ahead and play just this one scene you can see that my enemies I actually haven't created more of them since the last video my enemies do move around the level I think I have to make my second enemy actually roll because that's where I got to in the last video it doesn't actually roll right now it just slides but if I accidentally collided with an enemy I go to the game over screen and I can go back to the menu and of course play again so let's get off right into blender then an export our clean model of course I'm giving you in the description area of this video a link so you can download this blender 2.8 blender file of a coin and this is my client has a little crescent moon on it it's got a reflective material if I select the coin and then go to the materials tab I've got the material already named material - coin and I've got the roughness turned down on the coin so it is reflective so now with a coin selected I think this file actually does not have a camera or a lamp in the scene but I'll select it anyways I go up to file and export and gltf - you see them as many times before in this series so I'm gonna go through it quite quickly and on my desktop I'm gonna go into my Godot basic moving game moving dodge game folder and go into imports where all my other gltf files are and down on the bottom left I'm going to change my format to gltf embedded so not a binary file but an embedded and we're going to use selected objects even though this file only has one object the coin okay no camera or lamp so I'll go to export gltf on the top right and click on it and it'll take a sec to export into my project folder I also have another download for you that we're going to use in a few minutes I've got an image of the coin and that's for our heads-up display on the top right of the screen in our game so just a rendered out PNG image file of the coin these checker boards are transparent so right away I'm gonna right click on that file and say copy and then I'm gonna go into my project folder and I'm gonna make a new folder called images or sprites I'm gonna call it sprites and I'll go into that folder and I'll right-click and paste so now I've got that in my project folder and we can get into code oh it did some importing and so hopefully everything is there in my imports folder got coins gltf I'm gonna double click on that and I'm gonna say open anyways not the default you inherited but open anyway and there is my time you know coin and so I'm gonna save this scene right away as a Godot scene so scene save scene as not just save scene and I'm gonna go up out of my imports folder I'm going to press the up arrow and then go into where is it assets I think that's what I called it and this is where my scenes for my objects are gonna be so I've got enemy this scene is called coin though I'll click on save so now that it's a saved native Godot scene I can edit it ok this scene root of my scene should be an area a coin is not going to be a physics object it's just gonna have an area that we can detect if something like a physical physics body comes into it so I'm gonna right click on our scene root and say change type right there and I'm gonna change the type to under spatial and under collision object but not under physics body area okay and I'll click on change and so now my scene route is an area and it has a coin and I might call this coin mesh and I'm gonna call my actual root node coin there we go this coin has an error because it needs to have a religion shape in order to know what size of object it is so right away I'm gonna select my coin the root node and press the plus and I'm going to add a I'll just search for it coalition shape the pink one not the 2d one of course so double click on collision shape and the collision shape needs to know to know what shape it is over on the right-hand side with it selected I can go and make it have a cylinder shape and the cylinder is not rotate correctly and it's way too big so right away with my collision shape selected I'm gonna go to transform and we're gonna rotate it on the X the red axis this one here so I'm gonna say rotation on the X 90 and now I can go to my orthogonal view and my front view and if i zoom in on it I can drag the little handle and I'm gonna make the coin I'm just gonna eyeball it here right about its exact size as best as I can and then from another angle I'm gonna make it smaller if you want to change the size of the collision shape to make it easier to collect the coin you can that's totally up to you I might just leave it like that so it's a little bit easier to collect them okay uh I think we're pretty much good let's go ahead and save the scene ctrl s and I'm going to go to my level and I'm gonna select my root node because that's where we're gonna add not the coin necessarily but we're gonna add a folder for Cline's and to do that I'm just gonna select my root node and press the plus I'm just gonna add another I'll clear my search out a spatial node and essentially if you have a bunch of coins all lined up here why not have them in basically a folder or a group and so we can use a spatial as that so I'm gonna double click on spatial and now I've got a spatial as a child of my spatial that's a level our named level so this spatial I'll double click on it I'm gonna rename it coins and now I can create an instance of my claim so I'm gonna have claims selected and press the little link button and I'm gonna select assets coin T Sen and press open and my coin is now heating down below the ball so I'm going to move it up and move it over and it's okay if it's floating above the ground that's actually kind of what I want and so now I might move it somewhere in my game where it's very easy for me to test out I might move it oh I don't know up there okay and I'm just gonna do a quick test of this well I'm gonna test the C note with a little placing button and if it works correctly I can see it and I should not be able to do anything to it I should just go straight through it with my character okay so here we are and there's my enemy I don't want to hit the enemy but if I go through the coin nothing happens right now so we'll deal with that in a few moments but let's go ahead now and make a coin counter heads-up display how do we do that in a 3d level well it's really easy you just have to add a 2d node in other words a control node into your game and if it's a 3d scene it'll have a 2d scene floating on top of it it's really just that easy so if I select my level the root node of this 3d scene and I press plus and I double click on controller select control and press create I get jumped over into my 2d workspace mode and I've got this new heads-up display that I can work with this is a control node this thing that I'm dragging right now and I'm gonna make it the size of my game screen which is hard-coded to under the rectangle property of itself to 1280 wide that's pixels by 720 pixels tall so now in this control I can add a few things up on the top right corner I'm gonna add that picture of the coin I mean and a label with a letter that has little X 4 times I'm going to add a bigger label that has a number or in it in fact we're just going to give it a double so that we know that we are changing it programmatically in our game so really quick with our control nodes selected I'm gonna press the plus I'm gonna add under control we haven't seen this kind of note before it's going to be a sprite notice it under control or is it under no 2d it is called a sprite you know what I think you sit in a while so I'm just gonna search for sprite and it's a blue one this time it's called sprite there it is I'll double click and with sprite selected I'm gonna go over and give it a texture and that texture is just my image file so under my sprites folder in my project folder I can just drag this HUD underscore coin across with my sprite selected I can drag it across there it is to the texture and let go and now my sprites in my game and I can drag my sprite around that's totally ok I can drag the corner and if I hold shift I can scale it proportionally so let's make it about that size and I can just move it by dragging it to where I want it to be that looks pretty good to me I'm gonna to my control know what I'm gonna add a little label for an X so really quickly with control selected I'll press plus I'm gonna search for label and double click this label again it's just going to be I'll drag it and put it right there it's gonna have a little lowercase X and I'm gonna give it a custom font and really quick I'll check font create a new dynamic font go into it by clicking on it and go into font and now I can drag a font over let's try some time later let's make the setting of the font the font bigger let's try 35 and be like a bit bigger than that maybe 56 something like that and you know what I don't quite like that font so I'm gonna change it to that no-go font casual or red costs comic I like that one maybe I can make it a little bit smaller maybe for TV five and are maybe even 40 and I'm gonna change the color of it so under custom colors I can change the font color and I'm just gonna pick a yellowy gold color kind of like that to match I'm gonna make this smaller and so that's my little x times maybe I'll make it a little bit more gold well that's not be too picky for the sake of this video okay one more time I'm gonna create a label so I'm gonna select my control node press the plus we'll add a label and I'm gonna make this font a little bit bigger but this is gonna be the one that's actually gonna be programmed to count up so in my label I'm gonna put the letter Q and in fact I'm gonna make it a capital why am I using Q because I want to have Q in my editor but as soon as I launch this scene which is actually a 3d scene with the control over top of it I want it to not display Q I want display in the number that I programmed is not zero to be the starting value of this letter so I'm gonna go to a custom fonts with this label selected and make a new dynamic font resource and click on it and go to my font and I'm gonna try some time later and let's make it under settings let's make it a lot bigger that's making me be 80 and I don't like that font at all so I'm going to play around with the other ones let's try just red cost comic yeah that one's simple and I'm gonna make it a little bit bigger even maybe even 90 okay I kind of like that can I change my paragraph to be in fact it is left justified already so I don't have to worry about that I'm gonna change the color so I'm gonna collapse the the font and I'll collapse custom font and I'll go to a custom color and find font color and I'll give it the same Goldy color if I click on my original X and click on its color I can copy this hex code which is six digits long it has numbers and letters ABCDE and F and it defines the red green and blue values in my color so I can copy it and then go back to this letter go back to custom colors and just paste ctrl V that color and that that's where that way I can have it match so I might have more than ten coins in my game so I'm gonna move those things over and let's see how this looks I'll do a quick ctrl s to save and I'll play this scene and hopefully we'll see our HUD on top of our game aha so it works we've got the coin and a x and the letter Q and if I move around you know my camera is moving but this is staying static I'm just gonna wonder to myself here this control is at the bottom of my tree stack which means it's drawn last which means it's drawn in front of everything I wonder what happens if we drag this to the top of our tree so a child of level and I wonder what happens we'll still be on top I actually don't know the answer to that and it is so apparently if it's a control node in a 3d scene it does not matter where it goes but I'm just gonna put it down at the bottom and hopefully not a child of something wrong like I just did I'll try got back onto the level and there we go next up let's make the coin spin and let's make it W itself when it detects a collision with our Steve character so let's do a quick ctrl s to save and then go back into the 3d view so we can see our scene and my little coin is down here but if I select it in our scene dock in our tree so coin is right there I'm gonna have this coin have its own script so I'm gonna select it press the little add script button of course we're using the language GD script this is code or this object is an area so it's gonna use areas code it's gonna inherit that code template I'm gonna say no comments and I need to make sure I'm saving this script file as coin GD but not in the assets folder so I'll press the little folder but button and I'll go up one and I'll go into my scripts folder and this will be called coin dot GD so I'll press open I don't know why it's open but uh yeah and I'll press create now I can make this coin spin in a quite easy way I'm just gonna tell it always to increase its rotation on the y axis that means the green axis like the handle the hula hoop that's now white so it looks like it's spinning and we're not gonna have to use any kind of a physics way of doing that because this coin is an area it's not a physics object so what I can do is if I go back to its script is and I'll make this go a little bit bigger I'm going to add that same always game loop function it's called func underscore physics process so if I type underscore P it'll give me that hint and I can press ENTER this is the function that runs at 60 frames per second when your game is playing and it'll do any instructions you put under this line indented of course at that set that same 60 frames per second now we're not gonna tell our coin to rotate using physics we're just going to manipulate our area nodes properties under the inspector so if I go to the transform properties I can just affect its Y rotation and you can see I'm kind of jiggling it there and if I keep going well it's rotating in the right way but I'm going to reset that back to zero so I'm gonna go back to my coins script without little icon and I'm gonna press Enter and so we've indented once maybe I'll get rid of that extra empty line and I'm gonna say rotate underscore Y this is a method or a function that you can use to rotate an area on its own Y axis or I believe it's the global Y axis now this function or method does not use degrees it uses radians and radians aren't something that most people are familiar with until they get to about grade 10 math or so in high school so we're gonna use to grease instead so I'm gonna use a little converter a function called beg to rad and I might have used this before in a previous video I'm not sure dag 2 rad is a little function that you can put inside of an area where it's expecting a Radian and I can put inside its round brackets a degree amount and I'm gonna try 3 so 60 times per second it's gonna rotate the coin on the y-axis by 3 degrees and then convert that into radians and then it'll actually do the rotation so let's go and see if that works I'll do a ctrl s to save and play this scene and there we have it the little coin is rotating and it looks pretty good we want it to then disappear when it detects a collision with our good old friend Steve which is not doing right now one thing we don't want to do though is have the coin delete itself right away because we're gonna later part of this video add an animation of the coin kind of bouncing up and then down that will make it seem more interactive and fun so we're gonna put a timer into the coin and we're gonna set the timer to a very short amount like half of a second just enough time to play that little animation that we're gonna make later on ok hopefully that makes sense so I'm gonna go actually back into my coin scene and if I'm in my level I could also press on this little scene icon next to our coin and that jumps us back into our 3d coin scene or we have just a root node and a mesh node and a collision shaped node for the area root node under the root node of the coin I'm gonna select it and press plus I'm gonna add a new kind of a node it's called a timer and it's right here and a time where is one of the most handy notes in Godot because all the time you want to be able to have a timer that will count down or repeat and then once it gets down to zero or to whatever number you want you can tell it to do something and so we're gonna have it W itself once the timer runs out so I'm gonna press create with timers selected and I'm gonna leave it named timer and all I have to do now is with it selected is change its wait time and I'm gonna use the number zero point four I think is the number that I use when I practice this video so it's going to wait so your point four seconds and what is it gonna do when it it gets there well we're gonna go back to our our coins coat and we're going to deal with that so we're gonna go back to our level and we're gonna make our claim detect a collision with Steve and you've seen this before with our enemy in the case of our enemy we had the enemy itself which is an area just like our coin it's gonna detect using its own event handler signal if a physics body including kinematic body like our character or I can I quite like a character enters it so I'm gonna select my coin and I'm gonna go up to the node tab on the top right and there is under the area's abilities or it's common events there is one that we've seen before it's called body entered and this will emit a signal to be handled by some code when a physics body enters the area of this area so I'm double click on body and and when the body is interred of the coin I'm gonna have it run a function called on coin body entered that's the default on itself so I'm gonna click on the word coin here in the connect to no list of nodes in our scene and I'm just gonna press connect so click on that confirm that it's name this and then press connect and once it does that I can look at my coins code and there's a new function here called on coin body entered which will run when the coin is hit now we need to make sure that it's the right physics body by looking at this body's name so I'm gonna write an if statement on line 10 I'm gonna say if body dot name space equals equals because equals equals checks for equality and I'm gonna put in quotes Steve using this in the last video for enemies so if the body's name is Steve that means it's actually Steve that's hitting us or this coin and if that's the case I'm just gonna have it print to the screen this is how you test code you can do simple print so I'm gonna say collect and let's see if this actually work so I'm gonna press the play scene button make sure that your print line is indented with a tab otherwise the if statement will not work and you'll get an error so let's play this scene and okay we've got our coin rotating there and if I go ahead and touch it you can see on the on our output window on the bottom that it put collect twice so that means it's it's working if I press the stop button we want to do though is actually access our timer instead of pressing or printing a collect on the screen there's one little thing I need to change though if I go into my coin scene if so I go over here to the tab or just on my level click on the little scene icon we can't actually access our script from here and that's a problem because we don't actually want to put on line 11 delete myself quite yet we want to say hey timer start your timer and that can't really work until the coin knows about its own code so I'm gonna select in my coin seen the root node I'm going to go over to the inspector at the very bottom instead of pressing this little script add script button you can actually attach an existing script to any node that you want and so under script I'm going to click on a little arrow and say load and I'm gonna go into my scripts folder and I'm gonna add the coin dot Gd I'll press open so now this coin the root node of this coin scene has little script file that's the same as the script file that's on in my level that's on the actual coin and in fact this root node is the same node as the basis or the root of the coin that we're seeing so really these two files are the same thing and they're really in the same spot I really wish that if you put a script on a node that's an instance that it propagated back into the root node of the scene but it doesn't so the reason what we're gonna do this is because our timer is going to need to be handled as well but before we do that I'm gonna say if I'm hit by Steve I'm gonna need to access this coins child name timer and the way I can do that in my script is by putting a dollar sign this is how you can actually point to a node in your node in your game and I'm gonna say timer with a capital T now when use a dollar sign you have to type out exactly the name of a node as you see it but using this tell our sign justice I have it you can only access a child node from the know that you're putting this script on so it can only access these three nodes not something higher up on the level of our game unless you know how to do that and you can by the way do that so I guess a timer dot start and start is a function that needs round brackets to call it but you don't need to put any parameters in that function so when Steve hits us our timers gonna start and as we know if I click on my timer and go to the inspector the tower is only 0.4 seconds so it's gonna end really quick so what is the time we're gonna do once it runs out well let's handle that I'm gonna select my timer and then go over to the node tab next to the inspector and there is a common event that has a signal associated with it call the time out so if I double click on that and this is why we need to add the coin script into the actual coins scene onto the root note called coin so that I can select my my node my coin node and I can add a handler function for the signal of the timer onto my coin I you can't add a handler for a signal on to us on to a node that you can't see in the scene that you're currently on at least not easily hopefully that makes sense that's a mouthful so I'm gonna select my coin and this is called or it's gonna make a function called on timer time out good enough let's press connect with coin selected so now my timer if I go and look at its timeout signal it goes to function called on timer timeout which we know is on the coin script and if I look at the coin script there's the timer what do we want to have this coin do if the timer runs out well that's where this code goes on line 15 we want to have this coin which is this script this is the coin script not the timer script it's this file right here we want to have it delete itself how do you do that well it's a function for it it's called q qu eue underscore free right there so if I click on that or double click on it that's all you need and Q free will delete the coin once the timer times out after 0.4 seconds let's go ahead and try that out I'm gonna do a quick ctrl s to save and I'll play mile whole scene and we'll see if it works okay so here we are I'm just gonna wait for my enemy to get out of the way and I can roll around of course but I can collect the coin and the coin took a little half second or 0.4 seconds and then it disappeared but of course it didn't count up because that's what we're about to do I'm just gonna replay that and hopefully you can notice that when I go and touch the coin in a moment that it doesn't delete itself right away it took an extra little half second before it deleted itself and that's exactly what we want to add in animation during that little delay time okay let's go ahead and add counting to our heads up to splint so I'm gonna close my game window and go back to my level and the 3d view and I'm gonna select my control node to jump back over to 2d view and I'm gonna select the control node itself I believe Oh No I think I'm going to use the actual label that has a Q in it right now that'll make things easier so this label has a terrible name I'm gonna double click on it to name it counter with a capital C that might be important later and I'm gonna add a script file on to this label so I'll select it press little add script button I want to make sure that I'm saving the script file called counter into my scripts folder so I'm gonna go there and it'll be called counter dot Gd and I'll press open and then GE script label and no comments and create this code needs to have a variable to store how many coins you have collected and at the offset of the game that number is zero so right under the first line I'm gonna press ENTER a few times and I'm gonna say var coins equals zero and when the game is ready or when this note is ready I want to take this number and I want to put it into the labels text so I'm going to change this word passeth that's a little temporary code I'm gonna say Tech's space equals space coins and that's what we gonna give us an error but let's go ahead and try it out let's go and press the placing button and we got an error it says at the bottom under our debugger air setting property text with no value type of int essentially this number is a data type in programming called an integer or an int which is a whole number and this text field as text property actually will access the property of the labels text if I click on my counter and go to the inspector you can see there's a text property right there but on the counters code we're telling it to accept an integer using the assignment operator of the equal sign it doesn't like that so what we have to do is convert a integer into a string because text variables and data in programming is called a string so I believe I can just use a string method so string with a capital S that I can put an integer inside its round brackets this is I think good code so let's go ahead and try that out I'll press stop that's important to do and then press placing and as you can see now we have a 0 up here in our coin counter which is perfect let's go ahead and close this scene because somehow now we have to get it so that when the coin is collected so I'm gonna go look at my coins script so this little script icon right there I have to somehow get from this somewhere where if we're colliding if our coin is colliding with Steve either here on line 11 essentially or yeah right here I need to put hey a message or a signal that sends a message to our counter to count up 1 how do we do that or I don't have to put it right there I might actually want to put it depending on what you want if you want the coin to count up as soon as it touches Steve or if you want to wait until after the timer times out right before it deletes itself which is what I did you could put that same line of code right up here on line 15 before it deletes itself we have to create a custom signal because if I select my coin and then go up to the node tab under signals these are signals that are built in to Godot and the different types of nodes have different common events associated with them but you can make your own signals too and that's what we're gonna do we're gonna make a custom signal that we're gonna broadcast to the counter and that signal will say hey take that variable and count up by one and update the actual display of that number in the labels text so I'm gonna select my coin and go to my coins code and and I'm gonna create a new signal just like a variable up on the top of my code right below the top line that starts with extends so I'm gonna write signal space coin collect it okay that's how you make a signal and now if I actually click on my coin over in the scene dock and look we have a coin collected single over here but this signal doesn't know when to emit itself these built-in signals have certain events that trigger them they are event handlers but what is the event for coin collected we have to tell it when to emit and then we can use this interface up here to actually tell it where to connect to so I'm going to go back to my script on my coin and we're gonna decide to emit a signal either again when Steve touches this coin so I can put this line of code on line thirteen right there or I'm gonna put it on line 17 because I want to emit once the timer has timed out I'm gonna say emit this is a method or a function built in it's called emit underscore signal and so I can just click on it double click on it to a suit add it and the signal we just have to put in coin collected in double quotes so I'm gonna go into MIT signal type my double quotes and type coin collected and it knows what we mean because it knows for the signals or what signals I've defined in this code so there we go that act code should work except that it's gonna emit this signal but nothing is handling this signal yet in other words if I go back to my 3d scene and then click on my coin this signal isn't going anywhere there's no code that's running when it emits so I'm gonna double click on it in this node tab and I'm gonna connect it up to my counters code and my counter code is gonna have a new method or a function called on coin coin collected sure that makes sense um I'm gonna actually delete the first coin or the second word coin just so it's not redundant so I'll get rid of that on coin underscore collected yes you can edit that name so it's on the counter and I'm gonna press connect and there we have it on the counters code we had this new method called on coin coin collected or what I want to do here if a coin is collected I want to take coins and add one to it and then update the code on my screen so I'm gonna change this word pass or line pass with coins equals coins plus one that's how you can add one to itself you're saying hey make coins equal to itself plus one that's how you count up in programming it's a little weird but it kind of makes sense one City want you to think about it and we're going to say hey put the text on the screen and make it coins now I could copy this line of code but I think I can just call the ready function again and as long as we don't have anything else in here and we don't that hopefully will work I haven't actually try it that way before let's go ahead and press play seam and we've got our game and I'm just gonna roll around a little bit waiting for the enemy to go away and when I collect my coin aha it disappeared and weak get a count of one so we are almost there we need to add an animation and we need to set it up so that we can make duplicate coins and move them around and count up into a certain number and go to you in screen so we've got a little bit more to do still let's go ahead and and close up and now we know counting works the way we want so next up is animation and making the coin do a little bounce let's go ahead and go back to our coin scene and look at it in the 3d view our coin scene has a root note it's an area of course we have a mesh of course a collision shape of course and a timer of course and we want to add animation into this scene so I'm gonna go ahead and select my root note and press the plus and I'm gonna add the same thing that we or no that we added to our enemy in the last video the animation player but this time every coin is gonna have the same animation which is why we can add it inside of the original scene and it'll get propagated out into all the instances of my coin that I create so animation player and create and so now it's here I'm not gonna change its name and you'll see why a little bit later but with the animation player selected I get my animation panel at the bottom and let's go ahead and click on animation and say new and I'm gonna call this bounce all lowercase that comes in later so name it something that's easy to type and I'll press ok now this animation is gonna be very quick it's only gonna be 0.5 seconds so I can change that value I believe it's right here 0.5 no that's where I currently am it's a rate up here and this number it's 0.5 that's the duration number and so let's go ahead and animate so I'm gonna select my coin and I'm going to go to the inspector where because it has a child that's an animation player we now have all these keys and we're gonna animate the transform we don't need to rotate this time because it's are you rotating in the game so I'm just gonna animate its translation so I'm gonna press the key next to translation and we're at I'm gonna press cancel we're at frame 0 right now there is a step of 10 little steps or frames that I can make per second and only 5 for half of a second so at the very first frame at frame 0 I can type it in manually I'm gonna press the little key button and do I want to create a new track for translation and insert a key yes I do and so now we've got a keyframe there I'm gonna go to frame 0.3 and I'm gonna move it up and this is where we might have to guess and check later but I'm gonna press now the key next to translation to insert a keyframe right there and then it should drop a little bit so at 0.5 the end of our animation it's gonna go back down a little bit again and I'll press the key button so now we've got a keyframe at 0 a keyframe at 1 point 1 2 and a keyframe at 0.775 that's it this animation is called bounce because that's what we named it as we're gonna have to refer to that later because this animation is not gonna autoplay and it's not gonna loop it's just gonna play once and once it finishes it should be deleted it anyway because the coin is gonna get delete it anyways so let's do how to go ahead and press ctrl s to save and let's go back to our level and look at the coin script by the way inside of the coin scene this little button for the script goes to the exact same code file as if I were to click on the script right here next a coin in my level it's the same code file if you edit want to edit sit in both places so just in case you're worried about that it's the same thing o going to say instead of we're actually in addition to timer dot start when we hit Steve we not only want to have a timer start counting down we want to play that animation so I'm gonna add that UniFirst I'm gonna press enter right there and I'm gonna say dollar-sign animation player because dollar sign is how you access a child note or at least any node in the tree but it only gives you child nodes of the object that you're currently on which this code is on the coin so animation player I'll press Enter dot and this is where we can get two methods that are built into the animation player one of those methods is called play and all I have to do now is put in double quotes the name of the animation bounce and that's what we want to play okay so when a coin detects that a physics body is entering it it checks to see hey is the body's name Steve and if it is it's gonna play the balance animation and it's going to play the timer or start the timer and then when the timer runs out its going to emit a signal the custom signal that goes to the counter and it's going to also tell you itself so that should work let's go ahead and try this out we are going to have a problem I believe but that's uh let's see what happens okay so there's our scene and I can move around I'm gonna wait for the enemy to go away and I collected the coin but if you noticed when the coin played its animation it went back to over here and then play the animation and disappeared and the coin counter worked of course but let's just try that again I'm gonna close it and play the scene again and let's watch that again very carefully I'm going to go past the coin and it is little jumped over here and it'll play the animation in the wrong spot so we're gonna fix that there's a reason why that's happening yeah hopefully you saw there okay the reason why that's happening is because we created keyframes of the coin telling it to be in a certain location or translation and when we do that when I go to a coin scene and create those animation player keyframes we're telling it to be at that certain location which on the x and z axis is 0 0 the middle of our world so that's what's happening when we play the animation of our coin it should be it says to itself hey I should be over here at zero zero zero where this ball is and it plays there so how can we get around this it's using global coordinates well what we can do is we can trick it by making a little holder for each coin this is kind of a little hack I'm gonna select my coin on node that's the folder for all my coins and I'm gonna press the plus and I'm gonna make a new spatial note it needs to be a spatial type I'll double click so now I've got this spatial and I'm gonna call this coin holder one and I'll press ENTER and I'm gonna drag coin onto that to make it a child of coin holder one but coin holder one is at 0 0 0 if I select it and go to its transform properties it's at 0 0 0 so I'm gonna move coin holder 1 instead of the actual coin when I position all the coins around the map so I need to reset the coin position so it's at 0 0 0 in relation to or in reference to its parent which is also at 0 0 0 so I'm gonna select my coin go to its transform properties set it to 0 0 and 0 and now if I grab coin holder 1 and move it up and move it over and so I haven't actually moved the coin that coin still thinks it's at 0 0 0 because it's in relation to its parent and now if I select coin holder 1 you can see you can see it's translation is wherever I put it let's move it a little bit maybe let's move it oh I don't know over here ok just so we know that it's definitely not on the same axis at all and if I go ahead and play this scene and so let's see if I can get past the enemy and collect my coin aha it worked it stayed in the same spot it went up and it didn't jump back to 0 0 0 that's exactly what we want so let's go ahead and finish this project we're missing one major thing though we need a you windscreen so on the count of three I'm gonna snap my fingers I'm gonna fast forward in time and I'm gonna have the most beautiful you windscreen you've ever seen three two one aha there it is okay Godot is so easy isn't it in reality if you want to refresh yourself on how to make a menu like this with a button and text and fonts I'll put a link to that video in this mini-series on the screen right now but in reality this is almost an exact duplicate of my game over screen which I basically duplicated in fact I did duplicate it down here I took the game over right clicked and said duplicate and then I just edited the colors and the text and the different colors for the button and the background colour so there you have it how do I get there what how do i how do I win the game right now I can't even get to this screen well in my game if I go to level and then go to my counter I want to look at its code and it'll be the thing that sends me to the you win screen so the only time that I should be able to even check to see or need to check to see if I win the game is rate as I collect a coin so I'm going to do that I'm I'm going to and what I could do here is I could just say hey if coins is equal to ten then go to the switch scenes and go to the UN screen but I'm going to be a little bit smarter than that because there's often a little bit of a delay so I'm going to add to my counter it's own timer so with my counter selected I'm gonna press the plus button I'm gonna double click on timer my timer's properties I'm gonna set its wait time to a second sure that's fine with me when my timer runs out so I'm gonna select my timer go to the nodes signals dock and double-click on timeout and when this timer times out I want it to run a function or a method on the counter that says on that's called on timer timeout and press connect so now this timer is connected to a function on its parent the counter so if I if I go to the counters code it's got this function called on timer timeout but I actually need to make the timer actually start counting so when a coin gets collected I'm gonna do a couple of things I'm first we to see if we've collected enough coins so I'm gonna say if coins is equal to or double equal sign 10 will do 10 coins actually for this game we'll do five for the sake of the length of this video if coins is equals to five I want to say dollar store dollar stored all our time a dot and what's it called it's called start and once it starts it will eventually run out after one second and once it runs out it will run this function and when it runs this function we should change our scene and the way you do that is you type get underscore tree two round brackets because it's a method call dot change underscore scene and not the one with two in it but just change scene and now we can go to our res folder and the you win so our scene name is called you NTSC in but we're gonna point to it with res colon slash slash you win so I'll copy that and I'll paste it into double quotes right there and that should be it so just to recap that last little part when our coin collects or is collected it's gonna make the coin counter equal to itself plus one it's going to run the ready function which the text on the screen equal to the variable number and then it's gonna check to see hey if our coin is collected maybe that's all the coins that I'm done so it checks to see hey is if coins is equal to five then start our timer which is only ever gonna run if this line 12 is true if we've collected five coins if it is true of reflected five coins it's gonna run the timer and once that timer runs out it will change the scene to you win that whole thing just lets us do a little bit of a delay otherwise you could just put this line of code control see right there control V but I thought a timer would be better and the timer also needs to have this method made which is this one right here okay so hopefully this will work let's go ahead and do a ctrl s and play the scene and let's just make sure it all works and I need to duplicate my coin in the coin holder five times but just make sure this last little bit works before we duplicate the coins so that worked I got stuck on the wall there not not the coin that frightened me for a sec let's go ahead now and take our coin holder and duplicate it so I can right click on it and duplicate I've got coin holder too that's convenient maybe I'll put one up here and brown not to placate the coin I'm duplicating the coin holder because we're removing the whole coins world so the animation will play in the right spot let's duplicate that again right click duplicate and let's put that one though I'm making it easy for myself right click duplicate and let's put that one right here and right click duplicate and let's move this one oh I don't know let's put it up here I like it okay super super easy let's go ahead and play this game in fact before we do that I can actually to make this area a little bit less messy I can actually just collapse the coins node like a folder so I don't have to see all those coins that's why we made that there so let's press play scene and let's go and see if we can win this game I'm gonna roll around and collect my coins I'm totally avoiding that enemy and my coins are collecting just fine and if I roll down to the last coin aha it did it it got to five it waited a second and then it went back to the you win or went to the you win screen and now I can click on play again and now I can click on play and as we know it takes a few seconds to load our bigger level but it does work so that will be it for this video of course if you like this video or if I learned something go ahead and click on that like button below this video it really helps me and my channel out if you want to see more videos like this one in Godot or blender or technology click on that subscribe button and click on the bell icon to get notified whenever I upload a new tutorial check out my facebook page at facebook.com/cnn of what i'm working on next and that's where i communicate with you the most hopefully but that will be it for this video thanks for watching see next one bye you
Info
Channel: BornCG
Views: 40,818
Rating: undefined out of 5
Keywords: tutorial, lesson, 3D, model, beginner, godot, godotengine, engine, game, coin, count, counter, timer, signal, code, program, gdscript, gltf, blender 2.8
Id: rS-PcTTiGtA
Channel Id: undefined
Length: 56min 4sec (3364 seconds)
Published: Fri Jul 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.