Introduction to GDscript - Godot tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is dedicated to the basics of the GD script language that's the programming language that you are going to use in Guto to create your gameplay I'm going to do two things in this video first I'll try to answer the most common questions or any concern you might have about the language itself and then we'll look at a part of the syntax and point out some of the differences or similarities with Python along the way if you prefer you can skip ahead to the part where we look at the actual GD script syntax there's a link in the video description for that so the first question you might have is why is there a specific language at all why not use something that already exists like Python Ruby c-sharp C++ whatnot in the past when Godot was still closed source Avron lead had Lua and Python this caused the developers a lot of trouble and they decided to make their own language one thing is that traditional languages are not optimized for the needs of game developers GT script is optimized for code that runs all the time in a loop and like a more traditional application where you might rerender things only at certain points in time in a game you need to render 30 to 60 images per second and GT script is optimized for that having your own language allows you to add new features fast and to do that based on the very needs of your project so GD script can be extended with more functionality for game developers and it already has some built-in functionality for us like you can do vector calculation work with mattresses you can stall colors and variables and other things like these Gidi script is a dynamically typed language meaning it's easy to learn and to use you don't have to compile the program to run the code so you can test your game at any time it's also easy to read and to write as you don't need as many lines of code as with a more complex language C++ for example it's faster to add new behaviors to your game characters or your environment but also to navigate inside of existing scripts because we are fairly short these advantages do come at a cost some errors will only be detected while you are testing the game and the performances of the language are lower than with a statically typed language like C C++ Java C sharp but you really don't have to worry about performances because all the demanding aspects of the engine are written in C++ GT script is only an extra layer for you to code your gameplay so when you are making calculations with vectors when you are calling built-in functions in general this is based on C++ code and this is very fast on top of that you can always use C++ code yourself for critical bits of code if you have a very resource-intensive algorithm for example you can implement it in C++ and call it from GD script let's talk about code editors right now because you'll often see me working in Visual Studio code an open source multi-platform code writing application it doesn't have specific support for Godot right now it only gives you syntax highlighting for GT script you can write code directly in gurus built-in editor and you will get hints in real time to help you find the methods you are looking for you're going to get auto completion and also it will tell you about errors now I prefer to use external editors like Adam or Visual Studio code simply because they offer a much richer writing experience so when you are working in vs code whenever you save your changes will be reflected in Godot instantly and vice-versa so nothing Vence you from using both editors to your advantage if you choose to go with Adam which is another popular open source code editor you will find some plugins that adds GT script support so I'll leave the editor choice up to you because at the end of the day it doesn't matter which tool you are using you are just going to be writing code for me it's more comfortable to use Visual Studio code but pick whatever tool you feel most comfortable with and if you have trouble setting up an external code editor just stick to the one that's built in go do for now it's going to do the job really well alright now it's time to look at the basics of the syntax so in the previous video I said that GT script is like Python here we'll talk about both the syntax itself but also the similarities and the differences well with Python if you still have little experience with computer programming this tutorial might feel a bit abstract to you but that's normal and that's part of the learning process we are studying a new language here after all so even though the language itself is simple with relatively few words and grammar rules it's still completely new and foreign to us so the way we do it in general and computer programming is that we start to look at their keywords and the structure of a script in order to get started writing code with that language and that is partly due to the fact that if you have some experience programming all of the concepts that you know translate very well into a new language variables are still variables loops are still loops you just write them a bit differently let's start with the basics variables on the left you have Python and on the right you have some approximate equivalent in Gd script and the first thing that you should notice is the presence of a keyword var this one is used to declare a variable so for example if I type var Maya I'm going to declare an empty variable then I can store a value inside of it at any point so I can type my var and I'm going to assign it anything 18.5 and it's going to create a floating point value you have multiple types at your disposal and the basic ones that you have in pretty much every programming language integer and then if you add a dot it becomes a floating point value if you use quotes you're going to create a string so I can say hello and if you type null this is going to create an empty variable so in conditions which we'll look afterwards if you do if my BA and pass this condition is false okay it's not going to work then you can use types like boolean so you can type true or you can type false notice that it's different from Python true and false don't have a capital letter at the start and aside from that you can do a few interesting things first of all you have access to vectors so if you type vector 2 or vector 3 you can create a new vector you are going to use parentheses and then you will type your values so I can create a vector with a length of 2 in the x-axis and a height of 5 the y-axis alright you can create a vector 3 which is going to be a 3d vector with the vector 3 constructor open parentheses and use three different values separated by commas you create mattresses as well well look at all these types later on you you have some more that are very specific to game development so unlike in Python you can define new constants with the co NS t or Const keyword and for example I'm going to create a constant called player speed it's going to store the maximum speed for my player at a max in front of that using capital letters is a convention in Python whenever you want to declare something that's like a constant when you want to tell people hey don't change this variables value but in Godot if you try to set max player speed to something else it's going to give you an error so unlike a variable you cannot change the value of a constant it's something that you are just going to use to help yourself in your code you're basically putting a name on a specific value to help you write code faster and then you have containers so containers are variables that M can hold multiple values you have two types in Gd script you have arrays and dictionaries arrays are like lists and Python you can put anything inside of them like other arrays dictionaries integers strings boolean x' made a mistake float values other strings whatever okay it all works so it's just like in Python and then dictionary are just key and value pairs you can define dictionaries the same way you do in Python so you use brackets and then inside of these you are going to write a string put a colon the corresponding value that's going to be attached to this key and then a comma the second value may be a comma the third value so I'm going to call it four in that case four and four the last one you don't add any comma at the end it's exactly the same in GD script I have one Colin it has a value of 1 2 has a value of 2 3 has a value of 3 I have two commas but nothing after the three it's just going to be a closing bracket you can also define dictionaries like in Lua and GD script this is something that's not available in Python so you're just going to use a name an identifier then put the equal sign and the value so for example 1 2 & 3 here it's eggs going to be exactly the same dictionary as the first one and then you have the ability to define new values that you are going to add to your dictionary so first you can use a dot notation you can do my dictionary dot 4 equals 4 and this is exactly like adding a coma here and add an entry called 4 equals 4 all doing it right there at the top add a new string for the key and then I add the value 4 so this line at the bottom is exactly the same thing you can also do it like in Python so you just put brackets and you put the key inside of it and with the equal sign you assign a value to that key so for example my dictionary open brackets 5 I quote that and then I say equals 5 which is the equivalent of adding a comma here writing 5 equals 5 there you go conditions are fairly straightforward so you can use the if keyword to check if a condition is true or false you can use pass to temporarily make your code error proof so basically if you don't boot anything in there and directly go to else go-to is going to give you an error it's just like in Python you can't have an empty block so to make it you have to use the best keyword you can also use paths inside of functions or classes that are empty then you can use Elif which is the shorthand for else if and you are going to check for another condition so Elif some other thing is true and you can run some other bit of code and in the end you can use the else keyword if none of the conditions that you check before are true it's going to do something else right there at the end okay now inside of conditions you can check for example to see if something is equal to some other things so five is equal to five you're going to use two equal signs just like in Python so you can check if a variable is equal to something else you have the end keyword if you want to check some other condition player is alive for example it's going to check if the player is alive variable has a value that's true so if it's a positive value if it's a string something other than null or instead of V and keyword you can use or to check if the first condition all the second condition is true in which case it's going to run the code in there okay and then one thing that's interesting if I define an array and I'm going to put two three four three values in there if my variable which I'm going to define by the ways of all my bar is going to be equal to three and if I write if my VAR n array guru is going to take each and every one of the values inside of the array and check if my var is equal to is equal to them so it's just like in Python here my ball has a value of three so guru is going to check is going to say is to equal to my Val know is three equal to my VAR yes so this condition is true so we run the do something function in that case so you can use V in keyword to run through an array now we can talk about loops and you will find that in keyword with just so once again so you can use for loops to run through the content of an array or for a range which is going to just create an array all right it's very similar to Python which you can see on the left here if I create a variable called Y and give it values 1 2 3 4 for example this for loop is going to assign to X the value of each of the members of the Y array it's going to do something maybe with X for example or I can simply use the print function which is going to print the variable to the integrated console in Godot and it's going to print one two three and four then you have the range function just like in Python which is going to create a list like this one so if I use range 5 I'd say I think it's going to create an array that's start with 0 and then 1 2 3 4 so it's going to go at least in Python it goes from 0 to the value you have in there minus 1 you can also create a range like that so you're going to define two values 3 and 6 and this range will have the values 3 4 and 5 you also have the ability to use the wild keyword and this one works a bit differently you check for a condition and while this condition is true you keep looping over that code so in that case I create a new variable called X and give it a value of 0 and while x is lower than 10 I'm going to print X and add one to it and here's the last thing we are going to look at in this video and is how to create functions in Python use the keyword death and in Godot you are going to use V keyword Frank and then you are going to give your function a name do something and you can write the parameters so you can yeah I'm going to use parameter 1 prime into 2 and you can assign a value to them like something alright you put a colon so it's fairly similar to Python and then you're going to write your code inside of the functions buddy I wrote on purpose V if else a statement on a single line which you have access to in Python which is very handy it's a shorthand to assign a certain value to a variable if a condition is true and otherwise you use another value so it's a shorthand for if this is true variable I'm going to create the variable before that so variable test ok if something is true the variable test is equal to value 1 I'm going to use a string else it's going to be set to value 2 all right so this is what this line parameter 1 is going to be returned if it is set to something otherwise we return a default parameter to whatever it's just an example you don't have that in Gd script however you can obviously return a value from a function with the return keyword just as well you can also return multiple values just like in Python you are going to return an array instead of a single value string or variable alright you can even return a dictionary if you want and that's it we've taken a quick look at the basic syntax in Gd script so I really wanted us to go through that fair quickly so we have some basics and we're going to write code in practice to really better learn and understand all of those keywords because right now it's just the basics of the basics and it's still a bit abstract but we're going to get started making actual stuff in the next few videos so thank you for watching and see you then
Info
Channel: GDQuest
Views: 79,181
Rating: undefined out of 5
Keywords: gdscript, GDscript tutorial, godot gdscript, godot gdscript tutorial, gdscript tutorials, Godot tutorial, godot script, learn gdscript, coding, gamedev, indiedev, game design, programming, GDquest, Nathan Lovato, Game Design Quest, tutorial, godot engine, Game engine, Game creation tutorial, godot, game development, game creation, game programming, Godot game engine, godot engine tutorial, open source
Id: K5-5j4H4Ypo
Channel Id: undefined
Length: 19min 34sec (1174 seconds)
Published: Thu Dec 22 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.