Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at how to write code as a beginner. And we will be working with positioning a sprayed object on the screen as an example. Now when we want to write code we are aiming to do essentially three things. First we want to make sure that our code is readable. Then we want to make sure that our code is then maintainable. And lastly we want to make sure that our code is testable.
However I have this great out because as a beginner we should not be looking towards testing code as a matter of fact. Godot does not provide any sort of unit testing out of the box. However if you are interested in unit testing there is a separate plug in I believe it's called G you t could do unit testing. Moving on as a beginner I want you to have one go when writing code and that is to use functions as a matter of fact as a beginner. You should be aiming to use functions you should favor using functions over raw code logic.
When writing inside of virtual methods and we're gonna take a look at that later. And one thing to keep in mind is that a good function slash method will only do a single task.
Let's go ahead and take a look at a basic coding example. Before we take a look at the code let's look at the setup. So on the file systems section of the Gordo application I do have a single gene script file that we will be using. It's on my computer I call it hero that cheat script file and on top. You'll notice that there is a sprite node that I also named Hero. And over here you can see that I have attached the hero script to the hero node which again is a sprite node.
Now let's go ahead and take a look at some of the code just to showcase what we will be writing to screen. Let me go ahead and show you what we will be doing in this episode for our example on rating better code. So we're not going to do anything fancy we're just going to attach our sprite known to the top of the screen no matter where it was positioned initially in our game scene. So you can see here a Godot is actually outside the bounds of the window. And that's basically it. Now that we've established what the goal of this episode is let's see how we can apply the opinionated rule of using functions can help us improve writing code.
So let's go ahead and setup our script class because we are inheriting from these sprite class. Let's go ahead and extend from that. Now from here we're gonna go ahead and pick one of several methods. There are no wrong answers. However in this example I am going to use the enter tree virtual method
now from here. I'm gonna go ahead and set up the code to get our sprite image to center itself at the top center portion of our game window
and so as you can see here in four lines of code we are able to position our game spread at the top center of the screen. Let's go ahead and make sure that our code is working the way we want it to. As you can see here are sprite images in fact at the top center portion of our game window. Now to introduce how functions can help us out when writing code I'm gonna add a second go. And that second goal is that we want this sprite image to stay at the top center portion of the game window when we adjust the size of our game window.
As you can see here our sprite image does not in fact state to the center. So let's see how we can use functions to help us out make coding a little easier. Now I purposely wrote code like this to give an example of how a beginner may write code when trying to solve game mechanical problems. As you can see here the first problem we have is that we wrote raw code logic inside of our virtual method. And this is in fact an issue. The issue here are on two fronts the readability of our code and the maintain ability to make this a little clearer.
Let's go ahead and write code that would solve our second problem which is adjusting the image in a way I think a beginner would try to solve it. And so to adjust the sprite image constantly we're gonna need to use either physics process or process. So let's go ahead and write that out.
Now basically here we have the same lines of code. However we are able to solve our problem the problem being that we are able to not only position our game object when the game starts we are able to adjust the game object to the center top portion of our game window as the game is running and as the user changes the width of the window. Let's go ahead and take a look at that. So as you can see here we've solved problem number one positioning our game object to the top center of the window and Problem 2 which is adjusting our game object as our user changes the width of the game screen.
Now even though our code does exactly what we wanted to we wrote our code in a way that is 1 not maintainable and 2 it is not readable. We can read this right now because we just wrote it over if you were to take a break from coding your project for a week and come back to this you may have trouble trying to remember or understand what's going on and especially if you're new to a project you're to invite someone to collaborate with you they may have trouble understanding what your game script is trying to accomplish and remember as programmers we are trying to write a story with our code.
In this case a story about how our sprite behaves when it enters the same tree and a story about how it behaves while the game is running. You have to read basically eight lines of code to understand what you're trying to do when you enter true and what you're trying to do as the game runs. And it wouldn't be until you've read all eight lines you wouldn't realize that the two virtual methods have the exact same code. Let's go ahead and fix this. And it's really simple. Basically all you have to do as a programmer is make sure that you keep your virtual methods clean by trying your best to avoid using raw code logic inside of virtual methods and that requires you to put all your raw code logic inside of functions
now when you try to use this process for the first time especially if you are a beginner you may have trouble trying to come up with names. And that's OK. The best thing to do for now is try to come up with meaningful names that describe what your function does because your function should only do one task. Basically think of your function name as a quick summary of the task that your function is attempting to implement. In this case because we want to center to the top center of our game window that's what we'll name our function.
Now you don't have to explicitly type the data type you're going to return but in this case I do prefer to explicitly declare what kind of data type my functions will return as it makes reading code easier
and this is basically it as you can see here by putting everything inside of functions and keeping our virtual methods clean our virtual methods are easier to read. Now when you come back to this code a week later or you invite someone to collaborate and they look at this Sprite class excuse me the hero class in just one line of code we are telling the simple story that our sprite image will position top center when it enters the same tree and that our image will position top center as our game is running.
And as you can see here we've already got the full benefits of maintain ability and readability that functions provide when we try to follow the rules of making sure we do not put raw code logic inside of our virtual methods. We can also clean this up a little more obviously when we position to the top up center while our game is running we do not want to actually create new variables 60 times a second. So you could for example take this away and add it to the enter tree.
However again for readability and maintain ability it's best to follow the rules that I outlined in the slide show especially if you are a beginner. And so instead of copying pasting this code in the enter tree which is reasonable we're going to again follow the rule of creating functions and keeping raw code logic away from the virtual methods.
And basically that's what you're going to do for your homework basically for your homework. I want you to create a function that sets up variables for game screen width and Sprite height and it's very important that you do this first before attempting the other for homework problems. Now when you finished creating a function that sets up your variables for your game screen width and Sprite height I want you to then go ahead and create a function that positions your sprite image to the bottom center of your game window.
And then after I want you to create code that positions your game sprite in the center. Basically the middle of your game screen switch should look like it's floating. And then after. Go ahead and do the same thing for positioning your spread object to the left. This most side of your game screen position towards the center along the y axis and then do the same thing for the right side of the game screen again being centered on the y axis. Now I'm hoping that this homework will illuminate two things one as your code grows and as functionality grows you need a way to process what exactly it is you are reading.
The second thing I'm hoping this homework highlights is that sometimes the decisions we make when game programming is not final. For example I went ahead and basically programmed a piece of code that centers are a game object to the top center of our game screen. However maybe in the future I'd like to change the positioning to somewhere else. And of course if I were to write raw code logic inside of the virtual methods changing game functionality would be harder than swapping out functions. So if you're a beginner go ahead and do this homework and practice your ability.
I'm basically separating raw code logic that you would have or possibly thought about putting inside of your virtual methods and go ahead and write them inside of functions and those functions you put inside your virtual methods. Well that's all I have for you in this episode. Thank you so much for joining me. I'm gonna go ahead and upload what we've worked on on to get up so please feel free to download that and then go ahead and start on your homework. Thank you for clicking the Like button. Thank you for clicking the Subscribe button. I look forward to seeing you in the next episode.
Have an amazing day.