GDScript (and Programming!) for Beginners in Godot 3.2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I just started reading the Docs for GDScript. This is perfect. Been watching tutorials online for making games and whatnot, but want to put more time into learning GDScript so I can eventually write my own ideas as opposed to simply emulate tutorials. Thanks for the post!

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/[deleted] πŸ“…οΈŽ︎ May 11 2020 πŸ—«︎ replies

thank you

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/billis2020 πŸ“…οΈŽ︎ May 11 2020 πŸ—«︎ replies

Definitely gonna watch this! Thank you!

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/CodeJBDA πŸ“…οΈŽ︎ May 11 2020 πŸ—«︎ replies
Captions
what's going on guys Tom here and in today's video I'm gonna be teaching you the basics of Gd script within Godot 3.2 so let's get started okay so before we start writing any code I'd just like to give you an introduction to GD script itself and give you a brief explanation of what it is and how it can be used to make games within the go-to engine itself so I'll bring up the documentation and this is the gota engine 3.2 documentation that's available directly on the website and you'll see here that we have GD script basics and here it explains GD script it says GD script is a high level dynamically typed programming language used to create content it uses a syntax similar to Python meaning blocks are indent based and many keywords are similar its goal is to be optimized for and tightly integrated with the Gotha engine allowing great flexibility for content creation and integration so what we're saying here is that GD script is a programming language a dynamically typed programming language which we'll get to a little bit later on in this video and it's very similar to an existing programming language out there called Python which is hugely popular and there's masses of resources out there on Python that will still be applicable to the GD script programming language itself which is fantastic because that means not only do you have all of the documentation here for GD script but a lot of the documentation that's out there for Python is still completely relevant and learning Python will be a massive bonus if you plan on using GD script in the one term ok so the next thing for us to do is to open up goda and I'll start teaching you the basics of GD script so we have got it open here it's version 3.2 and I've just created a very simple 2d scene with a world node and a player sprite in the middle of the screen there to create a scripting Godot there's a couple of ways the first thing we can do is we can simply right click in our file system and click on new script this will open the create script dialog and here you'll see that we have a choice of language we can choose native script GD script or visual script for this tutorial were going to keep it on Gd script and we have an inherits option you can leave that on node for now we'll cover that in a later video and we have a template option again we'll leave that for now and then a path for where you want to store the script now here I'd advise that you change the default name it's giving it from new script and we're just going to call it player GD and then click on the create button what that's going to do is it's going to create a player dot GD file in the bottom left corner and you can double click on that to be presented with goats IDE now I'm just going to bump up the font size here a little bit before we get started in learning some of the GD script basics so that you can see it a little bit better in youtube so first of all we're going to delete all of this and I'm gonna gloss over this extends node for now it's a little bit of an advanced concept and it's not something that's hugely relevant for this tutorial but it will be when we get later on in these videos so I mentioned before that GD script is a programming language now what that means if you're not familiar with programming languages is that we can tell the computer what to do by assigning things called variables and running things called functions now a variable is simply a piece of data that we want to store within the application now this could be a number it could be a piece of text it could be a true or false value it could be any number of things and in order for us to declare a variable in Gd script we simply type the keyword var var and you'll see there that that's highlighted in red now we give it a name so for this example I'm just going to call it number and then we assign a value to that variable and to do that which i equals and then we give it the value we want so here I'm just going to assign the value 5 and there we've simply declared a variable a piece of data that we want to store something in we've given it a name number and we've assigned it the value 5 now we could do the same for a piece of text we can save our message equals and then to assign a piece of text we have to wrap it in quotation marks and we can say world which is something you might see if you're new to programming you'll see this all over every single application starts with hello world now I mentioned before that you might want to store a true or false value now you can do that by saying var and let's give it a name like alive equals true now if you wanted to say that the player wasn't alive you could simply change that to false okay so that is the basics of variables and variables come in really handy when we want to pass data around our application or in this case our game and we want to modify value so for example think of a score that you might want to increase every time the player does something or you might want a variable to like the one we've just declared say whether or not the player is alive or dead or perhaps you just want a piece of text also known as a string in programming languages and to display a message to the player whenever they achieve something for example a little pop up on the screen that says you've collected a coin or something like that now variables are great but on their own they don't really do anything and in order for us to use these variables we have to use them in what's called a function a function is a chunk of code that runs and will do some sort of action it might produce a value it might add two variables together it might call another function which then does something else in Gidi script every single script has a function called underscore ready which will run as soon as this script gets loaded into your game now to define a function we simply say func and then we give it a name so in this instance I'm going to define that ready function so that we can start seeing some of this script come to life we're gonna say underscore ready and you'll see here that it's given us some form of auto completion here which is really really helpful as we start to get more familiar with this and you want to start bashing out script really really quickly the auto completion is fantastic for that you'll see that it's requesting some brackets here when we open and close these brackets and we end the function definition with a colon that tells Gd script that we're ready to declare the trunk of that is going to get called whenever this function runs we're gonna press ENTER here and you'll see that it's indented slightly it knows now that we're inside a function and it's indented that to identify that it's within this function itself now you'll remember from the first part of the video where I should opened up the documentation then this language is based on Python which means the white space ie spaces and tabs are extremely important in this language they get interpreted by the engine as we are inside this function now if I was to say func something else here then because it's not indented inside of this function ready we know that it's outside the scope of the ready function and it's a completely different thing again if I was to say var something equals 5 here where it's not indented then it becomes a global thing just like these ones here however if I was to indent this this something variable now becomes part of the function and can't be accessed by things outside of it okay if I press ENTER now you're gonna see that it's indented automatically and that means that we are inside this functions definition and we're ready to start writing code that runs exclusively inside this function before we start writing any code I'm gonna introduce you to a fundamental concept within programming within any programming language and that is comments now comments are really useful if you want to sort of keep track of what you're trying to do in the code or if a piece of code is particularly complex you might want to use comments to explain what it is you're doing you might wonder why you're doing that if you know you're the one writing the code and you you think you understand what's going on but believe me comments are invaluable six months down the line when you come back to a script and you you think what was I write in here what was I trying to do comments can be really useful there to write a comment we simply enter a hash and now we are write in a comment so here I'm going to say print the string and remember string means a text a piece of text so print the string hello world to the output window and you'll see the output window is actually down the bottom here so what we want is we want to print the string hello world which is luckily for us stored inside this message variable we want to print that out to this output window here to do that we're going to call another function now Godot has a ton of functions declared that you have access to within any of your scripts we won't cover them all today because there are literally hundreds of them however there are some that are really really useful and one of these is the print function so we simply type the word print you'll see it's gone red there it's identified that it's actually a function we're going to open our brackets and now we need to give it a string so we could simply put some quotation marks here and say hello world and save that now before we do anything else we need to make sure that this script runs whenever we run our game so to do that we're going to right click on our player and we're going to press attach script and we're going to select the path to the player Gidi script and press load and now you'll see that this has a little script icon here that indicates that a script has been loaded to this and it's this script here so we're gonna save that and we're just going to press play here it's going to tell us that there's no scene being defined to run which is fine because I haven't set that up yet we're going to choose our world scene and press open and now if we run this down at the bottom left here it's a little bit small but you can see that it says hello world so that's great so we know now that this script is being called from our game the ready function has been called because the game has noticed that there's a script attached to the player it's identified the ready function inside out it's been called by the game and it's seen the print call here and it has printed out the words hello world to the screen now you'll notice again earlier we defined a variable called message which contained the piece of text hello world the string hello world so we could simply replace this whole piece here with our variable name so message so now instead of having the string literally defined here itself we are now referencing a variable that we declared at the top of the script again if we save that and press play you'll notice that it still says hello world at the bottom and if I change this to hello world from Tom save this and press play you'll see that hello world from Tom has appeared at the bottom left okay so that gives you a very brief introduction on how to call an inbuilt function but how do we define our own functions well it's quite simple we can come down to a new line make sure that we're not indented anymore we don't want to be inside that ready function and we're gonna define our own function here so we're gonna say func and we're gonna give it a name now we don't need to put underscore in front of this that's a bit of a naming convention for private functions and I'll get into that in another video it's a topic for another time however if you're writing a function you don't need to put that underscore in place so I'm just gonna leave that out and I'm gonna call this function the add function I'm gonna open some brackets up and now here's where we can start doing something powerful now functions can take what's called parameters and parameters are things that that function will be able to access and then act upon inside the function itself so for example here I'm gonna say a and B we're going to take two parameters we don't know what they are right now however inside of this function definition you'll see what we're going to do with those two parameters so we're going to put our colon on the end always has to be a colon we press ENTER and you'll see again it's indented now I'm going to introduce you to what's called operators operators are symbols that you can use to perform a particular action to a variable and it's just like maths really so for example we could say we could declare a new variable here var result equals and then what we're going to do is we're going to say a plus B okay and then we're gonna simply call our print function again we're going to say print and we're gonna print the result out to the screen now we need to be able to call this function so instead of printing our message here I'm just going to comment this out and if we comment out a piece of code that no longer runs and I'm going to say add and we're gonna give it our original number so the number we declared at the top which is number this one here num number equals five and then we're going to give it another one we're going to say ten so we want to add our number five to the number ten and then it will print that out to the screen so let's press play and you'll see here at the bottom we now have 15 so we can define another function func subtract and I think you're starting to get the picture now so we have a and B and we can save our safari salt equals a minus B we're going to print that result out to the screen as well and we're going to call it here subtract and you'll see that the auto-completion is actually notice in the way our function has been written and it's giving us some auto-completion they're saying that we want in a variable and a B variable so we're gonna say 10 and 5 to run that and we expect it to say 15 and then 5 because it's calling both of those functions the ad and the subtract in sequence so we've got 15 and 5 okay so let's delete these because we don't need both of those on the screen let's keep it clean we're going to define another function this time I'm just going to call it operator and we're gonna say a and B now we're just going to use this to play around with the available operators that we have so let's call operator let's give it the number 10 and the number 3 and we're gonna play with a different operator this time we're going to play with the multiplication operator so we're going to save our result equals a multiplied which is the asterisk sign B again let us print this out to the screen so we expect the result to be 30 we see 30 down at the bottom here let's try changing the Asterix to a forward slash and see what that does let's press play we get the result three now the reason we get the result three is that this is the division operator and you'll notice that 10 divided by three isn't exactly three but we're working with whole numbers here so it's actually rounded it down to three and we have one final operator here that I'd like to show you and that is the remainder operator which is the % it's sometimes called the modulo operator or the mod operator but effectively what it does is it divides the two numbers and gives you the remainder so we're looking for one to be displayed on the screen here and indeed we get 1 because 3 goes into 10 three times and there's one left over okay so that gives you an introduction to operators and how they can function on a variable particularly a number variable in this instance and let's just see how an operator might function on a piece of text so on a string in this example if we change the result to be I am and then plus and then B so let's take away that a parameter and let's call this operator here and let's just say 30 we press play and we get an error now the reason we can't add these two variables together is that this here is a different type of variable than B so this is a string type and this is a number type or an integer type now we can do something called string interpolation here and what we could do is we could say percent D which says I am and then we would basically create in a placeholder for a decimal number and we're going to say here % now this is the remainder operator however when we're using it on a string what it actually does is it interpolates whatever variable comes next into the string if we say % B here and then we refresh this we're going to get I am 30 so you'll see that operators behave differently depending on what context they used in so for numbers the plus and minus and division all work on mathematical operations however when you're using it on a string for example the modulo operator or the remainder operator becomes a string interpolation operator which means that it will take variables that come after it and place them into the string wherever the placeholders have been put now that's a little bit more of an advanced topic so I won't go any further into that right now but I just wanted to show you that operators can indeed function in different ways depending on the context where they used okay so we've covered operators what I'd like to show you next is conditional statements so let's define another function here func and we're going to call this conditional and we're just going to give this a parameter here called a or let's actually call it age we're gonna put a colon on the end and press ENTER and now what we're gonna do is we're going to write a piece of code let's let's document this as we go let's put a comment we're gonna say if age is greater than 30 then print you are awesome otherwise then print you are cool I don't know why I chose those two words but there we go okay so we're gonna say if age is greater than 30 then we're going to print you are awesome otherwise we're gonna print you are cool and to do this we're going to use a conditional statement and a keyword the keyword for that is if which is exactly how we've put it in the comment there we're going to say if age now we're going to say greater than 30 so we're gonna put a write angular bracket there and again this is just like it would be in mathematics we're gonna say if age is greater than 30 we're gonna put a colon at the end here because we're defining a code block just like a function we're defining another block and you'll see it indented just like the function there so inside this we now know that age is greater than 30 and we're going to print you are awesome okay so how do we say well if that's not the case well to do that we simply write else and put another call on so we're saying if age is greater than 30 we're print and you are awesome else or otherwise we're gonna press ENTER its indent and again we're inside another code block so in this case you're not greater than 30 we're gonna say you are cool okay so let's remove this operator function here actually let's just comment it out by putting a little hash in front of it and we're gonna say conditional and we're going to pass in an age we're gonna say 31 so we're expecting it to say you are awesome we're gonna press play you'll see down below it says you are awesome we're gonna change this now to be 20 we're gonna press play and it says you are cool okay so we have that age is greater than 30 we're gonna do something otherwise we're gonna do something else but what if we had multiple conditions we wanted to check what if we wanted to say well if you're over 30 then you're awesome but if you're under 12 then you're cool otherwise then you are and it's just gonna print there their number let's just say that so we're gonna say if age is greater than 30 you are awesome we want to put another condition now we don't just want this to be everything else is you are cool we're gonna say L if and this is going to allow us to define another condition here so we must say L if age is less than 12 I think we said then we're gonna say you are cool so let's put that there we've just got rid of the else statement and then for everything else so else print we're gonna say you are and we're going to do that percent D again and % age so we're just using that string interpolation again assent d % age so let's try that again we're going to run it's gonna say you are 20 because we're not greater than 30 and we're not under 12 let's change it to 11 you were cool and if we change it to 33 we get you are awesome great so that is the very basics of conditionals now we could go a step further let's add a new function here just between the two we're going to say func we're going to call it more conditionals and we're going to say here alive and health let's give it two parameters called alive and health and what we're gonna say here is if alive and so we can use the keyword and to check something out so if alive and health equals zero now you'll see here that I put a double equals now the reason for this is that and a single equals would be an assignment we would be trying to assign the value 0 to health but we can't do that in an if statement or we don't want to do that in an if statement so to do an actual equality comparison we use the operator double equals so we're going to say if alive and health equals zero then we're going to print out you died so let's call that here let's comment out that line and say more conditionals and we're going to say we are alive and our health is zero we're going to press play and it says you died let's do a else print you are alive and then let's change this here to ten and press play you'll see that we'll get you our life however I could change this alive here to false and if I press play we're still gonna get you are alive now that's because we need to add another condition here what we could do is we could simply say at the top if not alive then we can print you died again and what we can do then is we could return from the function and what that will do is it means that once we see this return keyword here it's not going to run anything else after that it's basically going to call the print function with you died and it's going to do nothing else it's going to stop at that point so if we press play it's going to say you died now this isn't the cleanest way to do this we could actually modify this statement here to include this condition as well so we could say if not alive or and let's wrap these in brackets because we're doing two conditional checks here so we're going to say if we're not alive or we are alive and our health is now zero then we're going to print you died so again we have you died there now that's just a way of combining multiple conditions into one single if statement now one thing you'll notice here is that I've been calling these functions very simple names like operator and more conditionals and conditional but actually that's a really really bad thing to do because it doesn't explain what the function is actually doing so let's change this one here and actually what we're going to do is we're going to just change this back to the way it was so we're going to say a and B and we're going to make the result a plus B and we're gonna give it a name that makes sense so in this example I'm gonna say func add and now that makes sense this is what I was talking about with self-documenting code our function is called add so we can see that whenever we call that function for example add 10 comma 2 you know exactly what that's going to do that is going to take those two values and add them together same with this more conditionals what it's actually doing is checking whether or not you are alive so we can call this check if alive and again this conditional one here it's basically printing out something based on how old you are so let's call it something like age check okay so for all of these functions so far let me just comment that on up for all of these functions so far all they've done is printed something out to the screen now it'd be great if we could call add 10 plus 2 and actually get the result of that back out of the function now you'll notice before that we called a return keyword and what that did was returned out of the function and stopped the execution of any further code along the line we can use that return keyword to actually return a value from a function for example no longer am I going to print the result out from this ad function here instead I'm actually going to return the result and then up here we can say var result equals whatever the return value is from this function call and then after that we can say print result and now who run that we should get the answer 12 popping up in our output window which we do so you can use this return statement to return anything from strings to numbers to full object with nested values and that's where the power of functions comes in you can modify values you can tweak values you can create new values from scratch and use those return values to then pass into other functions okay so now we've looked at variables how to declare them and how to assign a value to them we've looked at functions and how to define a function and two return values from inside a function what I'd like to cover now is the different types of variables that you can have now you'll you'll have seen before when we were doing the addition you notice that we couldn't add a string to an integer so a piece of text to a number directly now the reason for that is that they are different types so we have a concept of different variable types now there are four basic variable types within Gd script those are cool which stands for boolean which means a true or false value there's int which stands for integer which means a number and numeric value we have float which again is like a number however it can have decimal accuracy so we could have one point five one point six three five and so on and then we have a string which is simply a piece of text now you'll notice here that we didn't have to declare what variable type any of these things were and now going back to the original documentation that is what a dynamic programming language means so it means that dynamically it will just figure out what kind of variable you have created based on what value you gave it in the first place so for example here this number equals five Godot has worked out that that is an integer value or an int value likewise for message it's worked out that it's a string value and for alive it's worked out that it's a boolean value now what if we wanted to explicitly tell Godot what variable type we wanted to use and why would we want to do this so for example here we have the func add a and B now when we type in add an open our bracket you can see that it's expecting a to be a VAR a variable we don't know what kind of variable just any variable just a dynamic variable that isn't really useful if for example this add function was to only be a mathematical function we only wanted numbers to be passed into this now we can do that by writing a colon and then declaring what kind of variable we want here so we can say in int again for this we can say that B is an int as well and now if we open our brackets again you'll see that it says a should be an integer now if I pass in a message here like a string and we say hello and then we give it a number afterwards and then we save that you'll see that immediately we've got an error message popping up here it says at add call argue one is assigned type string and it doesn't match the functions argument type which is integer now you don't have to do this you don't need to explicitly tell Godot what kind of variable you want inside functions however it can be extremely handy because it means that will immediately get little pop-ups here or notifications that something in our script is incorrect and it can stop your game from crashing later down the line because you've had a chance to identify bugs before they actually happen now we can do the same thing for these variables at the top here we can simply put a colon directly after the name we're going to say an INT after this one we're gonna say string now notice this is a string with a capital S and after alive we're gonna say bull so let's save that and let's just run it again in fact let's call the add function to make sure that everything is still working we're gonna save that and we're gonna press play everything's still working we've got 12 there let's pass in our number so we've got number and 2 let's press play we expected to say 7 and indeed we get 7 at the bottom there so these are the basic types within Godot we have again int strings Bulls and floats which you haven't seen yet so a float is simply var let's call it time delay I don't know I'm just picking a random name we're going to assign it a float and this could be for example the decimal so we could say 1 point 5 2 6 5 for example and that is a valid value for a float so we've seen the four basic types of variables here however there are advanced types and you can obviously define your own types which again is something for a later video however I did just want to quickly show you what an advanced type might look like now a lot of the time in games you'll be dealing with positions and to do that you'll be working with things called vectors now I'm not going to get into vectors and what they are in this video however just wanted to quickly show you what it looks like when you're working with more advanced types so for example at the top here I'm gonna save our let's say var target position and we're going to give this a vector to type and we're going to simply say that it is a vector 2.0 now don't worry about the assignment here that's basically just assigning it to a zero vector two and again we can say var target 3d position is vector three and again we'll say it's a vector 3.0 you don't actually have to put this in if you leave that off then it will automatically be defined as a zero vector so you can do that if you want I'm just gonna leave that in for now just to show that you can assign a variable a value straight away I'm gonna save that and then I'm going to come down into my ready function and if I type in target underscore position you'll see now that when we do a dot we've got a bunch of different functions that we can call which are attached to these advanced type for example we could call the length function or length squared the length function and that you'll see there returns a float variable type and that result will be whatever the length of this particular vector is again I'm not going to get into vectors and how they function it in this tutorial but I just wanted to show you that you can have advanced types now we'll take a look at these more advanced types in a later video however I did just want to show you that it is possible to have other value types rather than just in strings bulls and floats there is one final thing that I did want to show you and that is the null keyword now Knollys a bit of a special case so for example if we say here nothing that nothing equals null and let's just comment this out here and I'm gonna print out the value of nothing I just play it let's see what we get we get null with a capital and ull and basically null is a keyword for I don't have a value it's it means this variable does not contain anything now that's really useful for example if you had something like a target and you wanted to assign something to this target whenever let's say for example an enemy came into view of a turret it might say right okay I know what enemy I'm targeting now I'm gonna place that inside a variable and as soon as you have a target variable assigned it could then start triggering some fire functions and start firing at the enemy however if that enemy goes out of range you might want to clear that target and to do that you would assign it null so you'd simply say something like target equals null whenever you're ready to clear that and then that would then tell the game that you no longer have a target and you can stop doing all of those fire functions now that was a very brief introduction to GD script I've basically covered variables functions and parameters calling methods writing comments using operators and conditionals and gone through the basic types and a couple of the advanced types there for you we'll cover a lot more of this in the next video if you enjoyed today's tutorial please click that subscribe button and hit a little notification bell I release new content every Monday so don't miss out and as always thanks for watching and I'll see you guys in the next video [Music] [Music]
Info
Channel: Code with Tom
Views: 58,894
Rating: undefined out of 5
Keywords: godot for beginners, gdscript for beginners, programming for beginners, game development for beginners, game dev for beginners, gdscript godot 3.2, gdscript for beginners in godot 3.2, godot 3.2 gdscript, gdscript intro, gdscript intro godot 3.2
Id: WbJ3ttZ2rZk
Channel Id: undefined
Length: 37min 29sec (2249 seconds)
Published: Mon May 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.