• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Pulling data from an array

J

Joe Banko

Guest
I would like to pull data from an array which I am reading from a text file I created. Below is the code I use to pull the data into the array from a tab-d text file.

GML:
//projectile array
projectiles = array_create(3);

//projectile file location!!!!!
projectilefile = working_directory + "projectiles.txt";

//point to text file
var rline, bogus
fid = file_text_open_read(projectilefile);
if fid {
    for (var i = 0; i<3; ++i)
    {
        //make a projectile entry
        projectiles[i] = file_text_read_string(fid);
        file_text_readln(fid);

    }
    file_text_close(fid);
    var bogus2 = 1;//for debug breakpoints
}
Now that I have an array I would like to pull the data out using the first "column" which has a unique name in it. How do I pull the values out of the line which matches the first column? For instance my first entry into the array would be spear and I want to do something like:
Code:
if projectiles[@spear",P_ENABLED]
{
    hspeed = projectiles[@"spear", P_SPEED];
    sprite_index = projectiles[@"spear",P_SPRITE_INDEX];
    projectile_damage = projectiles[@"spear",P_DAMAGE];
    ...
    ...
}
In my macros file I have defined which columns relate to the projectile:
Code:
//projectile
#macro    P_ID                0
#macro  P_LAUNCHER            1
#macro    P_SPRITE_INDEX        2
#macro    P_IMPACT_INDEX        3
#macro    P_RANGE                4
#macro    P_SPEED                5
#macro    P_FIRE_DELAY        6
#macro    P_FRICTION            7
#macro    P_WEIGHT            8
#macro    P_DAMAGE            9
#macro    P_HEAD                10
#macro  P_TRAIL                11
#macro  P_FIRE_SND            12
#macro  P_IMPACT_SND        13
#macro  P_ENABLED            14
 
J

Joe Banko

Guest
Is this the correct way to pull the data out of the array?

Thanks,

joeb
 

chamaeleon

Member
Is this the correct way to pull the data out of the array?
No, you are creating a 1-dimensional array, which you can only use a single number to get data out of it, none of this string plus number indexing stuff. It is possible that you are better off using a ds_grid instead and split your string by the tabs and store each substring in a cell in the current row (while still using only numbers as row and column indicators). A ds_map would allow you to look something up by a string, if it's a hard requirement.
 
Top