GameMaker Get list of files

Z

zendraw

Guest
How do you fill a list with the names of files? like you have multiple saves, then you do a loop and store theyr names in an array. i done this before but cant remember how was it done.
 
H

Homunculus

Guest
You can list the files in a directory using the file_find_* functions, if that's what you are trying to do.
 
Z

zendraw

Guest
yeah right, that was the one, but how do you structure it?
 

chamaeleon

Member
Code:
find first file
while found file is not empty string
   do something with filename
   find next file
find file close
 

TsukaYuriko

☄️
Forum Staff
Moderator
Define the search criteria via file_find_first. Any subsequent calls to file_find_next will return the remaining files matching the same criteria, in sequence.
 
Z

zendraw

Guest
like this?
GML:
save[0]=file_find_first(string(working_directory)+string("*.stk"), 0);

if (file_exists(save[0]))
{
    var i=0;
    while (file_exists(save[i]))
    {
        save=file_find_next();
        i++;
    }
}

file_find_close();
 

TsukaYuriko

☄️
Forum Staff
Moderator
That'll overwrite the array stored in save with a scalar file path after the first completion of the loop because you're not using it as an array there, so no, not quite.
 
Z

zendraw

Guest
ok,
this
Code:
save[0]=file_find_first(string(working_directory)+string("*.stk"), 0);

if (file_exists(save[0]))
{
    var i=0;
    while (file_exists(save[i]))
    {
        save[i]=file_find_next();
        i++;
    }
}

file_find_close();
gives me this error
GML:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object oarmies:

Push :: Execution Error - Variable Index [0,1] out of range [1,1] - -1.save(100022,1)
 at gml_Script_scrstackgetlist (line 9) -        while (file_exists(save[i]))
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scrstackgetlist (line 9)
called from - gml_Object_oarmies_Create_0 (line 7) - scrstackgetlist();
called from - gml_Object_ocontroll_Other_10 (line 58) -                             else {instance_create_depth(0, 0, 0, oarmies)};
called from - gml_Object_ocontroll_Step_0 (line 41) -               event_user(0);
 

chamaeleon

Member
ok,
this
Code:
save[0]=file_find_first(string(working_directory)+string("*.stk"), 0);

if (file_exists(save[0]))
{
    var i=0;
    while (file_exists(save[i]))
    {
        save[i]=file_find_next();
        i++;
    }
}

file_find_close();
gives me this error
GML:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object oarmies:

Push :: Execution Error - Variable Index [0,1] out of range [1,1] - -1.save(100022,1)
at gml_Script_scrstackgetlist (line 9) -        while (file_exists(save[i]))
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scrstackgetlist (line 9)
called from - gml_Object_oarmies_Create_0 (line 7) - scrstackgetlist();
called from - gml_Object_ocontroll_Other_10 (line 58) -                             else {instance_create_depth(0, 0, 0, oarmies)};
called from - gml_Object_ocontroll_Step_0 (line 41) -               event_user(0);
You're getting an error because you're testing if save[i] exist before assigning it inside the loop.
 
Z

zendraw

Guest
btw it returns the string as "save.stk" right? and not just "save".
 
Here's how I do it - check for empty string, rather than file_exists()

Code:
index = 0;
save[0] = "";

var _file = file_find_first(working_directory + "*.stk", 0);

while ( _file != "" ) // file_find_first() / file_find_next() return "" (empty string) if no files found.
{
    save[index++] = _file; 
    _file = file_find_next();
}

show_debug_message("Found " + string(index) + " files.");

file_find_close();
 
Top