# For-loop with LoadVars. How to distinguish files? (OMG He's back)

**URL:** <https://forum.kirupa.com/t/for-loop-with-loadvars-how-to-distinguish-files-omg-hes-back/214534>\
**Category:** Uncategorized\
**Created:** [January 29, 2007, 10:03pm UTC](https://forum.kirupa.com/t/for-loop-with-loadvars-how-to-distinguish-files-omg-hes-back/214534 "2007-01-29T22:03:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![CEOofAEP](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@CEOofAEP](https://forum.kirupa.com/u/CEOofAEP)\
**Post date:** [January 29, 2007, 10:03pm UTC](https://forum.kirupa.com/t/for-loop-with-loadvars-how-to-distinguish-files-omg-hes-back/214534/1 "2007-01-29T22:03:20Z")

</div>

Wow it’s been so long… (and I still seem to stumble across the same problems ☹ )

Here’s the deal. I am working on a game and I want to have the layout of the levels in textfiles.

Each textfile is named “level(n).txt” - where (n) is the number of the level (“level1.txt”, “level2.txt”, etc).

The level-“data” in those files is simply several rows of comma-seperated values that describe what kind of tile to use at that position. So typically, it would look something like this:

```auto
100,100,200,300,100
100,200,200,200,100
100,300,200,100,100

```

(and so on - you get the idea)

My question is, how do I use just one loadVars-function together with a loop (other ideas are welcome) to retrieve the data… and be able to tell which data the function is currently loading?

I used to do it like this:[LIST=1]  
[_]set a variable (on \_root) as a “distinguisher” of the level number that is currently loaded  
[_]load the first level (with a timed call to the function with “setInterval”)  
[_]assign the retrieved data, if successfully loaded (with the “onLoad” even function)  
[_]upon recurrence of the Interval, check if data was assigned (or if assigning failed) and…  
[\*]load next level … [rinse, repeat][/LIST]My question now really is (without further ado):

Can’t I just call the “onLoad” function for the “loadVars” in a loop so it kind of loads all levels at once? If so, how does the function know which level it just loaded?

Or am I doing something wrong?

Here’s how I’d like it to work… but it’s probably even simpler than this:

```auto
gamedata_Arr:Array = Array();

distributeValues = function(success) {
  if (success) {
  // assign the data (that was loaded) to "gamedata_Arr",
  // Array-Key = Level Number (how?)
  }
}
levelValues = new LoadVars();
levelValues.onLoad = distributeValues;

// The loop to loadeverythingatonce (whee!)
loadLevels = function(howmanylevels) {
  for (var ln = 1; ln <= howmanylevels; ln++) {
    var filename:String = "level"+ln+".txt";
    levelValues.load(filename);
  }
}
loadLevels(3);

```

Yea… I’m lost… any way to help me? 🙂
