Game Development Community

Returning an array from a function

by Rob Segal · in Torque Game Builder · 10/09/2006 (1:07 am) · 2 replies

So I have this function I made getFileList (isn't this in the engine already?). My problem is that when I interpret the return value from this function it's empty. It's like it's not array at all. Am I doing something wrong here? Surely I am lacking some sort of script line because I would think this should work. Any thoughts?

function getFileList(%path)
{   
   %file = findFirstFile(%path);
   %i = 0;
   
   while (%file !$= "")
   {
      %fileList[%i] = %file;
      %file = findNextFile("~/data/levels/*.*");
      
      %i++;
   }
   
   return %fileList;
}

#1
10/09/2006 (8:39 am)
In your example %filelist is not an array. %filelist is a variable that is empty which is exactly what you're seeing. The array is %filelist[0] through %filelist[n] explicitly. You may want to look into using a SimSet instead of an array. You can return a SimSet in just the way you are trying in your code sample. The main issue is a SimSet must contain some objects so make sure whatever you are storing in them is an object of some type. You may also want to reevaluate what you are trying to do there. Your code makes a list of files where the first file is from %path and the rest are from the levels dir? Hardly seems like what you would want to do ;)
#2
10/09/2006 (11:01 am)
Ah right. I'd forgotten that %fileList is different from %fileList[0] in Torque script. Here is a revised version I made using a SimSet...

function getFileList(%path)
{   
   %fileList = new SimSet();
   %file      = findFirstFile(%path);

   while (%file !$= "")
   {
      %fileList.add
       ( 
         new ScriptObject()
         {
            fileName = %file;
         }
      );
      
      %file = findNextFile(%path);
   }
   
   return %fileList;
}

This works. My only gripe is the way I have to reference objects out of that SimSet afterwards as...
echo("Level file name is " SPC $levelList.getObject(0).fileName);

as opposed to...
echo("Level file name is " SPC $levelList[0]);

But that really is a small detail that I can live with.
Quote:Your code makes a list of files where the first file is from %path and the rest are from the levels dir? Hardly seems like what you would want to do ;)

Hehe... yep you're absoloutely right. That literal string should be %path like in the call to findFirstFile. Thanks for the help Ben.