Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at animation resource at the animation resource is a resource used to animate everything in the game engine. You can think of it as a data container used for animation purposes for the Godot application. Know what's really cool about Godot is that you are free to do pretty much anything in terms of creativity and one of those things could possibly be if you wanted to create your own trailer just because of what the animation resource provides you.
Now the animation class inherits from the resource class which again inherits from the reference class which means we don't have to worry about managing its memory on top of that. The animation resource must be used with a node that can take advantage of its capabilities. For example the animation player node which is a node object or a node that can be attached onto the scene tree. Now The Animation Resource class provides you with three properties link loop and step now.
Link is just the total length of your animation and by default it's set to float the value one point zero which just means one second. The Loop property is set to post by default and that basically means that you either want to or you do not want your animation to loop. So by default animation looping is set to focus. You just want the animation to run one time. And lastly is the step property which by default is set to the float value zero point one and that just means the animation step value the animation resource class comes with the signal.
It's called tracked change and it's emitted when there's a change in the list of track. So when either, A track is added move or has changed its path. Now in this episode we won't go over signals but it's here just to let you know that the animation resource class does come with a signal. Now the most important thing to understand about the animation resource class is the Track type enum and it's important to understand track types because you need to supply the correct track type depending on the methods you want to use provided to you by the animation resource class.
And again different animation tracks of different types each with its own set of dedicated methods. Now track type is basically just in homes and in video the animation resource class provides you with six different track types. Those would be the type value type transform type of method type Desir type audio and type animation. The one you're most likely going to be using in your day to day will be type value. And that's because when you want to interpolate node properties you're going to use the track type type value enum.
And of course the second most important type you may end up using is type audio. And it handles audio streams regardless of what track type enum you're going to use. You basically have to follow this basic formula for creating an animation resource. First obviously you have to declare your animation resource but after that you need to setup the length of your animation and this is very important by default it's set to 1 second. However you may not want to have your animation last for a second. You may want to have it last for more than a second.
So in a sense you need to change that. After that you need to add your tracks basically by defining its truck type. Then depending on the track you need to call the appropriate set method. And that's an important word to understand. You need to set it. And then lastly after you've set your track depending on the track you need to call it appropriate Insert key method. And then from there you're allowed to insert as many as you want as long as it's within the length you've set for your animation and we will go ahead and take a look at this in code at the end of the episode.
So the first thing you need to do is add a track and so you use the Ad Track Method you declare your track type and then you can define its position by by default its negative one which again means you're going to add it towards the end of your array because negative 1 is the last index position of your array and the ad track method returns back an integer value which is just the index position. It has been set in the array after adding a track you're going to set or use the appropriate set method.
And in this case we're going to use track set path assuming we're using the enum track type called value. And basically you're going to provide its track index followed by the node path and again because we're using the value enum type or excuse me the type value. That means we want to interpolate between node properties and so in this case we need to providing no path which is a string value to your property set on the note. So for example the position property.
Now after you've used the appropriate set method you need to insert a key so in this case again because we're using the track type type value. We're going to use the track Insert key method and again we need to provide the track type index followed by the position in time which again is limited to our linked property followed by a variant value which just represents the key value. And then we can also add the appropriate transition type which is set to 1 by default.
And basically we insert a generic key in a given track before I get into the coding example. Let's go over some other methods provided to us that you may find useful. First you have the clear method which just deletes all tracks in your track array. You may not need to use it but it's there. Next you have the copy track method and all you do is copy a track and you pass it into an animation type. Just keep in mind that it returns back a void or excuse me. It has the return data type void which just means that it doesn't return back anything.
And that's because you're just copying a track not necessarily returning back of value. Next you have the fine track method and you need to provided a no path. And it's just going to search your array for the appropriate node path. Keep in mind you can only have one node path per track but regardless it's going to find it and return back an integer value which is just its index position in the given array. And lastly we do have a method called Get track count which just returns back the total amount of tracks you have in your array. Let's go ahead and take a look at a code example.
Now I'm gonna go ahead and upload this to get up so please feel free to go ahead and download that and play around with it. If you press the play button you're gonna see an animation set by your animation resource in the next episode will go over the animation player but in this episode I just want to stick to the animation resource class. So as you can see here in our animation class we are extending from the animation resource class and of course we've given the class name and this is just for preparation for the next episode and everything is inside a function called Get animation resource method which returns back in animation data type.
And of course the self keyword represents our animation classes because we're extending from the animation class by returning back using the self keyword we are in fact returning back in animation data type. So let's go ahead and take a look at this real quick. So in this case we followed step 1 define our link. In this case we're going to have an animation that last two seconds long. We can ignore the print statements. That's just for you if you decide to download it from GitHub so you can see the values and how it changes over time.
After that we need to add a track in this case we're going to use the type value as the track type which is just the value of zero which is just basically we want to interpolate between a node property 2 and we're gonna assign that integer value which is the index position which in this case will be zero and we're going to assign that value into our variable called Track index. After that we are going to use the track set path method. We're going to provide it the index value which in this case is 0 and then we're going to provide it a node path which is just a string value of the property value we would like to interpolate.
So in this case we would like to access the route same tree followed by the top level parent node in this case that value or excuse me the node name is Node 2 D and we would like to interpolate the node called enemy we would like to interpolate not only the position value but more specifically the x value of the position value which just means we would like to affect the X access of our node and from there we can in fact insert keys for this specific track index because by using the track set path method we've defined that our track index will be related to the enemy nodes position x value.
So in this case we insert a key at B 0 second position for our track index and we would like the x value to be at the position of zero. After that we would like to insert another key on the track index a variable which again is 0 which in turn means we're just talking about the enemy nodes position x property value and at the one second we would like that x position value to be one hundred. So keep in mind that the computer is going to calculate at the zero second we would like to be at position 0 and we will do internal calculations to make sure that the node moves and by the first second we will be at the position x value of one hundred and now as you can see here we're going to add another key because keep in mind or link this two seconds so we can manipulate as many keys as we want as long as we're under the 2 second link value.
And in this case we would like to do the same thing track index. And in this case at the second second we would like our position x value where the enemy node object to be at the position value of five hundred. And so in our heads we can just imagine what's happening at the zero second. We're going to move very very slowly until our exposition is at one hundred at the first second and then at the second second because we're going to move 400 pixels to the right.
You can imagine in your head that are node object called enemy is going to sort of snap. And lastly I have some extra method so you can play around with. In this case we have bind trap and then you can see the values being printed out onto the screen then we're going to add another track that's completely different than our first track and we're going to assign it to a different variable. We're going to set the path but we're not going to add any keys to it. And then we're going to again find that track based on the Node path. And we're going to print those to the screen so you can see the values changing over time so you can get a better understanding.
Or in this case a higher level understanding of the animation resource class. There's a lot that goes on in the animation resource class but in this episode I just want to break it down into the necessities. So to summarize we need to set a link or change the linked property value if we want to work with anything more than one second after setting the linked property to whatever you want it to be we need to add a track. And then after that set a path and then after that we can insert as many keys as we want as long as it's within the linked property values. So in this case if we press play you can see we move slow and then we sort of snap.
And this took two seconds. Now let me show you what happens if we add keys that go beyond the linked value we set. So in this case I'm going to change the value to one. We're gonna go ahead and play that. And notice here at one second we don't do the second track or in this case the second key which would make our enemy position object X's value at the position five hundred. Because in this case are set length is only one second long. And regardless of what we inserted even though we declared that at the second second we would like to be at the value five hundred for our position x value. We're
We're just going to stop here at the first second. And that's because the amount of length or excuse me the amount of seconds allowed for your animation to run was set here to one second. And that's basically it for this episode. Well that's all they 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. Don't forget to download the get a project and play around with the code and see what happens when you change certain properties or use certain methods.
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.