Code Your First Script with Godot and GDScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will get to create your very first script with godot and the gd script programming language we'll start by making a character turn in circles and then we'll add input support to make it a nice little toy kind of ship moving forward or in that case really the good ahead this is for programming beginners well i'm going to write only a few lines of code and i'll run you through all the instructions now i don't expect to know programming after 15 minutes it will take much much more time than that and perhaps some of those things will be a bit challenging like you might not remember everything at the end of the tutorial but the point is to have you write concrete code that already leads you to game creation you will be rehearsing reusing and building upon this knowledge in the upcoming videos this is part of a complete free series to get started with godot in 2021 if you haven't already i invite you to watch the series from the start you'll find a link to it in the description below with that let's get coding let's get started by opening godot and we're going to create a new project starting from a clean slate so i prepared an empty directory which is a requirement for your projects i'm going to paste the path in the project path here also set it to opengl es2 as it's compatible with more laptops and things like these we end up on the 3d view but we want to create a 2d game so we're going to create a scene with only a sprite node i invite you in the scene dock to click on create an other node and we're looking for sprite here so you can type it and press enter to create the sprite currently the sprite has no image and it's stuck in the top left corner of our viewpoint so we're going to select the node and to assign an image to it we drag the icon.png image file onto its texture slot now goto will appear with the select tool selected it's this one in the top left of the viewport we're going to click and drag on the character to kind of center it in the view all right so that's our scene setup done now we can attach a code to that sprite node that's only displaying an image to animate it to do so we first need to save the scene so we're going to press ctrl s or you can go to scene save scene and we'll save it in this empty directory our project root as sprite.tscn you can see it appears here okay so with that we can add a script to our sprite to do so you can select the node and an icon appears in the top right of the scene doc you can also right click and select attach script this opens a little pop-up that gives us a few options as to how we want to create the script first the programming language we'll use gdscript the default the inherited class by default this will be the type of the node you created that's what we want the template by default it's default which has a little bit of boilerplate code you can create template files that you can build from if you reuse a certain structure all the time in your code you'll want to create a template we'll use the empty one which is as the name suggests empty built-in script allows you to save the lines of code inside the tscn file we created the default however is to create a separate file in this case sprite.gd which is what we want here so we'll click create this takes us to the script editor and first on the left you can see the list of open script or documentation pages i should also mention that you can use ctrl shift f11 to expand the script editor and you also have a button to do so in the top right of the script editor here the first line extend sprite tells us that the script has access to all the features built in the engine on the sprite class or type you can control click on the name to open its documentation page right the code reference sprite inherits from four other classes so it also has all their features like that of note2d the position rotation and scale and it also has all the ones defined and listed in this page the properties and functions also called methods in that case all right so i'm going to right click on it to close the sprite page and we can get coding we want to move our character smoothly on the screen and to do so in a game engine we have to update the sprite every frame a game runs in a loop with by default 60 images calculated per second and the way you move something is every frame every 1 60th of a second you move the character a little bit to do that in godot you have a function built in that's called process and you use it to tell the engine this is how i want to update my character every frame we define a function with the func keyword and then you want to type the name of that building function process underscore process you will have an auto completion suggestion that appears you can press enter to accept it and save some typing when you press enter you see that the cursor returns not at the start of the line but after one tab character in gd script tabs are really important they define blocks of code so when you define a function with a colon at the end you then need to add one tab one indentation level for godot to know that this is the body this is the instructions of the function if you don't do that and you try to type some codes like rotation plus equals three for example you will get the red line that appears and stays and an error at the bottom that says indented block expected after declaring the function this means that your indentation level is incorrect so you need to add a tab by pressing the tab key at the start of the line and you'll see the error disappears all right first we're going to make our character rotate so we can add a very little value like 0.05 and so to add some angle to the rotation we first type rotation to access the rotation property then we can write equals rotation plus 0.05 or use the shortcut i used before write plus equals why that value of 0.05 it's because rotation is in radians in the engine radians are based around the number by 2 pi radians represents 360 degrees okay once you've done that you can press f5 and we have to define the main scene for our application so we can select double-click and run it and you'll see the character turned that's a start now that hard-coded value is not ideal you know we might want maybe something a bit more descriptive than 0.05 to do so we can define a variable or property these properties you define them outside of this function typically at the start of your script you can define one using the var keyword and you can start to see a pattern where built-in keywords are in red by default after that you name the variable in that case we're going to call it angular underscore speed and we're going to use the equal sign to define a value so our angular speed will be we could say 3.14 this is an approximation of pi or you have the built-in constant pi the constants that are defined in the gd script language are written in red and this in the game when it runs will be replaced by a value 3.14 and lots of decimal numbers after that so now we can replace this line by pi and as our value is now a speed its units are radians per second and we want to multiply this by a time value to turn it into a delta rotation a rotation this frame you can notice that the function process has something in parentheses it's called an argument it's a value that the function receives in this case delta is the time interval between two frames and you can use the value to make your code time dependent so in here we're going to multiply pi by delta to turn our value into a rotation offset we want every frame why does that matter because the time between two frames is not equal it changes a little bit from frame to frame so by using the delta value we ensure that the rotation is consistent and depends on the time that elapses in the game rather than being applied per image in particular you know you've seen when games slow down if you don't use that delta value everything will start to move slower you can now press f6 to see the character still rotates roughly as before okay so now we want it to move and to do so we can change the character's position so we can type position again we want to add to the position so we say plus equals and then the position uses the type vector2 it's a vector with an x and y component we want to add a vector2 to it actually we must add one to do so you type the type vector2 and you open parentheses to create a vector2 and then the engine tells you you need an x value it's a float in other words a decimal value and you need a y value also a decimal value so we could say 1.0 1.0 every frame we add that and we're going to see f6 the character now moves down diagonally so note a convention it's that the x-axis is positive to the right in a game engine and the y-axis is positive going down that is unlike what you saw in math where the y-axis points up so you have to keep that in mind so now once again we can turn that into a speed to do that we're going to use bigger values in here like 100 and 100 you could think of it as a value in pixels per second and then we're going to multiply that by the time delta then the character moves still diagonally but now we don't have that frame dependence problem it's now time dependent next up we're going to change that a little bit we're going to use a speed property so we're going to name the the thing that moves the character speed to do that we go back to our angular speed here and we add a line return and we're going to write a new variable named speed we're going to set it to a value of you could say 400.0 400 pixels per second it's not a vector right so now we're going to change the code that updates the position a little bit instead of having a hard-coded vector we're going to use one of the vector2 constants that exist in godot to have a direction which will multiply by our speed which will multiply by the delta time this is a more appropriate way of calculating move direction in games as you'll see moving forward to do this we first so select everything you had here you're going to delete it and then type vector2 dot we're going to access the properties that are defined in the vector2 class and you can see that you have a few so axis x and y you have down left right up you can use the up and down arrow key to navigate these suggestions and we're going to select the right vector so the character will move to the right we'll multiply it by speed times delta so just to show you here vector2.right is equal to a vector2 with a value of 1.0 on the x-axis and 0 on the y-axis but it's a bit nicer to have a name for it right it's the same for speed the point is to understand the meaning of the code while writing and to help your future teammates also read your code so this will make the character move to the right now we're going to go one step further and have the character turn i'm going to create a new variable inside of the process function for that we write the var keyword and a name once again we're going to call it velocity this variable is different from the ones outside the functions the ones outside the functions we call them properties because there are values attached to our sprite script and that are available in any function but when you write a variable inside a code block like our function then it becomes a local variable it is a value that's only available within that scope the velocity here is only available in the next slide like when we change our position we're going to set it to vector2 dot up so we're going to start with the up direction it's a vector pointing up and we're going to rotate that vector to do that the vector2 class i go to its docs page if it wants to open i'm going to click on methods in the right side to jump to the nodes methods and scroll down a bit we're going to use this one rotated the function is going to take an angle and it's going to return a new vector rotated by that angle let's go back to the sprite script to call a method on a given object like our vector2 we add a dot and we type the method name this one is rotated again can press enter to complete and then we need to pass an angle this angle will be our rotation this will allow the character to move in the direction it is looking we'll see why in a second and i'm going to multiply that by the speed value for now while just calculating that value and storing it in a container or assigning it the label velocity but not moving our character yet to do that we have to replace the calculation here we'll add the value of the velocity variable to it multiplied by delta to explain that why we call it velocity why we multiply it by delta a velocity is a vector that represents a direction and a speed together to then turn that into a motion every frame we have to multiply it by time value so the velocity is going to be in pixels per second and we multiply it by a value in seconds to turn it into a position offset so you can try now to see your character turned in circles you might be a bit puzzled about this part why it makes the character rotate it's because every frame we increase the character's rotation so we change it then we start from a vector pointing up and we rotate it by the current rotation and as its rotation its angle is changing it's looking in a different direction so it will move in a different direction that makes it move in circles okay so now i'm going to introduce input we're going to listen to the keys pressed on the keyboard and make the character only move if the player is pressing a key let's start with the rotation so first i'm going to turn the two lines that move the character into comments by selecting them and pressing ctrl k you can see a dashed line gets inserted at the start of the line and they become grayed out they will not run anymore then we'll start with just the rotation so to do so we're going to first define a variable we'll call it direction and it will start at a value of zero and what we're going to do is we're going to use conditions if the player is pressing a key then we will change the direction and we'll use it when adding our rotation you'll see how in a second but first let's see how we listen to input in gudu i'm going to show you one way today we start by defining the condition with the if keyword then we write the condition the condition is going to be input a global object available in godot that has many methods one of them is is action pressed this is the one we maybe use the most in godot and it takes the name of the action many of which are defined by default in godot like ui accept it's for when pressing enter or a on a gamepad but the ones we're looking for are ui left in that case we're going to start with that one navigate down to ui left and press enter to define a condition we write if the condition to evaluate and then a colon at the end and we're going to press enter which will create a new block with two indentation levels everything that's at two indentation levels like that is going to run only if this condition is true so only if the player presses the ui left action which is the left arrow key by default in that case we're going to change the value of the direction variable to do that we type the name direction with an equal sign and we write the new value and it's going to be -1 so i'll consider going to the left is a value of minus one i'm going to add the other condition to move to the right then we'll explain how that works okay so if input dot is action pressed and then we're going to go with ui right so we look for ui right at the end of the list in that case we're going to set the direction to 1. so direction will be 0 by default if the player presses the left key it's going to be -1 if they press the right key it's going to be one and then what we can do is when changing the rotation we can change this by angular speed i forgot sorry times direction times delta so it's going to be this value multiplied by this value zero minus 101 times delta now if we're not pressing left or right the value of direction will be zero so we'll add zero because of the multiplication by zero giving you a value of zero in that case the character will not rotate you can already test it by pressing f5 the character does not rotate if you press the left key it rotates left if you press the right key it rotates right or clockwise now this if keyword i told you it's going to evaluate what's after it a condition uses an expression that evaluates to either true or false you can see that in red because there are core values in most programming languages called booleans true false and so luckily that function input dot is action pressed returns true or false so at runtime this is going to become either true or false every frame when it's evaluated we press the left arrow key this becomes true and so this line of code runs otherwise it is false and this line of code does not run all right now we're going to use another input key to make our character move forward if we press the up arrow key to do so i'm going to remove the comments here so i select the two lines and press ctrl k and what we want to do is do something similar we're going to remove the valve keyword in front of velocity and define it first with a value of vector2.0 so we type variable velocity equals vector2.0 we have to remove the var keyword here otherwise we'll get an error we already defined the variable velocity we can't do that twice okay it starts at zero then we're going to use a similar condition so how is it going to to look here we want to check for pressing the up arrow you can pause and try it for yourself the condition is going to be if input dot is action pressed ui up we have to look for that one ui up replace a colon and then we're going to take our line velocity and at the start of the line we press tab to move it into the condition block like this in other words by default our velocity will be a vector that looks like this so vector 2.0 correspond to a 2 with the values 0 0 and if we press up then we replace that value with our vector that moves the character in the direction it is looking and we add velocity times delta to our position still so you can press f5 and if you don't do anything nothing happens you can turn left and right keys and press the up arrow key to move forward at your define speed and you can combine it with the left and right arrow keys and that gives you a moving character okay so this was your first script and that's a lot to take on at once you're going to learn more moving forward and you're going to rehearse and rehash these things we saw here in many different times in many different contexts so don't worry if you don't remember everything at once it really takes time now i just want to show you one thing is where these ui left ui rights and ui up come from and how you can see the list and even add your own these are parts of your project settings so they are per godot project to see them you go to the project menu project settings input map and then you have the list you can see ui accept ui select i cancel if we go down we have ui left right up and down and so with this interface you can define a new named action by entering its name so for example we could name one jump and then we click add or press enter and it gets added at the bottom but by default it is completely empty so you have to click the plus sign to add a key uh joystick joy button is a gamepad button like the x the cross the square the triangle and a ps controller for example y-axis mouse buttons so we can add a key here and then you can press the key you want to add and click ok so we can jump when pressing space we can add more as well like a joy button and by default it will be the cross xbox a nintendo b so we can click add here and so whenever you press space on the keyboard or a on an xbox controller this input will be considered pressed which you can check with the input dot is action pressed method call and that's it for this video that was a lot to take on but at the same time i wanted your first group to be something concrete it's almost a game character right that that could be in a game moving collecting things on a map so you can pat yourself in the back for already doing this and we'll do much more in the upcoming tutorials if you like this video first subscribe because there's more coming and then we have a complete free series on our website it's complementary to this one all you have to do is click the link in the description you enter your email and you'll get lots of game development insight again completely free you can unsubscribe anytime the links in the description below but with that i want to thank you kindly for watching we'll see one another in the next video to talk about godot's signals see you then
Info
Channel: GDQuest
Views: 30,617
Rating: undefined out of 5
Keywords: godot gdscript, learn gdscript, godot beginner tutorial, create your first script, scripting godot, gdscript, gdscript programming language, godot scripting, godot engine beginner tutorial, godot tutorial for beginners
Id: 4FGC0J3SlEs
Channel Id: undefined
Length: 24min 48sec (1488 seconds)
Published: Thu Apr 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.