Hello and welcome to another episode in the Godot basics tutorial series. And this episode we will be taking a quick and brief look at moving a game object now moving a game object is a very common thing. Games may want to do for example you may want an enemy to move or perhaps your game has bullets. And so you may want your bullets to move and it's quite simple. As a matter of fact this entire episode we will be taking a look at code. But regardless here we have a sprite game object and all you really want to do is compound a value to either the x or y position of the game object.
You can do both. Or you can do nothing which would mean your game object is a link. Or rather it is not moving. And in this case because we're moving to the right we are compounding a positive value to the x position. Let's go ahead and take a quick look at code that implements moving an object. So
So over here we have the same file from the last episode. However I added a few things. The first would be the velocity variable which we typed as a float data type on top of that on the entry. I added the function setup velocity we're going to setup the Variable value in this function and towards the bottom we have our two functions set up velocity which sets up our velocity to the float literal value one hundred and a move. Right.
Function which takes in the parameter Delta given to us by the physics process. Virtual method and all we do is compound a positive value onto our x axis. We have our velocity one hundred multiplied by the delta value which again is a float value. Now there's gonna be a homework but before the homework I want to go over something to do with your coding adventure. In the last episode I mentioned a starting point template in that starting point template was to put everything in sight of functions and make sure to keep your virtual methods clean.
Now this is a great first step to take because one you're thinking about your code organization. You're thinking about how code basically allows us to implement tasks and these tasks help us solve problems. However we need to move forward and grow from there. And so I want to introduce something. First is our functions. Notice how our functions are consistent. Basically we use the verb setup to describe a function that assigns a value to a class variable.
And notice how we use setup and setup velocity setups. Right. And setup game window and the first thing I want you to take note is that our naming conventions for functions are consistent amongst similar tasks and the same thing is true for positioning our game objects. It starts with the verb position and it's generally good practice to have your function start with a verb or noun.
Even though it's great naming practice convention to start your function names with a verb or a noun the most important thing I want you to get from this is to use naming consistency amongst functions that implement similar tasks.
Now the second thing I want to introduce to you is function purpose. Again I mentioned that there are multiple ways to solve a problem and in this case the problem is for one setting a default value for our velocity. The reason we want to set a default value for velocity is because we want to implement some type of movement functionality. However consider this to yourself even though our starting point template says to put everything instead of function when we want to assign a literal value to a class property variable is there a better way to do it than to put it inside a function.
And the question you should be asking yourself really is does this code help speed up my ability in testing new game features. And even though we're not making a game we are implementing some type of game functionality in this case movement. And so you may find it cumbersome for example to come into this function set up velocity to change the value every time you wanted to test the new speed. And so instead of using setup velocity we could instead add our value directly to our variable property and we could easily write code like this.
There is no need for a function for our velocity if we plan on starting it at p value one hundred. Now on top of that we can even make our game programming journey easier and testing different types of velocities by adding the export keyword. And what this does is it allows us to edit our velocity variable directly in the Godot Editor. This means we don't have to come to our hero class script file and manually change our float value we can do it in the editor and this makes our coding much easier.
So as you can see here in this case even though we started with the setup velocity function because we're trying to keep raw code outside of the enter virtual method you can in fact do the same goal of keeping real code outside of virtual methods by directly assigning default values in the class property. And as a matter of fact the same is true for sprite width and Sprite height because our sprite width and height do not change values. We could directly access our texture property and assign the width and height of our image right here in lines five and six instead of creating these setup sprite function.
And because we're not using setup game window inside a physics process we can in fact do the same thing four lines three and four. And so as you can see here I'm trying to shine a light that even though we have this starting template of creating functions for everything the next best thing is to ask ourselves is the way I'm writing code going to help me or hinder me when I want to add edit or remove game functionality. And that includes testing functionality as well.
In this case for velocity let me show you what I mean. So here we are in the game editor as you can see here are velocity variable is one hundred. I'm free to edit my default velocity value right here in the Godot editor no need to go inside the code. So even though we started with having the value set instead of function we went ahead and did it directly in the variable declaration line along with the export keyword and now testing for new velocity values is easier.
So you can see here we started at the position left center moving right at a constant value of a hundred pixels per second. And without even going through code we can actually edit the value and see the edits change on screen in this case. This is value two hundred and if we wanted to we could even go really high just to see the vast difference and look at that. And as you can see here because we decided to assign a literal float value in the variable declaration along with the expert keyword we've basically streamline our ability to test for new values for our velocity variable.
So again I want to reiterate that even though we're coding and you have this great starting template that you're using as a beginner which is to put everything inside of functions now we can go a step above and ask ourselves does everything have to be done inside a function. And there are times where we need to use a function to in fact set up values for our variables and learning the difference between when we need to do that will vastly improve your ability to code. So just a quick recap function names with similar task should stay consistent it's good practice to have the start of a function name be either a verb or a noun on top of that.
When we code we should be writing our code in a way that makes our game dev lives easier. That includes readability and maintain ability and in that example of using the export keyword that would in fact align with both readability and maintain ability especially since that variable did not change values. So I do have homework the first assignment is to make sure you add functionality that moves the game object down left and then I want you to add acceleration to your velocity movement.
So in this example our velocity movement was static it never changed. However I want you to add acceleration. So it starts slow and then it speeds up over time. And then lastly I want you to implement a maximum velocity to the game object in this case. Velocity cannot pass a certain threshold. Well that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the Like button. And thank you for clicking the Subscribe button. I'm going to upload this project to get up so please feel free to download it and then work on the homework from there.
If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day