I Teach My Wife How to Code Games From Zero (live stream)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so uh today we're going to make a game that uh esfania chose we're going to let the helicopter pass um so she chose those graphics from candy which are really nice the abstract platformer graphics and we're going to make a few mechanics from a platformer game okay um so right now you're in good o i'm just going to show you around the interface to get started okay here are the main screens we're going to use two today the 2d screen and the scripting one which is where you will be writing the code i prepared a game scene here in the scene tree this is where you create nodes you can see a couple here the ground platforms um those are the basic building blocks of your game in godot so you it's like lego blocks that you put together to make something more elaborate the ground and platforms for example are tile maps that you can use to draw parts of your level okay so here you have the file system this is just the files that are in your project i put them all um there in a single directory and separated the player okay on the right side you're going to have an important piece which is the inspector when i select different nodes on the left you can see the inspector update it shows the properties of the node it allows you to configure each node we'll see how in a moment um and so the last thing i can show you is uh here you have the viewport where you can select things you can draw etc at the top you have a toolbar where you have a couple tools so you have a tool to select objects a tool to move things to rotate them scale them etc we see we'll see them as we need them and at the bottom the bottom panel as well gives you access to more interfaces and we'll see if we i'm not sure we'll need one uh in this session but there we go uh last thing to be wary of is when you select different nodes you can see that sometimes something appears in the interface like when you select a tile map you're going to have a special interface for level drawing where you can choose if you're going to draw plants the background etc okay so that's for a really quick tour now um yeah someone's asking if you're completely new and yes yeah yeah yeah all right um we're going to start with the player so you're going to grab the mouse and in the bottom left in the file system you can see i made a player folder for you right you can double click it to expand it no the player folder this one there we go there you have a couple of sprites a couple of images of the player and if you go back up um you have the player animations that i prepared for you it's a scene you can see that from the tscn file in this icon you can double click it to open it in the editor should i yeah yeah go ahead so um you can see now you have two tabs at the top uh one for the player animation scenes and one for the game scene i showed you before um so this is how you're going to edit your game you're going to have multiple components that you edit and create separately and then you put them together those scenes that you make you can put a copy of them an instance of them it's called inside of your game so here you have the player animations it's just animations that i prepared for you you have the player animations note selected if you go to the inspector on the right and you set it to playing on you will see the animation starts to play then you can change the animation here in the drop down menu and pick the one you want idle does nothing okay and fly is going to have the character flying that's what we're going to do today we're going to make the character uh fly walk on the ground and fall so you can set it back to idle is a good one to start with and so you we're going to create a new scene in which we will use those player animations so you're going to go in the top left to the menu scene the really this one and you're going to create a new scene so you're presented with options to create the root node in the scene dock that you can see you can create a 2d 3d user interface or other node we're going to create an other node here so you can go ahead and do that click that and then those are all the notes in godot those are all those lego blocks i was talking about that you can use um for the character we're going to use something called a kinematic body you can see you have the search bar highlighted at the top and so you can start typing to filter the notes so i'm going to look for kinematic there you go you can start there and you have in red you have a 3d note while working in 2d so we're going to use the blue nodes and you want to double click kinematic body 2d at the bottom double click there we go that's going to be our moving player the kinematic body is something that can move detect collisions when it touches a wall it can stop when it touches an enemy you can or anything else a gem you can collect it those kinds of things so you're going to rename this now yeah i want to ask you if now my kinematic what is in this scene yeah it's an unsafe scene as you can see in a new tab you can double click the kinematic body 2d uh in the scene dock to rename it we're going to yeah you are going to rename it to player there you go um right now you can see a warning sign next to it because the kinematic body alone doesn't do anything what it's telling you is it needs some kind of shape to detect the walls and the floor etc because in games you have the characters you see and you have geometric shapes like squares capsules circles that are what the engine uses to do math to to calculate the geometry and see if things are touching if your character should be on the floor etc so you're going to add a new node and to do that you have the plus icon in the top left of the scene dock the little plus in the top left there we go and the next one is going to be a collision shape so you can start to type collision and collision shape 2d is the one we want this is where we start using the inspector on the right so with the collision shape selected on the right you can see it has an empty shape it's around the top there right you can click that uh on the empty on the arrow and then you can choose the shape you're going to use now two we typically use in those kinds of games the capsule and the rectangle the capsule makes it so if you have a corner like that and the character walks it's going to to smoothly fall off it's going to slide up the corners but also when you have a slope it's going to move a bit more smoothly at times than the rectangle the rectangle is much more precise that's what you would use maybe in a mario game in a tight platformer where you want to be really precise in this case we're going to use the capsule or perhaps i could use a circle just so the character smoothly slides against things and you will see this will come in handy when we make it fly at the end because we're going to make it fly so you can pick either the capsule or the cytochrome shape so this will be like transposed with my character afterwards exactly this will move with so the way the nodes work the 2d ones when you move the player somewhere and you're going to see that you can select the player here and then you can go to the move tool it's the four arrows there and at the top and you can in the interface click and drag to move it's good the tool selected you can see you are moving the player but because the collision shape to the is a childhood player it moves with it same thing if you rotate the player or scale it the children are good this is the scale mode if you want to try you can click on either axis and scale the shape will scale with it although after and you don't want to do that for some technical reasons i'm going to cancel all those changes we have the collision shape okay now we need the player sprite before we set the shape of the capsule so first you're going to go back to the arrow tool this one and you're going to select the player to add a new node selected in the scene here player node and you're going to add a new node as a child of it do you remember how to do that yeah exactly and you're going to look for a sprite this time this one already animated uh the sprite ah no we have the animated sprite excuse me uh um that's incorrect you can cancel uh uh escape to go out and we're going to because i prepared the player animations for you instead you see the player animations file in the file system you're going to click and drag it onto the player node this is one way on to the player node really okay you can see the changes to a hand and now you have an instance a copy of the player animations inside of your scene so we'll be able to access it from the code and to make the animations play so this is the sprite yeah this is multiple sprites that are put together you can see the interface that appeared at the bottom on the left side you can select the various animations i prepared for you the walk animation the idle etc you can click them to see they have different images in them each of these images is called a sprite typically okay um so now you want to select your collision shape again because the collision shape to the node in the scene and you can see the capsule doesn't match the character right so we want to make it roughly so it matches the height of the character at least one thing you can do is change the order of the notes because right now the capsule is behind the character it will be easier to edit if you put it in front so you're going to click and drag the collision shape 2d node below the player animations and be careful when you do that you can see the icons when it's an x it's not going to do anything but when you click and drag there you can you can see the line yeah exactly that was what you needed to do because if you drag on top of something it's going to make it a child of the node instead right so it's very flexible how you place as i was saying it's lego blocks so you can really nest them and do lots of things with them okay now select your collision shape there we go now it's in front of the character you can zoom in with the mouse wheel if you need to and you're going to do two things you can click and drag on the center to move the shape right like that there we go so this goes to the top of the the character now the thing is this is the shape that's going to touch the floor when the character falls so right now if you leave it like that the bottom is aligned uh not with the feet of the character but the body so you're going to have the feet inside the ground right you want to move it down a bit in the scale as well no no no you just move it down oh okay and then you have this little points that you can use to change the size of the capsule right so you can click and drag to change you can see what it does this makes the shape larger right this changes the radius and the top point makes it taller you can click and drag you can just we can cancel everything so no problem but this is the same as scaling but here it's not the same in this case so physics are a bit special in game in games often uh scaling can cause issues if it's not taken into account in the code so you have the shape that has a certain size mathematically and then when you scale it's going to add a multiplier to that size so when i select the scale tool and i change the scale like that if i expand on the side you can see you have a radius and a height here but when i scale the shape you can see these values do not change yet my shape is changing size right so the scale is multiplying that and you can find it here below at the transform under the transform category um speaking of which um one thing you can um do here is set the position of the shape to zero just so it's centered in the scene so you're going to pick up the mouse and then the inspector on the right on the right uh down to the transform category position there you go you can see it has an x and a y you're going to click the x field the value there you have to click on the yeah and enter 0 and type enter there we go so that moved the shape a bit to the left this is another way to change the position when you use those tools in the top of the toolbar like the the move and select tool that you have those here yep when you use those and you click and drag you're going to you can do that you can see the value changes so the inspector and the tools are connected you're always changing the properties of selected nodes right go ahead and cancel that and now the shape of our character is done we can place it in the game and this one the way it's fine that it's it's just to align the character you can click and drag on the value if you want uh with the mouse so click and drag left and right now on the value here in the inspector ctrl z yeah to go back um so you can click and drag instead so you have to keep the click pressed and you drag left and right so i'm going to press escape to go out and you can on the this field you click and you drag the mouse there we go you can drag it more like and you're going to see the thing moves slightly there you go so that's a good way to adjust the position of the capsule so clearly but here it's like to put it in the grid we wanted to do that yeah you want it to align with that line here basically you wanted to align the character's feet so move it up a bit you really want the the bottom bottom here to align with the baseline yeah exactly okay all right so this is the ground let's see the y this represents the ground now okay so it's a helper we can use to um align things with the feet of our character we're going to save the scene by pressing ctrl s or you can go to see scene save scene top menu inside the player folder so you can see this is a typical folder navigator and then you click save in the bottom right here you find this yeah this isn't default name player.tscn it's going to use the name of the first node you created okay okay so um we can now go back to the game tab at the top you have a list of scenes and the game scene there you go you're going to place your player inside of there to do that you're going to click and drag you can directly click and drag the scene we've seen you can click and drag it inside the scene dock but you can also drag it inside the world so you're going to click and drag it inside there and place it wherever you want yeah you're moving yeah so what's happening right now is you have the move tool selected right and you want the arrow tool one thing you can do so this doesn't happen um is yep good uh select the game note the game note in the scene doc there you go and you're going to click the little excuse me the little lock here this will make it so you can't inadvertently select it and move it uh from the scene so see now you don't have that problem you can select the player i'm just going to check the chat very quickly um all these french people no i'm french and she's from costa rica i don't know if there's anyone maybe there'll be a ud or someone there's another guy called chakra who's from here as well uh okay so we're going to start coding from there because we have the player i prepared the world so the ground has that collision information basically so the character will stay on the ground but right now you have the scene set up but it doesn't do anything because you don't have instructions yet on how you're going to move the player okay so the way you do that is you can save your scene right now the game scene yeah and you're going to go back to the player tab okay yeah and we're going to add a script to the player it's a file that contains code that the computer will understand um and that will tell the computer what to do with that player so you select the player node to do that and in the top right of this uh docker you have a a thing that's like a text file a document icon yeah this is the add script or attach script tool so in this interface i'm just going to tell you um you can choose and godot the language that you use for the script we're going to keep gd script um you have a couple options native gdscript etc because developers have the choice of a language that they're going to use in goodwill then the inherits part we're going to skip for now the template you're going to select it click it and set it to empty because by default you have a little template for people who start with godot alone so they know you know you need to put this kind of code here and this kind of code there a bell new to the language then the path you see it's going to save it inside the player folder as player.gd so this is good for us you can click the create button to jump to the script interface here we are in the scripting interface i'm going to just explain the new the first line you have extends kinematic body 2d because you are working inside a game engine kudo you created a kinematic body 2d node for the player this is code that other people have made inside the game engine right they wrote lots of features lots of functionality that you can use that saves you a lot of time making your games the extends keyword in this language called gdscript makes it so the code that you are going to write will have access to everything those developers made inside kinematic body 2d inside the goodwill engine right so this is telling this player script i wanted to have access to everything from kinematic body2d it's going to extend it it's literally going to add your code at the end of the kinematic body to the code file right you can think of it like that it's like i'm summing up instructions or something yeah you're adding code to the kinematic body okay now um there are a few things that are going to be a slight challenge in godot that i'll explain as i go but sometimes a bit super superficially so you can see first how it works in practice and then we'll we'll start to break it down so the first thing we're going to do is make the character um fall okay before moving left and right perhaps so you're going to type here and you're going to do a line return uh no line return is enter okay we want to make the character fall so the way we're going to do this is going to um the game runs uh yeah i need to explain that the game runs in a loop right so um every second the engine calculates images that the displays on the screen it's like traditional animators if you've seen how they draw they make lots of drawings and then they flip them right they show all the pages very quickly and you see things moving a game is like that the computer calculates many images per second and displays them one after the other so the instructions that you're going to write typically the ones that we're going to do right now are going to be inside a loop the computer is going to 60 times per second read the code that you do and go back to the top once we call that frame it's going to read all the code execute all the instructions and then wait and a couple milliseconds later it's going to do that again typically 60 times per second right it's not one frame per second no it's 60 frames per second okay default so to make our character fall 60 times per second we're going to move it down a little bit and we're going to use what you learned in physics in middle to high school the newtonian physics or going to apply a bit of gravity to the character 60 times per second okay so the first thing we're going to do is create that function that allows us to tell the engine this is what you need to do every frame those 60 times per second okay to define a function you start with the func keyword fu and c there you go a space followed by the name of the function i just need to do one thing i forgot in preparation for that because we're not going to use snipe hints today this is something okay otherwise it's going to add information that we don't want just yet so these functions are special in the functions to update in the game loop are special in every game engine in godot it's underscore it starts with an underscore so shift no it's uh shift and here you can see the underscore there okay right um and it's called physics process so you you start typing physics and you can see it appear and you can press enter to accept the suggestion from the engine then a line return and now you are inside the function this language there's something particular with the syntax i'm going to write pass temporarily here just so we don't get the red line you can see it has these symbols here those are tabs this is the the tab key you can see every time i press tab it inserts one of these symbols right and this language works like that the computer knows what is part of your function thanks to those tabs what we call them indentations so the instructions you can't write them there on the left you have to indent them you have to have one tab on the left okay the kinematic body 2d node to move things has a special function it's called move and slide so you can call it in godot you can use the code that those developers made and have it run 60 times per second so you're going to type move and you're going to see it appear go ahead and you want to take move and slide the second one so you can see there are a couple of things move local x etc those are all the the functions all the code that the good developers wrote for you they are amazing yeah every game engine is is like that now move and slide you can see you get this line that tells you the arguments so the the parameters the the values you can pass to the function you just have to spit it on the screen um so a velocity uh to show you i'll take the this uh so excuse me in the chat sometimes i need to draw things for estefania and i'm going to do this here because of the the setup we have so a velocity is going to be a vector you remember what vectors are from yeah i think so okay so just as a reminder a vector is if you can think of it as a set of two coordinates uh is going to typically we write it like that v x v y the vectors x component this is this horizontal part and the vectors y component this is this vertical part here um so you have two coordinates you saw earlier if i select the player node and go back to the interface and expand the transform size tab here category you have a position and you can see an x and a y this is a vector with two components x and y a velocity is a similar vector okay and uh the information gives you here are both a direction and some speed encoded together into a single vector we typically the vector is this or is that was it the line it's this is a representation it's the whole thing so this is the starting point of the vector if you want um this is uh the direction that you can see like the orientation and it has a certain length that's the definition of a of a vector right um so it can be in any direction and we control that this x and y is the coordinates of the starting point no this is those are the coordinates of the whole vector if you want in the computer on the computer the way uh we represent vectors um the starting point and then the length and the the end of the vector are with two sets of coordinates we have an x and y for the starting point and we're going to have the components of the vector as two values x and y right all right so the velocity is going to be something like that what i want to point out is we're going to use this x and y right to define how long the vector is and how it is oriented right in the case of games there's something a bit special so in math you saw something like that um the you saw graphs like those where the x-axis goes to the right and the y-axis goes up okay in games it doesn't work like that it's the opposite for the y-axis the y-axis goes down and the x-axis goes up those things are just conventions so in math you can do that as well actually the axes can go anywhere you want it can be rotated and those kinds of things but it's just in games it's a convention we start counting the pixels here the positions from the top left of the screen to the right and the top left of the screen down towards the bottom so this is the position zero zero zero on the x-axis zero on the y-axis and this would be a higher value on the x and on the y so here in math it was this was minus y uh yeah this was minus y so it becomes minus y here minus y is to the top yeah okay with that uh we can get started so we're going to give it a vector that represents how our character should move every frame the way you create a vector is using a name there again uh defined by the good of developers called vector2 it starts with a capital letter so you're going to start typing vector with capital capital v back and you can see vector 2 you can press enter okay you're going to open parentheses open let's see parentheses next go back to 2 wait you need to be right next right after vector 2. okay and then you can see the vector 2 takes an x and a y value so for the x that's why it's true or no that's why it's called exactly vector three takes three xyz and in math you can have vectors of any size you could have vector four x y z time for example right those things are arbitrary but now handy to represent coordinates for the this is going to be the velocity so we want to apply gravity to our character so we want the vector to point down how do you think considering that x is the horizontal and y is the vertical how do you think we make it point down which values do we use for x and y well for y something positive yeah and for x for x it doesn't matter i guess uh it does if you make a big x it's going to do this or and i guess if x is going to do this okay zero okay so you can enter zero comma and then the value you want can be a few hundred because it's going to be a speed in pixels per second right there we go so um i just want to to take a second to go over the syntax here you can see that you wrote the name vector2 and then inside parentheses the x the comma and the y there is a space you don't have to put the space just for readability to separate them so vector2 is let's put it like that for now it's a built-in feature uh that takes values arguments to build the actual vector2 right inside the parentheses um is it's like a function that you're going to call on the vector2 that's going to create it and it tells you you needed an x and a y the values inside the functions need to be separated by a comma now if i cut this inside the parentheses and i open the parents there you can see there you have something similar like move and slide takes a linear velocity it's a vector2 and then you have a comma it takes an up direction that's a vector2 etc right so um we're going to do the same our first value is the whole vector2 with the x and y so i paste it in there and we have the first value then we can add a comma and you can define the up direction this is important for the engine to know how the game world is turned right the up direction says this is up so if you say up is like that right it will know the floor is in that direction the walls are in those directions left and right and the ceiling is up right this is how the engine gets that information uh because in games you've probably seen games where the the gravity is reversed and you can walk on the ceiling and jump like that down you've seen that already in mario and all i think they've done it that's with this kind of information that they do that so if i put up direction down would it float it won't float directly uh it's just about detecting where the floor is where the walls are right so if it will fall because you're you're moving the character down still but then when it touches what you consider the flow if you reverse the up direction it would say oh i hit the ceiling right okay okay yeah okay so for the direction you can see it says the up direction is a vector 2 as well um so you're going to enter vector 2 again do i need another parenthesis no first you type back to two and then the parentheses is to build it okay enter no vector2 a two you can just uh correct and put a2 open the parentheses to create it and then the x the the up direction we want is a vector that points up like that right so how do we do this one if this one was 0 1 or 0 something what is this one zero negative something yeah and negative one because for directions we use vectors of length one so we're going to type that and yeah you remember the comma that's good and minus one exactly okay so uh with that your character is going to fall i'm going to go back to the game scene and to the to excuse me to the main screen select the player and move it up really quickly and then we can play the game by pressing this play button or press f5 and the character falls it falls very fast so i'm going to put it up so you can see it fall boom until it touches the ground and you can see godot makes it stick to the ground right this is the first thing you've done now we're going to go back to the script and i want to introduce a few things you can see this works right but we have a little issue right there it's that if you go back later two months later and you read the code and maybe you know you wouldn't remember why is this 0 500 or what is this thing that's 0-1 right it doesn't when we create the function when we write it like that um when i do move and slide it tells us it's the linear velocity and the up direction but now we don't have the names anymore so you come back later and you don't remember why you had that right and you have to figure it out now there's something you have in every programming language called variables that allow you to give a name to things right so this instead of being a vector 2 0 1 could be up direction the way we do that we can do that in different places but we're going to do it outside the function and we're going to do that by defining variables to define a variable you type the var keyword var you start with that there you go a space and then you type the name of the variable it needs to be letters and underscores so typically we write a word underscore another word you can't use spaces in there okay so uh the first one you can create would be no you can start by the word okay right but instead of spaces between the words you're going to use a direction so this is called a linear velocity we can shorten it to velocity so you can say var velocity you can type the name a space so i use underscore instead no no um you're going to put a space literally oh right that's a whole name then you're going to use the equal sign to tell to tell the language to this name i want to associate this value so equals okay i'm sorry the equal sign is here okay i'm using the spanish keyword yeah yeah and you're going to put in the value so one thing you can do is with the mouse you select your perspective to here completely and you're going to cut it and paste it after the equal sign or copy cut cut yeah uh you took one more parenthesis but just oh okay yeah just the vectitude with the values yeah and paste it there okay great so now you have variable name velocity before the comma in the move and slide function call you're going to enter the word velocity and you can see you can complete with enter press enter there you go um by the way each time i want to use that i can use velocity now exactly exactly so one thing i want to mention is you see when you type that i've seen you use the arrows several times it automatically selects the first thing and you only use the arrows to go down the menu or another entry and you can press enter yeah it goes automatically to the first section so um now we have that second value that is uh corresponds to our up direction uh how would you go about you know replacing it with the name of direction instead of using the hard-coded vector to zero minus one so i would put another variable yeah and name it direction and well cut and paste as well exactly if i try it yeah you can go ahead and do that okay okay then i put direction i guess yeah exactly and this and there's a back wait yeah you need to remove that okay okay so there we go um uh the name could be because we're going to have another direction for when the character moves so that would be good to name it up direction so default direction you can type up underscore see that's how we name variables and same thing there there you go you can play your game again by pressing f5 and sorry i want you to ask you why did we use move and slide for falling um because godot takes care of a couple things for us so if you don't do that if you try to move your character manually in the game the way it works is imagine this is the flaw and your character is here when your character moves it can move past the end of the floor okay so the move and slide function takes care of putting it back on the floor and many other things if the floor is like that it will find where to place it you can move along the floor and those kinds of things um it's called move and slide because if you're hitting something that is not straight you're going to slide against it okay you will see that probably a bit later um but so we use that because doing it manually is many more lines of code and uh because when developers other people made good code for you to use as developers we typically use it to save time right okay so you can uh any other questions okay you can press f5 to play the game and nothing change right it still works that's a good sign when thing where things work you can press f8 to stop the game there we go go back to the code um so this was just show you how we name things in code to make it more readable right okay now inside your physics process we're going to start moving the character and i want to ask you if we left here a space for readability or it is yeah typically uh so what we do in general is the functions we separate them when we finish a function with a couple lines uh variables we will group them so typically you wouldn't have a line in there right uh and it's up to you how many lines you want typically we put two lines you know one line between the variables and the first function but if i had not if i didn't have these spaces it would work as well yeah you just need to have each instruction on its line right you can't put two variables like var velocity in another var up direction it won't work you can try to do this and that will give you an error uh okay the next thing we want to do is move the character left and right okay so in the physics process before we call move and slide we're going to calculate a move direction and move left and right depending on the keys you press on your keyboard okay i'll save this yeah it's good to say that when you play the game it saves your code by the way so uh before move and slide we're going to add that and so those variables we created we can also create them inside the functions the way it works exactly is the name if you put it up there is available in the entire script file in all the functions anywhere but if you put it inside the function the function ends after move and slide the variable the name will only be available until the end of the function also inside the function right so typically we call that the locality of variables we call those local variables those are variables regular ones let's say member variables to be precise and we're going to use a local one because we only need to access the direction inside physics process right but i have a question so moving slide is a function that is inside the function of p6 processor the move and site function is a function you call it's something that exists indicator engine that you call inside your physics process physics process tells godot run this code 60 times per second i see so you're going to create a new variable called direction curing physics process yeah on this line uh first you need the bar keyword to create the variable okay always the bar says i define a variable and you're going to set it equal to um and the direction is going to be a vector 2. so our velocity is a vector 2. uh the direction is going to be the same thing just like the up direction 2d vector that represents how the character orients how it's going to move and you're going to create a new vector2 parentheses and you're going to initialize it to 0 0. we'll the comma and zero exactly so at first we have no direction now we're going to listen to the player's input those computer programs um when you type your keyboard or things like these the computer is constantly pulling it's asking the keyboard which keys are pressed or any other peripheral that are removing the mouse etc very often if you want so in games it's the same we have to listen to when the player presses a key using a line of code right and in godot you're going to go to do a line return of enter and we are going to do two things at once here first we need a condition if the player is pressing a key a specific key then we change the direction okay so to start that condition you type the if keyword i'll let you do that okay a space and then comes the condition so this is going to be something built into godot this is how you ask for the keys that are pressed so this is input with a capital i there we go and then a dot and the function on the input object is called is action pressed so is underscore just press no action pressed okay this one press enter and then it gives you a list of actions that you can pick from the move ones which uh while i'm going to use are ones i prepared for you for this uh first lesson so we're going to check for move right fast you can go down press enter there we go and at the end of the line just like at the end of the function definition you have a column at the end of a condition you need to put a column yeah the column is this one so shift this there you go and line return it finishes with the color yeah this uh ends the condition to be exact what the colon does says is after that starts a new block that is indented you're going to see when you press enter it's going to go one step further to the right so press enter there you go so you have another indentation level and this is uh the body of the condition those are the instructions that will be run only if this condition is true right so if the player presses the move right action which is d on the keyboard or necessary the column is only for conditions it's for every block so you can see at the end of the function definition you have a column at the end of a condition you have a column but it doesn't matter why we didn't use it yeah it's because move and slide we want to always run it inside physics process but the code we're going to write is only valid if the player is pressing the move right action which is the d key on the keyboard okay so if the player is pressing d on the keyboard if the player wants to move right we're going to change our direction right so now we're going to type direction okay there we go you can enter it dot x a space an equal so the equal here means i want to assign the value on the right to the thing that's on the left the direction is a vector 2. a vector 2 has two components x and y you can access them with eight dots then pressing x typing x or y right respectively so because this is x here that we are accessing this is a single value and so if while pressing move right we need the vector to point to the right so what value do we use on the x-axis one exactly can type one there we go so this is um um very simple uh right now what we're going to do i'm going to do a small thing here we say velocity dot x is equal to 500 times direction.x i'm typing this for you just for testing because then you only need to create that code but if we press the move right key we're going to move to the right um and the velocity we always set it to the direction that we have um multiplied by some speed value right and that's going to make our vector point to the right so now if you play the game if you press the d key on your keyboard the character should move to the right but only when you press it you see so this is why we have this condition this is so the character only moves in those cases okay all right so i'm going to remove that because i'll have you write it later but how does move right nose did it see it's because i defined it i'm going to show you because it's nothing magical in the project settings you have all the input actions and uh i defined those down there so move right corresponds to the d key or left stick to the right on a gamepad which i'm going to bring i'll go get it because it's more fun to test with the gamepad okay now um we want to have the character move to the left as well right so one thing you can do is align return you're going to go back one level align it with the if and so there's something called no you can use we're going to stick with if you're going to do the same thing but or move left you think you can do it completely i'll try yeah okay i'll let you do that i'm going to go get the gamepad and i'll be right okay [Music] not bye [Music] minus one i guess yeah exactly um so uh now this code only changes the direction right it doesn't make the character move so what you want to do is again a line return go back exactly and this time we're going to change the x velocity so same thing velocity is a vector 2 like the directions you can type lsd.x equals direction.x times some value that you want dot x a yeah multiplied by uh you can use it like there and um can be 200 300 400 whatever you want okay you can press f5 okay and it's done now yeah because we move in slides using the velocity so because we change the velocity we say it's a speed multiplied by the direction direction can be zero one or minus one depending on the conditions that's all met right so we move inside with the velocity so while changing the value of that velocity dynamically based on what the player does and you're going to test that right now so you can use the left joystick to move the character oh yeah there we go nope lots of chats for example by the way for raspbian who should be here uh if people have questions and all you can if you want to chat you can chat as well of course but um you can also answer that questions although i don't see too many yeah it seems people are mostly commenting so what's what are you trying to change ah yeah timestamp if you change the speed indeed it's going you can change it again i already did it faster okay okay and you can move in the air as well right while the character is falling okay let's try it again so this i delete no no you're going to keep it the only thing is instead of 800 like we've done for the velocity and direction how about we name that value right so at the top of the script you can create a new variable called speed here a velocity is a speed with with a direction put together right so the speed is one thing and the direction is once one thing which we calculate 60 times per second speed equals whatever you want it's the the value you put there so 500 it's fine yeah it might be a bit fast but that's fine 300 that's fine and so direction times now exactly just for the people watching if you can put the line up to speed just give some space good now you can test it if you want it works the same excuse me you can use the controller anytime i'll just keep it or you can put it on the side whichever you prefer okay then that's done yeah it's all good so that's moving the character on the ground um now there's just one thing when uh we put the gravity like that i'm going to make it smaller and you're going to see the character moves at a constant speed but not that's not a realistic or natural way to fall right it's very floaty it's very unpleasant let's say so instead we are going to apply gravity now this is something you probably have seen in physics it's like uh gravity is typically a false and so it's going to apply constantly and so the thing is going to start slow and fall faster until it hits something that stops at the ground right so we need instead of having a fixed value here we need to have something that gets bigger frame up to frame so there again it's going to start at zero and we're going to apply some gravity every frame to increase the body so here i'm going to have you create a new variable called gravity okay yeah very power here yeah whichever it's it's good okay gravity and it's going to be equal to some value it's so this time it's not a speed it's an acceleration downwards um so the value needs to be a bit bigger something like 2000 should do why because it's compared to a speed that is how um many pixels you are moving per second this is about how many speed units you are increasing per second right so the character this is so the character falls and accelerates like that downwards we need to calculate that of course so inside the physics process function so that is done gravity yeah yeah okay inside the physics process function we're going to use it so below the velocity.x calculation it's a return yeah you need to align like that very good um you're going to say velocity.y and so we're going to increase it every frame yeah every time that physics process runs so to increase it you can do equals dos dot y plus something so velocity dot y plus plus here now you have to type again because you need to increase velocity that way so velocity dot y is going to be equal to itself okay can i do later yeah plus what value would you like to add to make the character fall like a number or just uh other gravity yeah and so if you have the gravity like that you are going to increase by 2060 times per second the downward speed so this is huge don't want that to happen if you do it you'll see the character like that and this is going to be on the ground yeah right and may even at some point go through the ground and disappear for technical reasons because that value is increasing increasing increasing so much that some point might just jump through the world because it goes so fast okay so you're going to go gravity multiplied by and this is where we introduce this the physics process function provides you with a value uh called delta this is similar to when you call move and slide you give it a velocity and up direction you give move and slide that the engine gives physics process delta inside the engine there is something like that just to show you imagine while in the engine code and there's something like physics process data it calls it passes it the the value delta is a time difference between the previous frame and the current one it's how much time has passed since the previous frame so when you multiply something by delta you make it time dependent instead of frame dependent the instructions you write without delta are going to be ran every frame now the thing that adds complexity to games is the computer needs to do these 60 frames per second but you've probably seen a game you played that slowed down at some point like the game started to be slow laggy when that happens um a thing that can happen is if you keep the instructions without the time delta everything slows down because the frame take take more time to run so um the there are fewer frames that run so there are fewer updates and instead of doing 60 times per second making the character move forward it's three times right or six times it moves ten times slower and you see these choppy images like that right by multiplying by delta you make it so the update depend on the time that was spent between the frames and so if the game slows down the things will still move the same distance over time okay uh so now we have gravity times delta and you play the game the character you can see now it doesn't fall uh the same way as before we're going to lower the value it's a good one 2000 but you can see how it accelerates until it hits the floor okay now we have the base movement and gravity working there's one technical thing that we're going to do is here i'm going to do this for you and briefly explain i'm going to say velocity is equal to move and slide let me explain a function can make some changes right it's going to move the character it's going to make it collide with the flow and then at the end it can return a value you've seen that in math f of x is equal to 2x plus y something like that right if you remember those kinds of things when you call x you get another value returned move and slide moves your character and when it hits the ground or something like that it says i hit something so i was accelerating downwards and velocity.y is getting bigger and bigger and bigger and move inside we will say if i hit the fall i'm going to reset that velocity to zero right and so it gives you the result it says i hit the floor so the horizontal velocity is fine but because i hit the floor the character stops it doesn't have any more speed going down right so it tells you now velocity is equal to zero on the y-axis and when it returns that value when it gives it back to you you have to manually capture it you have to manually assign it with the equal sign here so that's what we're doing it's not too important it's small technical detail that we need to take care of but i didn't understand i'm sorry okay um yeah this is a bit tricky at this point because with needs probably to to work more on functions and how they work move and slide is going to move the character based on the values that you give it at the end makes calculations and it's going to send you back some new value for the velocity that's just how it works so it depends on every function but it's in its documentation if we go there you can see there's a lot of documentation and just tells you it it makes some calculations and maybe you can read it there uh returns the linear velocity vector rotated and scaled if a slight collision occurred this is a bit you know technical speech for people have more experience programming but it tells you it returns you a value right this value is a new velocity it's an updated velocity based on what happened when the character moved and so we need to assign it to our velocity here but i don't understand maybe it's why velocity equals to movement's light which uses velocity so that's what i'm saying okay when you do that at when the code runs this will get replaced by the value returned by move and slide so what this says is call move and slide and give me back the result when you call a function that's what you say in code you say run the code inside this function and give me back the result so this does stuff right it calculates a new velocity so maybe it's going to be something like that new velocity just to to show you going to be maybe 500 zero if the character was moving right and it touched the floor right but maybe before that um here before that maybe your velocity was uh 500 1500 because the character was falling and it hits the fall and that function is going to detect that and going to reset that value to zero but it just returns it if you remove that part and you don't store the result inside your velocity the value gets lost it makes the calculation it says here's the value nobody uses it i don't care so you stole the result in the velocity variable there you go um and with that you have your character falling moving left and right you can test that in the game if you'd like you have the controller okay all right um the next thing we're going to do if you want are the gems so we're going to collect gems okay i'm going to remove the distraction free mode and go back to the 2d workspace right let's move your player somewhere like that so now we're going to create gems that when we touch them we can collect them this needs two components we need to design the gems to be collectible okay and then you will need to add some code to collect them okay for the gem we're going to create a new scene do you remember how do you create one let me try here okay uh in that scene you're going to create an odd node as well to start with in the scene dock oven yeah that works um and the one we're going to no no it's good it's good yeah it's the same the node that you are going to create uh right now is called area 2d there we go okay so this node defines an area the kinematic body is going to hit things and stop on the floor etc the area is not like that you can use it for any kind of area like uh inside water you change how the game works all those kinds of things so it's just going to be it's going to use a collision shape like we use for the player can be a circle it can be a capsule it can be whatever but in itself you can pass through it uh it's just going to detect that you touch it and tell you right so as a child of the area you need to add two things first we're going to add the gem so in the file system in the bottom left you can fold the player directory and then you have maybe the red jewel right and you can click and drag it inside your scene here yeah and creates a sprite node for you uh now it's a bit far off compared to the start so one thing you can do is select the red dual node and you can select it in the no now you're moving everything let's see select the area 2d should i return it uh yeah control z um one thing is by the way no no no no you've uh canceled a bit too much well you have to recreate the area 2d node now you have to recreate it area 2d and then you're going to click and drag the red jewel in the scene wherever yeah we're going to move it so um i'm just going to mouse wheel click and drag to move the view and center it here so and zoom in so we can see our jewel a bit better okay right and so what you're going to do now the jewel you want to roughly center it on on those axes right because otherwise it will be offset every time you will place it it will be opposite the problem is now you're selecting the area so control z you need to this is the move tool and you need the selector which is the arrow there we go and then you can click and drag it and place it in the center great perfect select the area again you can see the warning sign it tells us we need a shape like we did for the player right so you're going to add a new collision shape 2d node as a child of it so let's say the image without that is like it's just an image it's just like yeah it just displays on the screen it doesn't do anything you can't detect anything i see so i have to add add a node yep collision shape 2d okay can i do the same put it here uh no you can keep it down it's fine now the collision shape has a warning because it needs the shape so select the node and in the yeah you remember the shape you need to click and you can create a circle or this one there we go you can click and drag this is the the area that will control when you touch this circle with your player's capsule you will be able to collect the gem so you want to make it roughly the size of the gem now you're moving it and if you click the point you have to click that little point if you want to change the size there we go that's perfect you can save that as maybe a cancel and you want to rename area 2d and talk double quick to gem for example jim joel uh jewel whichever you prefer yeah press enter save okay control s and now it's going to say that's dual.tscn which is the theme okay you're going to add a script to this jewel as well um and you remember how to do that um top right of the scene doc no okay let's see here no here no this creates a new scene no you need to add it it's right and you can create it straight away click the create button this is fine yeah i'm going to use the same language and everything okay so this one you can see it's different from the planner was a kinematic body 2d node automatically we get the extends kinematic body to the line this time it's extends area 2d which is a different it has different features compared to the kinematic body this time um how are we going to do this uh okay that i can introduce a feature that's a bit special so we saw with physics process that we can write code that good will run 60 times per second now um with the area we could do that we could use physics process and 60 times per second ask is the player touching me is the player you know nearby right and if that's the case we we do something there's another feature in godot and in programming languages often to make it faster because running the code 60 times per second well it's often more work than you need to do after you you don't want bit something that happens just when you touch the area you're like why run it 60 times per second if instead i could just do it once when i touch it right you can do that in godot through a feature called signals signals are inside the engine the engine does its stuff it does its physics checks and then it tells you this happened right i did my calculations this happened so in the case of an area you can know when something touched it through a signal for the signals i'm going to show you to set them to connect them to by default they're all disconnected so you don't receive any information and you have to selectively connect the ones you want to listen to this is for performance because listening to lots of things you don't use is a waste of time uh so in this case yes it helps with uh performance yeah on the area node to find the signals we go to the node tab and then you have the signals here and you see you have a long list and you can see why they are not all connected by default because there are many of them right and then on the area 2d we can see a couple that are interesting area entered mean and means another area intersected with this one right but our player is a kinematic body node so you want to use the body entered here okay so i'm going to go back and just to so you know you to connect a signal double-click it then you select the node you want to connect it to which has to be a node with a script and it's going to create a function for you i'll explain that later but first i'm going to go back and you need to do that yourself select the node go to the node dock the node the dual node the area the origin not just this one no or no just the jewel just the jewel yeah you're not selecting it right now the top jewel node this one click this one then you go to the note tab and you double click the body and signal to connect it you're going to connect it to the dual this method is fine yeah okay a method is a function it's a technical term for function that is attached to an object to something like a node if you want in this case right um so you can see it created a function you have a little icon that you can click it says uh the jewel connects the body answered signal to itself right this is what this means because you can connect to other nodes as well um okay you can click ok in there and so when the body entered the jewel that body is going to be our player because there's only that in our game you can write some code so you can replace the past instruction pass just means do nothing but don't give me an error right so i'm going to select that line and replace it entirely okay go ahead and we're going to call a new function called q underscore free so q and you can press enter there we go q3 means delete me the term q is because again technical thing it's like sometimes there's a little delay for the computer so it might be nanoseconds right so you but it's just technical because you have the other three that is unsafe and q-free is safe okay but we use q-free and good old almost all the time so there you go you have that and that's enough to just make the jewels disappear when you touch them so you can save now then you go back to your game scene that's empty yeah yeah you q3 doesn't take any argument just says delete me delete me it's the jewel and the children that go with it um the the way this works in uh this kind of code is when like before you called move and slide you didn't say move and slide me automatically it's implied that is the player which has the script attached to it so when you call q3 it's the same thing it is implied that this is a jewel that has a script attached that's why it's extensive today because we are working on it i get it so um now you're going to go back to the game tab the game scene sorry there you go and to the 2d main screen which you can see at the top this is 2d yeah yeah the the script uh view is different from the from the scene view so uh you have your player in your game now you're going to add some gems so click on the game note past game note this one because when you drag and drop gems in the view it will be added as a child of the selected notes as child of the game if you drag and drop them and you have a player selected it will be a child of the player and so when you move the player it will move the j [Music] you can do that if you don't select the player select it there you go then you need to find in the file system the jewel scene so jewel.tscn you can use such file to find it type dual and you can see dual.tsc but twins here yeah yeah it's just for fun jewel dot tscn you can drag and drop it there you go you can drag another one jewel dot yes yet no no this is yes this is just an image there you go wherever you want and now play play the game when you move the character what happens because the jewel is a child of the player when the player moves the jewel stays at the same offset it's like the game you played you later which one uh pika niko no no the puzzle one i forgot i have baba yeah yeah a great game uh so now with the game note selected you can do the same you can drag and drop jewels inside your game go ahead i'll let you do that the jewel.trcn that part right so i put them wherever you want wherever the player can reach for now perhaps because if you put it in the sky you won't be able to collect them no no no no no there you go so now you have three here one thing we're going to do i'll do it really real quick is just remove that stuff so you can see them better and now you can play the game i want to see yeah it will wow like that so you can see you touch the drawer very fast and when you move forward and you touch them you collect them it's nice so technically they are just getting deleted we don't have code that keeps count absorbs it yeah exactly um but let's start already i think the collecting and the the accounting for that would take a bit more time let me see how long have we been yeah it's been one hour 20. okay so i want to ask you if we can reduce the velocity to see when it collects this one yes so how do you think you would do that where did we define the player velocity let me see our animations you can close it already this one yeah it was in the script yeah no this removes this first oh no you need to click the script icon it doesn't delete it it just says detach it there we go where is your velocity now carrying gravity yeah okay and probably you need to go back to the game scene you need to go back to the game scene uh click the 2d screen there you go and you need to either move the character up a bit all the jewel down a bit because otherwise it's going to be too fast ah yeah so it works you can see okay all right next thing we're going to do is perhaps animate the player right i don't know if we'll have time for the fly thing or we'll need to go very fast your car um because the fly thing is cold but it involves small math so to explain the math is a bit longer okay um we're going to add the animations on the player now this is going to introduce something new we have a player node we have a player animations when nodes are as children of one you can access them from your code and you can say do something with this right so i'm going to show you very quickly and then you're going to to reproduce that one thing we can do is we can say um if direction.x is not equal to zero we can call a function called get node and we're going to get a node by name player animations and once we get the node we can put a dot and we're going to play uh all animation is equal to walk we can set an animation by name like that i think that's the way we do this animation yeah that's the property okay uh and we're going to set it to play and by default it didn't have the animation right no uh-huh so that's the thing um so you can see there not animating but if i press oh yeah now it animates yeah but if i stop it doesn't stop so this is just to show you i wanted to introduce that to say the computer does exactly what you tell it to do and nothing more so if i say if the direction is not equal to zero set the animation to walk but i don't tell it to set it back to idle then it's just going to walk indefinitely right um so i'm going to have you write that again if the direction is not zero you use exclamation mark equals to say not equals direction yeah good dot x is not equal so it's e uh exclamation mark equals exclamation is shift here equals not equal to zero yeah colin this is a condition so this is the end of our condition okay then you go down one line and you can say we have a function to get access to the node we need to be explicit remember we need to tell the computer everything so you say get the node player animation so get underscore node player animations so you can go no you need because it needs to be it's not that it needs to be in quotes so it's easiest to use that you see there are signs okay we say it in quotes because when you see the quotes like that in in yellow it means the computer is working with text information right so there are lots of things in the computer that we do with text just like the actions is action pressed move right was the textual representation textual name for the action so you get the node player animations dot you're going to access the dot by the way i didn't mention now after okay we'll first get the node and then it's going to give us the node see when i say the functions return something when you call get node when the code runs this is replaced by the actual node player animations right so it's like if you hide the node and you write the dot to say access something on this note on the player animations note you're going to type it has a variable like the ones you made above called animation there we go you can press enter and equals and there again as text you need to give it the name of the animation so you need to add quotes which is shift here there we go and now you are typing inside the exactly the name is walked yeah the names are exactly the ones i defined here idol walk and the fly are the three animations we have and you'll see that inside the guru editor i set the animation to be playing i checked the box here if it's off it wouldn't work it would just not play it would change the animation but it would not be playing right which can seem a little weird but again you need to tell the computer everything and sometimes you want to change the animation before playing it in preparation all those kinds of things so if the direction is not equal to zero the character works now we're going to introduce another thing can we move a bit this to see like yeah we can actually do this a bit more space then it will do the same thing i showed you so we need to change it back when the direction is not so when the direction is zero sorry the way we do that you could use an if direction is equal to zero but there's a keyword called else right okay so you can type else colon yeah because there are so many cases in programming where if this happened do this else do that so common that in every language pretty much you have that okay so you can go back go to the next line and it's going to be the same get notepad animations just a little thing you can save some time instead of typing everything you want to type everything or practice or yeah okay no go ahead we can duplicate lines and move them around and then player animation yeah use the arrows press enter yeah exactly then to access the animation animation equals to idle i guess exactly it's pretty good right all people are saying well they're just chatting ten thousand persons came in i didn't see if there was someone from family family or something uh probably but um and then we can try it i guess then you can try it exactly but if you keep walking it's going to walk right and if you stop it's going to stop and go back facing towards you okay yeah cool um and one thing we can do you saw that if you walk to the left it still looks towards the right yeah i think so uh-huh okay we can make it look towards the left that's why it was like weird okay yeah the way uh we make it look left is when we change the direction we're going to change a property called flip h right so if i i'm going to play the walk in the editor just so you you know everything while editing here when you change the animation in the code or you change it here in the editor you're doing the same thing right you're changing the properties of the note this is just a visual representation of what your code is basically okay so you see the character looks towards the right if we turn on the flip h it's going to look left so we can change flip h would be would be like upside down exactly like that which you can use to make weird things okay so flip h if i show you here um in good ol in the inspectors i'm telling you this is the variables available on the player animations node like the variables velocity and all that you created now they are presented with nice capital letters but inside the code you can see property is flip underscore h right so you will have to type flip underscore h to change it and then this is a checkbox this is a particular type this is a particular kind of values called booleans there are two possibilities for that when it's on it corresponds to the word true when it's off it corresponds to the word false right so boolean can be only true or false it can't be a number one two three four those kinds of things so back to your code now what we're going to do is when we move to the left we're going to flip the character when we move to the right we're going to unflip the character so you can start with the left perhaps you add a new line inside the condition like that and you're on the inside yeah inside otherwise it's always going to run all right um and then you can do the same get the note player animation okay like the one before getting this call node player animations and then after the parentheses a dot lip let me see flip h equals i told you true is going to flip the character fast is going to not flip it so what you use here when moving is this true yeah okay now you can duplicate the line with ctrl d ctrl d the shortcut okay okay listen yeah and then you can move it without up and down like that so alt up a couple you keep i'll press and you exactly so now when you're moving right false you set it to false but is this necessary yeah because once you set it to true told you the computer does exactly what you do so it's going to make it look left and then it doesn't do anything else it keeps it going yeah uh you you don't have to delete you can press ctrl k for comment so this is okay it's like committing for a while uh exactly a comment starts with the hash sign here you can see the line becomes gray godot will just ignore it when it's like that okay now uh yeah you can comment it and you can try the game i see okay yep then right oh yeah it works yeah so now you have the animation uh working let's see what time it is we have about 30 minutes i think so well i can keep going for a long time i just need water because i'm going to die what can i give you to do now uh in the meantime one thing you can do if you want and i'll just show you that while i go get when you select the ground here yeah you can draw things so you can click and drag and you can draw the brown here you can select the water and draw water so just to show you the water is in two parts the ground has this magical feature that allows you to automatically form platforms like that it's called auto tiling the squares i plus place are called tiles and auto tiling means based on some rules change the the look of each cell right and click left to draw right click to erase and you can select things like those so typically you will draw some ground and you can place some fences above some signs which you can pick like that or something to make it play uh yeah we can do that afterwards i'll go a bit quickly over how can i move if i want to move uh middle click and mouse middle mouse click sorry and drag and another thing you can do i'm going to expand the view and you can click output here to fold that click okay and middle mouse click and drag to move the view and it's infinite you can where are the bits which things background you select here this is your palette yeah and you can click and drag if i wanted to like the background yellow the background the background you don't have to worry about it it's going to move with the view oh okay so the background is always going to fill the entire back of the screen and how do i do it right click ok i'll let you do that i'm gonna get some water do you want something no thanks okay so no thanks i'm just setting things like that you can control shift click and drag to make um rectangles huh click and drag ctrl like this let me show you okay ctrl shift you can see i hold them click and drag and it makes a rectangle controller control shift click and drag oh okay and you can yeah and then we're going to pass through the code let's see um my stuff many people are mostly chatting um not too many questions okay pretty good that's pretty crazy there's been almost up to three 250 persons at some point all right okay i'm good yeah i think so okay so now we do the flying things to wrap things up okay i'm going to remove the expanded view and uh what we're going to do is just have well you had some jewels there now i'm going to move them up there and now to get them well you can't because you can't jump or anything so uh everyone instead of teaching uh to jump jump and fall we already have the gravity jump it's just making the thing go up you use the arrow to select things in the view like that yeah and you can click and drag and move them as well we decided to go with a flying mechanic this is mostly for teaching purposes because um then you can introduce some more back to math and more people who do a bit of math that's called it's good except we're going to be a bit short to explain everything so we're gonna have to go a bit fast delete delete too okay all right you can zoom with the mouse wheel like that it's not a line but it's fine it's fine it's fine okay align technologies that's the company she worked at before okay um so in our player code we're going to add some code to make it fly we'll see if this works i have no idea but we'll try to make it work so the way we're going to do this is um aha we need to change the controls when the character is flying okay so this will be a bit trickier than than i thought but i'm going to to do that okay so one thing you can do i'm going to add a variable called my speed so it flies a bit faster um than it walks on the ground so uh 500 okay and now we're going to have controls but allow us when the character is in the air right um to to fly like that right you can change the direction you're going if you want to arc like that you can and you're going to do that with the joystick okay now in games those kinds of things um work like that you have some code that makes the character move on the ground but now in the air you want to change the controls a bit so you're going to need big conditions so um we're going to complexify our code to have kind of two modes if the character is flying all right it should be flying we're going to have applying controls if the character is on the ground or just falling something like that we're going to have the ground controls and the way we're going to toggle that is by pressing the key so with the character working to make it jump maybe with a right to jump initially uh and a again to fly and then you can do this to move in the air oh perhaps we can try something like that um so there are many notions i won't have the time to explain in detail right now but let's get that done um perhaps this can be a bit more a mix of you code a bit and i do a bit of code as well as a demo to save some time because i have to figure out some things that i didn't think about in advance so the way we're going to make the switch here if we don't have time we can make it jump only jump jump is very it's very easy yeah ah don idiot is here uh i'll be right back okay go ahead okay i'm going to do the the demo our landlord is here so um so the the way the uh while going to do that is by introducing two functions i think that would be the easiest way to do this uh we're going to have a move on okay and then we're going to um [Music] we're going to have in physics process two different conditions to manage the two different states right uh and then we need to pass in the delta i guess i'm going to explain it to her when she comes back and we're going to have another function above that which is going to be function y all right so what i did is adding two functions right um so you can do that you can define as many functions as you want in the code and we have one called move on the ground right or this is we could call it i'm going to change it to platform uh movement platform and move and we're going to sorry what have you done i just took the code you had in physics process i moved it to a new function called platform and move right so that will make it easier to see the the change in state if one in this condition we do platform and move if we're in this other condition we do fly right okay so it's like splitting physics process into two to make things more readable you can create new functions like that with a fun keyword um i define the function as taking the delta parameter as input and we pass it when calling the function so basically it's like we define platformer movies this yeah you see when we call move and slide and we pass the velocity et cetera well this is how we define a function that we can call except move and slide is inside the good engine and platform and move is when we just created inside of gd script you can see the definition here i'm highlighting okay um so we're going to have platform remove and we're going to make the character jump initially so inside platform and move this is still platform and movement the way we do that is with a new line we're going to check if the player i think i don't have a jump input in there like it's called move up but we're going to call it jump i'll just create a jump action with the you can see roughly how this is done so w key uh on the keyboard and uh joy button the a key on the xbox controller there you go so now here after applying our velocity you can use a similar condition to this one if the action press is jump then we're going to change the velocity we're going to make it point up right so you can go ahead and do that you can put election press jump uh press jump yeah column at the end and then we do we're going to give a vertical velocity so how do we do that we're going to make the velocity go up how do you think we would do that so direction point x equals 1 no no no no x is horizontal and we need to change the velocity point y no the velocity velocity dot y is going to be equal to ah velocity dot y yeah because we want to change the vertical velocity is equal to and what kind of value would you use to have the character go up negative minus one no minus one is very small it's a velocity it's a speed in the up direction so what kind of value would you use to make it go up a lot it's minus something minus gravity you could do that it would be a big value but minus whatever you want minus minus velocity that y no no no no no the problem is if you reverse it if the player just starts falling down then it will just go up a bit and then it will fall down again right okay uh you want the jump to be a fixed value in games for the player controls so player know exactly what to expect so okay what we can do is uh minus 800. and once more we can create a new variable and it's going to be a jump string there you go because it's a bit of an arbitrary value it's immediately we'd set the velocity sorry jump strength is going to be minus at 800 because we need to move on the y axis and we're going to put it in there okay and now does it jump yeah but it's going to do something very interesting i'll let you test so you can press a to jump oh okay and press any time oh no i can actually kind of play and now yeah that's the way to fly to explain what's happening you know you can jump anytime right now we're not exactly flying we don't have a fly code there but i want to show you a couple of things uh with that so while this is fun uh this is still a bug there are a couple things that are happening first the condition says if we press this action anytime we make the character jump so if i were to lower the value here and i keep the a pressed it keeps going up basically our code makes the character go up whenever we press the a button right so in a sense it's kind of like flying i didn't think of it but it's an easy way to make the character fly um so minus 500 so it doesn't jump too high going back down the code to where we were we need two things first there's another function called is action just pressed that is going to say if you keep it pressed it doesn't keep registering as true it's just the moment you press the button until you press it again okay now this introduces another problem i can jump see now i can't keep down right yeah but i can jump as many times as i want and that's not what we want so we need to complete our condition when you have a condition you have if condition but then you can combine multiple you can use the end keyword to say this and that have to be both true or you can use the all keyword the left or the right condition have to be either true we need the ends keyword here after the jump so i'm going to let you you put the space and space before before the column yeah that's still a condition definition so end and withdrawal and you're going to call there's a function on the kinematic body 2d called is underscore on underscore flow on this call on there you go and now you can play the game and you can only jump if you're on the floor pretty cool right yeah so that's your typical platformer one thing i want to mention here you remember how at the beginning we put the up direction well the is on floor as i told you you need the up direction to be correct for this to work basically that's how you tell godot this is the floor's direction it's the opposite of the up direction right anyway now this works i'm going to do another thing um we're going to have another condition to trigger the fly mechanic if um uh so this is nice and error no it's okay it's it's fixed already okay it goes pretty fast as you type code it tries to make some checks to warn you if something is incorrect uh the way we are going to do this um okay we're going to add a new variable called is flying and it's going to be equal to false by default i told you it's a boolean it can be true or false and we're going to change this now we're going to say if the character is flying we call the fly function otherwise we call the platform and move function is already no no it's in there i i wrote it earlier and actually i need to put the delta uh in here and in here so we transfer the delta from physics process because while splitting physics processing two so we're on the same information okay um now the character is never flying for now because it's always false so one thing we can do is change the value is flying based on the character's state so if the character is not flying so you say not is flying and uh not is on floor you put the nut before to negate what comes after it right so if it's not flying and it's not on floor and the player presses the jump again you can put as many and as you want input dot is action just pressed jump okay we have these three conditions we can say is fine equals true so this will make our character fly now flying doesn't do anything but we have a question what is it not displaying if flying is false so you you could write it like that actually i wrote that this is the way we typically write code but equal uh to check for equality is two equals because one equal is to assign the value to three clauses check for equality is flying is equal to false right you're asking the question we do that because the equal single equal is already taken for assigning the value to the variable right so we have if those three conditions are true right if all those three are met we're going to make the character fly and if it's flying it's going to call the flight function otherwise it's going gonna do the platforming but i didn't understand why if he's flying false because if it's already flying uh you don't necessarily want to make it fly well i guess i guess we can remove that it's not going to be a problem right yeah if you want it's going to start conflicting when we add the rest of the code as well going a bit fast i'm adding those things this is a safety check to avoid bugs basically uh and otherwise uh else if this is uh a check no we can just put in we can stick with the if's phone now alif is run try this condition if it's not true you try this one and then you have else left right so well i can show you that alif is flying um so you don't need to check for equality with boolean variables uh variables if it's true basically the what conditions do uh here when you say input it's going to be replaced by true or false and all conditions work like that right so not is on floor is is on floor is going to say true or false and we negate it to say the character is in the air right yeah uh so same thing when i said not is flying is flying might be true so we negate it to say only if the character is not flying right we do that so we can say it is flying if the variable is flying is true uh and we are going to do that and that all the character touches the floor i guess or is on floor like that right so when the character is flying if it touches the floor goes back to platformer movement if it is flying and the player selects to stop flying by pressing the jump key then we go back to not playing all i'm going to say is flying is equal to false right and then in flying mode so we can jump we can keep that and if i press a again it's in flying mode right now there's no flying code so the character doesn't do anything now how do i go back to normal mode press a again i'm in platform movement so there you have a code to switch between two general behaviors we call them states typically in games and you can press a to fall back on the ground if you'd like um the next step is going to be uh to write that fly function so this one if you don't mind uh because we're getting yeah it's almost two hours i'm going to write it for you run you through it very quickly uh and we'll close here okay yeah so can i can we exchange chairs or you can look like it okay it's just i don't need to be fully but i need to be more in front of the keyboard so this is more demo like right when we fly we're going to use a math uh equation to make the character move smoothly in a direction so it's not going to move exactly where you put the stick instead it's going to smoothly turn towards that okay it's called the steering behavior and this uses vector calculations to make if the character the player wants to move like that and currently it was moving like that instead of instantly going there it's going to do like that right so we use that with vector math the way we do it is we have our two vectors current velocity the velocity we want we call it the desired velocity and there's a difference between the two it's a vector between those two points here so what we do is we calculate the difference and we add a little bit of it so that makes every frame because it does 60 times per second it's going to do that right okay so that's what we're going to do i'm going to create another variable called direction note here the direction is local to the platform and move function so what i'm putting here is independent from the other function this is going to be a vector 2 and a value 0 0. now we can calculate the thing like this so direction dot x is going to be equal to input.getactionstrength like that of move right minus get action strength of move left this is a special feature that takes into account if you just press the stick a little bit the value is not going to be one like we do with the direction instead it's going to calculate the amplitude of the joystick to make it so if you use less joystick like that the character is going to move slower or turn slower right so we can do the same for direction.y it's going to be get the action strength of move down minus move up right and so this is going to calculate a vector that is going to turn like that and i need to do one last thing called normalizing the vector because a direction is what happens is if you move right and up so in the top right the problem is the x component is y the y component is minus one right and the vector is then longer than one right so it means when the player moves diagonally if you don't normalize the vector it's going to move faster in diagonals i think i showed you once in the game forager but you could do that when you went diagonally you could move faster because they didn't use that thing very often we want to do it normalize just says the vector if it's longer than one we reduce it to one or we we make it so the vector is always of length one which is exactly what we want for directions okay so we have the move direction now when we are in the air uh we're going to to define a variable that's the steering behavior the equation i was talking about so the desired velocity is equals to direction times the flight speed so this is a bit like the direction time speed that you had here but here it's like this is where the player wants to move this is what the input tells us but we don't want to go there immediately so we're going to do something called steering we create a new variable and it's uh desired by velocity minus the current velocity right so if the character is going at that speed in that direction and want to go there then it's the vector that goes from this one to this one right then we're going to add to the velocity one thing i didn't get to show you there are many shortcuts in code there are many things you did that you can do much with less code faster one of the simplest ones is plus equals to add to a value down below you had to do velocity.x equal velocity dot y sorry plus gravity times delta or you can change it in velocity.y plus equals gravity times delta this is a nice shorthand velocity.y velocity is going to we're going to add a portion of steering so we divide it by an arbitrary value the higher this is the more drag the more inertia the character will have the lower this is the faster the character will go right um and so the last thing we do is called move and slide and we're going to pass in the velocity and the up direction now right it's a bit weird because we don't have gravity applied so one thing we can do with that is apply a little bit of gravity i'm going to copy the line you have here velocity dot um okay i'm going to add a condition like this if direction dot y is not equal to minus one or uh sorry i need to do it like this if direction dot y uh is not equal it is uh lower equal to zero so if the character if you are pulling the stick down right here right we're going to apply a portion of gravity so gravity divided by uh 3 excuse me i'm used to using those numbers here with the the point didn't get to work on them today but okay so see it's falling a bit so we're applying a bit of gravity and now we can move the character like that last thing we can do something pretty cool we can get a character and i'm going to show you get node because we use it a lot and godot has a shorthand it's the dollar sign i'm sorry i have a question why it's minus here like why what is minus the sign uh excuse me that's a mistake on my end uh greater or equal to zero you're right um player animations dot rotation is going to be equal to velocity dot rotate angle i think it is variable so we're going to turn the sprite to look towards the velocity and you can test this and see you can jump so when you're flying you don't have yeah you can see okay so this let me explain what yeah it looks a bit weird we agree there you go that's a fun bug the reason is angles are based on conventions so the base angle is looking to the right in godot an angle of zero is like that the same as in trigonometry with the circle if you if you remember that so angola zero is looking right but our character the sprite is looking up so when we change the angle the problem is it's offset by 90 degrees right so we i need to after offset that and the fun thing is angles by default don't work in degrees they work in radians which is something do you remember something like that from math class it's a circle with a cross and we say the angles are like pi divided by two oh yeah that's angles instead of degrees those are in radians those are based on the value pi so we need to offset by pi divided by two let's see if it's correct because uh there you go and now and when it falls on the ground so while you are in the air ah yeah i know why this is not working well you need the game controller so now the character won't fall yeah it definitely needs more gravity so the gravity divided by three was unnecessary okay wow that's because the searing is overriding that so now you can fly and you can go collect all those things right and when you fall on the ground i just have to do one last thing or i'm going to let you do that okay when you go back to the ground when you stop flying we need to reset the character rotation to zero we need to get the player animations node dot rotation and set it to zero and you're going to do it here when we stop flying you can use the dollar sign to get the node it's here yeah shift dollar and this is like calling the get note function this is just a shortcut built into the language okay so you can get the player animation node dot rotation equals to zero we reset it that will make it so when you touch the ground or you stop playing like it's not if it's if flying is false um basically no it's this is the condition to say we were flying but we don't want to fly anymore so we set flying too fast and we reset the rotation now you can play the game with the gamepad and grab all the gems now so you don't jump many times right when you press a you are in flying mode and there you go the gravity in the air is not perfect right but uh it would require a bit more code to do that do you have any questions about that code or you know the logic i i put in there anything no it makes sense overall cool um so for everyone watching uh that code i'm showing here here the steering behaviors um take some learning but they are very very powerful and we have a free tutorial on the channel on the website on the channel if you look for steering in the videos you will find it uh how did it go for you well yeah okay it's nice proud of you oh you did you did a very good job you learned very fast um but um yeah uh so everyone i'm gonna go over really briefly the kickstarter that we have going uh right now well it went up again uh we're running on kickstarter to make uh calls to learn to code from zero with the go to game engine um this is a paid call someone asked me about precision because i know this can be confusing the course is paid but as usual you know we make free content you know it on the channel on our website on github and we contribute to goodwill we do lots of stuff and uh we do that thanks to those projects and paid courses right those are what allow us to also make other things and of course if you want to a very good course you need to spend a lot of time on it and that takes budget and all there are other persons in the team um you can find a link in the description to check all the details of the campaign actually what did the computer result the screen turned off okay yeah excuse me i'm just going to put back the chat all right and so uh as part of this project we're doing something for the godot community this is an app that is going to be free and open source because we reached our stretch goal of 50 000 euros this app is going to allow anyone in the community and any teacher to make interactive courses to learn the g script programming language you have a mock-up i can show you i can't show you that the technology uh works but it does right now because it would be a bit complicated how do i add a new instance of godot there we go and go to live editor uh well just in the process of making all the interface so it doesn't look like much but there's quite a lot in there where is the interface uh code edits uh is there uh the lesson panel and those kinds of things so if i go to a lesson here you can see there's a lot of stuff what's happening well well in the process of working on it so while breaking things every day this is i know why i know why this is happening it's because we are using godot 3.4 to make it applications go to 3.4 but don't open it here we are going to have errors there we go lesson.tscn and there we go it's such a mess right now uh we're in the process of working on the interface you have a code editor you have a game running you'll be able to do interactive assignments in that my teammate is working on this in parallel right now um so yeah we focused on the technology fast and now while making the interface and we'll make it free and open source when it's ready of course it's going to take a while with a free course to learn the g-script language to attract new people to godot so you can check the campaign link in the description below consider becoming a backer to help us make more to help this project get better you know um every pledge counts and with that uh that's the end of the the session for now so um uh question does gd question more good quest on game dev quest initially it was game design quest but it's whatever you want game dev quest would be the thing uh so there you go while also making a free and open source game uh community driven um uh you can see the previous video was about that just a two minute video and uh we'll make it community driven the idea is to turn this into a roguelite uh eventually this is the final project for this course but it's also something we want to to build into something larger uh yeah what do we have here uh if you want to play a bit while we say goodbye um thank you everyone for watching uh if you have any questions you will also find a link to our discord server in the video description and with that thank you kindly for watching be creative have fun and we'll see one another in the next one bye-bye
Info
Channel: GDQuest
Views: 19,925
Rating: undefined out of 5
Keywords:
Id: KZmKoBdzzYg
Channel Id: undefined
Length: 130min 33sec (7833 seconds)
Published: Wed Oct 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.