Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at the File class in the last two episodes we dealt with bits and bytes and Unicode and the two episodes prior to those two. We covered saving and loading files in this episode we're gonna cover some methods provided to us by the file class and see how they work.
So the file class comes with a property called Endian swap and endian swap is a boolean data type. By default is a set of foes and when it is set to false that is implied that you are using that to endian byte ordering. We went over a detail about endianness in the previous episode I'll go ahead and leave a link in the description down below. The one thing I want to talk about is CSV. TSB stands for comma separated values. By default the delimiter is the common symbol and Godot allows us to change that symbol.
Godot offers us two methods when dealing with CSV files. The first is the get CSV line method by default. It can take in an optional parameter but by default that the limiter again is set to the comma symbol. However if you decide to provide your own delimiter just keep in mind that you're only allowed to have something that is one character long. The second method provided to us is the store CSV line method. The first argument is a poor string array data type which is just a special data type provided to you by good.
And it's basically an array that holds a string of values. The second argument is optional. By default it's set to become a symbol. However you are free to choose your own symbol just keep in mind that it has to be one character long. Now what the second argument does is for every value in your array it will add that delimiter onto the text file you are saving too and then continue adding each value inside your post string array. Let's go ahead and take a look at these stores CSV line which takes in the pull string array data type as the first argument.
So we have our array which has an email address first name last name and if we were to pass this in the store CSV line method what we get is the following saved to a file. Now when we use get CSV line method what we do is we take in. In this case or email come our first name common last name and we convert that into a post string array and as you'd expect because we're using the comma delimiter by default when using get CSV line method we're going to properly separate in our array based on again the comma symbol in the text file.
Now what happens if we decide to use a different delimiter for our Get CSV line method. Well because we saved to the text file using the comma symbol as our delimiter when we do in fact change the delimiter we change the way we retrieve data from the text file. So let's go ahead and take a look at that in the text file. We only have one at symbol and so as you'd expect everything to the left will be retrieved first. Everything to the right will be retrieved after. And because there are no other symbols in our text file we're going to retrieve two different data objects.
And so that's what you'd expect here. John comma space which is God's way of letting us know that it's separating the data and then everything else and it looks a little confusing because we have commas and cited an array. Now when we highlight our values here you'll see that John is an index zero of our array and DOE dot com. Come on john come out doe is an index one of our array. And so you can see that there are two different string values. Now this brings up an issue because we're able to choose our the limiters but when it comes to saving and loading CSV files there is a rule of thumb and that is to stay consistent in your delimiter symbol when saving and loading to a file.
Let's move on. Another thing that Godot provides you is the ability to grab bits from your text files you're able to get a bit sixteen bits 32 bits and 64 bits no matter which one you choose you'll be returned back an integer value. Now if the file class allows you to get bits you are also able to store bits and good provides you with the ability to store 8 bits 16 32 and 64 bits. Now the one thing I want to touch is reading files which are given to different methods that allow us to customize how we read files.
The first one is get length which returns the size of our text file in bytes and keep in mind that one byte is a single character when dealing with UTF encoding. The second method provided to us by the file classes get position and it returns the file cursor position. Let's go ahead and take a look at some pseudocode. So here we have our standard while loop when reading a text file but it basically boils down to two methods versus the get position method which is where our cursor is currently at in the text file when we want to make sure that it is less than the total length of our file.
And of course we want some way of reading from the file in this case we're just getting a bit every iteration which is just every character in The File and our file just holds the string value. Hello world. Now when we think about cursors for example the get position method we normally think of the standard cursor when we for example type in the Godot application when we're making our game or when we're typing something on Google. However instead of thinking of a cursor like this I want you to think of a cursor that highlights an entire character.
And so when we're getting the position we're really cursor ring over a character in The File. Basically the first character in The File now on top of a cursor highlighting a character in our text file when we grab our position our position always starts at the value of 0. So in this case we say that the capital H is in the zero position or index of zero and we basically iterate. So E is one two three four five six seven eight nine ten eleven.
And so our exclamation point is at position eleven. Again also referred to as index eleven. And basically if we were to print these out get position and get linked when our cursor is at the exclamation point what we're gonna get is the following position is at eleven the length of fire was twelve because hello space world with an exclamation point is 12 characters and of course when we read the binary format of the exclamation point it will have the base 10 or decimal value of thirty three because again we're getting 8 bits in the file every time when looping through our text file 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 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 and the next step soda have an amazing day.