Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a shallow and yet at the same time a deep dive into the sprite class before we get into the sprite class. Let's talk about the canvas item class the canvas item class comes with a lot of properties and methods that are useful for us. However I want to introduce to you three methods that might prove of some value to you on your programming journey. Now if you want to edit the visibility of your sprite image you'll be using methods provided to you by the canvas.
Item class the canvas item class comes with a method called set. Visible and you pass any boolean as an argument. And based on that boolean you will either show the image onto the game screen or you will hide it. Of course if you want to check whether your image is visible or not you use the is underscore visible method that returns back a boolean value. You can also shortcut setting the visibility of your image through to other methods.
The hired method which hides your current image and they show method which shows your current image. Moving on now the sprite class is used in conjunction with these sprite Node. These sprite classes use when you want to display a 2D texture on screen and this also includes using sprite sheets. One thing to note is you cannot edit your image directly with these sprite class. I want to introduce to you some properties you may want to know.
The first is the offset property. It comes with a getter and setter method the setter method is called set off set and you pass any vector to data type as an argument and it comes with the getter method get underscore offset and it returns back a vector to data type. Now if you want to edit and control the offset of your sprite class when the game is running this is the property and center get her methods you will use. However the behavior of editing the offset value in code will depend on whether or not the property call center is set to True or false.
Now the centered property is of type boolean and it comes with its own center and get her method. If you want to change whether it's centered or not you use the setter method called set underscore centered. It takes in a boolean as a argument and then in order to see whether you're offset is in fact center to your sprite image you'll use the getter method is underscore offset method which returns back to you a bullion value. So just keep that in mind. However it's best to keep the center property set too. True
True it makes coding a little easier now. The most important property that your sprite class comes with is the texture property. Now the texture is the object you're going to draw on your screen for example. This would contain your image data in order to set the texture value of your spread image which is not recommended you use the setter methods set underscored texture it takes in a texture data type as an argument. However what we are truly concerned with is getting the texture data type to do that we use the get underscored texture method which returns back to us a textured data type and this is one you will find yourself using when you want to know the width and height of your image textures or items you create by loading from a file as a matter of fact.
Any PNG file you use is considered a texture on top of that texture is a base class for other resources. As a matter of fact you can not use the texture class directly. One thing to note is that your PSG file is actually considered a stream texture which inherits from the texture class and it comes with all the methods that the texture class provides. So something to note now you cannot edit your image data directly through the texture class.
However the texture class comes with methods that are useful to us when we want to program. I want to introduce 2 3 methods I think are important when you start programming. The first is the get size method. It returns back to a vector to data type. Of course the first value in the vector will be the width and the second value in the vector will be your height on top of that if you just want to get an individual data of your sprite image in this case the PMG file. You are also able to use the get height method which returns back to an integer value and the get with method which returns back in to draw value both of these methods are referring to the height and the width of your image file the most important texture method is the get data method and it returns back to you in image data type.
This image data type will contain your image data. Now the image class that you're being returned back to when you use get data also known as a data type is a native image data type and this data type contains your image data and of course from an image data you can then convert that into a texture or rather a class that inherits from the base class texture. Now here in the image class slash data type we are able to edit art image file.
So before we get into that let's look at some of the properties. As you can see here we have the same properties we saw in our texture class. We have get size get height and get wet on top of the properties that allow you to edit your image is the resize method. You can also shortcut this by shrinking in half or expanding to double the original size. However just because you call these methods does not mean you're going to edit your image in real time. Keep that in mind.
It would be really bad if you did however even though we cannot edit our image data directly we are able to save the edited image data type into a new file and so we have a method called Save underscore P and G which will of course save your file was a PMG file and it takes any string argument that axe has the file path you would like to save. This new PMG file too. Again I do not recommend you edit and saved images. However it's there for you if you want to implement some type of image editing to your game.
Do not edit your image directly through the Godot editor if you need to edit your image then go through the proper channels which is an image editing software such as Photoshop. Now in this episode I introduced 2 4 classes. However I only really want you to understand fully the first three classes I introduced to you. I introduced the image class as well so you can get a better understanding of where our image file is coming from. In this case our PNG file. Now to recap the canvas item class allows us to hide and show our image to the game screen.
The sprite class is the class that contains our image data and basically this class allows us to draw our image onto the screen. However the sprite class alone doesn't give us the data of our image. For example the pixel width and height of our image and so in order to do that we need to go one step down which is the texture class. Remember that the texture class is a base class. It cannot be used directly. So in the case of the PNG file we are using the stream texture class which inherits from the texture class. However
However the texture class gives us the methods that we need when we want to get the pixel height and or the pixel width of our image file. And lastly of course the image class is what holds are PMG data. And just for fun I wanted to show you that we could in fact edit our image file and then save that to a new PMG file.
However I do not recommend doing that unless your game really needs that.
And that's basically it. Well that's all I have for you in this episode. Thank you so much for joining me. Thank you for subscribing and thank you for clicking the Like button. 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