Nathan Laing
Member
Hello!
I have been stuck on a 2D array iteration for 6 hours now, I'd love some assistance.
I am successfully reading in data from a text file--the data is:
The code below takes each value in between the "|" and stores it in a 2D array, val [i, j].
The first series goes in ok, i.e. val[1, x] all the way to val[1, 16]. But the next series, val[2, x] does not.
The reason, as far as I can tell, is that I am incrementing the j value twice--once at the beginning of the for loop and once again within the for loop. I know it's wrong, but I can't figure out how to keep track of the for loop counter AND the array position at the same time, in this case.
The end result, if you were to type the values 'manually' into Gamemaker code is this, but for all 8 guns;
I have been stuck on a 2D array iteration for 6 hours now, I'd love some assistance.
I am successfully reading in data from a text file--the data is:
GUN1|2|30|120|1|-1|10|20|1|0|2|0|30|3|0|-4|
GUN2|6|15|45|1|-4|10|5|1|8|3|45|15|3|0|-4|
GUN3|11|30|120|1|5|5|3|1|13|0|100|30|3|0|-4|
GUN4|7|30|120|3|9|4|4|1|17|0|110|30|3|0|-4|
GUN5|10|8|120|1|-17|10|5|4|14|0|130|8|3|0|-4|
GUN6|8|5|15|10|-40|20|15|1|18|0|200|5|3|0|-4|
GUN7|9|20|200|2|-3|20|15|1|18|0|200|20|7|0|-4|
GUN8|9|200|1000|2|5|10|15|1|18|0|100|200|7|0|-4|
GUN2|6|15|45|1|-4|10|5|1|8|3|45|15|3|0|-4|
GUN3|11|30|120|1|5|5|3|1|13|0|100|30|3|0|-4|
GUN4|7|30|120|3|9|4|4|1|17|0|110|30|3|0|-4|
GUN5|10|8|120|1|-17|10|5|4|14|0|130|8|3|0|-4|
GUN6|8|5|15|10|-40|20|15|1|18|0|200|5|3|0|-4|
GUN7|9|20|200|2|-3|20|15|1|18|0|200|20|7|0|-4|
GUN8|9|200|1000|2|5|10|15|1|18|0|100|200|7|0|-4|
The code below takes each value in between the "|" and stores it in a 2D array, val [i, j].
Code:
var file, str, str_pos, str_temp, val, num, i, j, test;
file = file_text_open_read("guns.txt");
if (file != -1) // if the file exists...
{
str_pos = 1;
str_temp = "";
j = 1;
for (i = 1; i < 9; i++) // for as many weapons that exist...
{
str = file_text_read_string(file); // read a line into str
//show_message(str); // check what we got
file_text_readln(file); // skip to next line
for (j = 1; j < 17; j++) // for as many properties as each gun has...
{
while (str_pos < string_length(str)) // while we are not at the end of the string...
{
while (string_char_at(str, str_pos) != "|") // while we have not reached a "|" at said position...
{
str_temp += string_char_at(str, str_pos); // our temporary string gets a new character added to it, from said position
str_pos += 1; // increment the position in the string
}
// ...We reached a "|" so store what we have so far as a value in the array...
if (j == 1) // if the first iteration is the weapon description, must store as text value
{
val[i, j] = string(str_temp); // store it as a text value, not a real number
str_pos += 1; // increment the string position, taking as past the "|"
str_temp = ""; // clear the temp variable, ready for fresh input
}
else
if (j > 1) // j must not be 1, so we're on to the real numbers
{
val[i, j] = real(str_temp); // store it as a real number
str_pos += 1; // increment the string position, taking as past the "|"
str_temp = ""; // clear the temp variable, ready for fresh input
}
//test = "TEST" + string(i) + " " + string(j);
//show_message(test);
j++;
}
}
}
}
The first series goes in ok, i.e. val[1, x] all the way to val[1, 16]. But the next series, val[2, x] does not.
The reason, as far as I can tell, is that I am incrementing the j value twice--once at the beginning of the for loop and once again within the for loop. I know it's wrong, but I can't figure out how to keep track of the for loop counter AND the array position at the same time, in this case.
The end result, if you were to type the values 'manually' into Gamemaker code is this, but for all 8 guns;
Code:
gun[i,1] = 'GUN1';
gun[i,2] = 2;
gun[i,3] = 30;
gun[i,4] = 120;
.
gun[i,16] = -4;