Login | Register

List files

July 25, 2006, 15:05 by Calle
[loading]
-->
If you have ever created a game or program that saves files, then you have probably also encountered another problem: how to load a file. This article will not deal with how to load the information itself, but it will deal with how to give the user a chance to select a specific file. It is very common to use slots; because if the user selects slot one you know that you’re supposed to load slot1.sav … sometimes this slot-based system doesn’t work, and you want to be able to save an infinitive number of files, maybe with custom names.

<div style="clear: both;">I use the following code to compile a list of all my save files and present it to the user:

c = 0;
extension="extension";
file[c] = file_find_first("files/*."+extension,fa_archive);
*/
< just ignore this line;
while !(file[c] == "";) {
c +=1;
file[c] = file_find_next();
}
file_find_close();
d = 0;
exi_files = "";
while(d<c) {
exi_files += string(d) +". "+file[d]+"#";
d+=1;
}

selected_nr = get_integer("Choose a file and then enter the number
below (use unexisting number to cancel):#"+exi_files,"";);
</div>

It will get all names of the files in the files folder that got the specified extension. I am doing this with two loops: the first one is using my mask and attributes to find all the files, it sorts them into an array. The second will go through that array and add them to a list and it will also add a new line character, this way I will be able to draw the list as one. That is what I do in get_integer. It will look something like this:

0. Custom name 1
1. File2
2. Custom name 3
Etc. If the user selects 0 we can figure out what file that is by looking back at the array:

if (selected_nr<=c) {
file = file_text_open_read("files/"+file[selected_nr]);
}


I add an If statement, with proper condition, because we do not actually know that the user will enter one of the numbers that are displayed. He might enter any number, by mistake perhaps, and if we try to open that file it will cause an error. This will work though.

Note that you may also use the first part of the code, this last open-up-file code, but present it anyway other than in a pop-up. This method is highly sophisticated and it’s possible to present the info in anyway, as you got it in an array.

Example:
http://gmtutorials.com/files/listex.gm6

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games