• 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!

Trying to read single words from a text file

D

Deeo

Guest
I have a text file exported from an excel sheet that has a bunch of info for my game loaded into it in this format:

name,type,spriteid,objectid
name,type,spriteid,objectid
name,type,spriteid,objectid
name,type,spriteid,objectid

and i figured out how to get the entire line with file_text_read_string(); but i can't seem to find a way to just get, for example, just the name and add it to a 2d array like

array[item, item info1] = name;
array[item, item info2] = type;
array[item, item info3] = spriteid;
array[item, item info4] = objectid;

Is there a built in function I'm missing for this?
 

Perseus

Not Medusa
Forum Staff
Moderator
A simple string parser should be able to do that.

Code:
/// @description Parse the string and set values to appropriate array indexes
/// @param {real} array The array that you want to save the data to
/// @param {real} index Index of the first dimension
/// @param {string} string Text obtained from the file

var text, num, len, temp, _arr, ind;
text = argument[2];
num = 0;
len = string_length(text);
temp = "";
_arr = argument[0];
ind = argument[1];

for (var i = 1; i < len; i += 1) {
   temp += string_char_at(text, i);
   if ((string_char_at(text, i + 1) == ",") or (i == len)) {
      _arr[@ ind, num] = temp;
      temp = "";
      i += 1;   // Skip ","
      num += 1;
   }
}
Using the following code:
Code:
array[2, 0] = ""; // Declare the array
sample = "name,type,spriteid,objectid";
string_parse(array, 1, sample);
...would fill index 1 of the first dimension with the following data:
Code:
array[1, 0] - name
array[1, 1] - type
array[1, 2] - spriteid
array[1, 3] - objectid
 
D

Deeo

Guest
Ah so you just get the characters and put them together in a variable to make the word and stop at the comma!

You sir are a god lol, never would have thought of that!!
 
Top