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

ds list trouble

F

Fenixgalax

Guest
OK here i go...

so..
since i could not find this anywhere on the internet i decided to ask here.

i have a ds list where i would need to add like 2000 words in it.

is there a simple way to do it since adding every word individualy would take lots of time and unneccesery space.

ty in advance =D
 

Ricardo

Member
If the words are in a included file you could read them and solve it using a couple of lines of code.
 
F

Fenixgalax

Guest
i was thinking to manually input them in the ds list as i would later use them at random choice
 

Ricardo

Member
i was thinking to manually input them in the ds list as i would later use them at random choice
You definitely don't need to input 2000 words manually in a ds_list to have them random as we have ds_list_shuffle. If you let me know how are these words stored I could help you with a code snip that feed the ds_list with the words.
 
F

Fenixgalax

Guest
they are not stored anywhere yet.
i guess im a master noob but i wanted to manualy insert them directly in the code itself.

so... how would i store them and where?
 

Ricardo

Member
they are not stored anywhere yet.
i guess im a master noob but i wanted to manualy insert them directly in the code itself.

so... how would i store them and where?
You can store your words in a simply TXT file, one word per line, and include the file in "Included files". Then, feed the ds_list with the words is as easy a write a loop using file_text_eof.
You could also use CSV format and load_csv, but it'll feed a grid instead of a list.
 
F

Fenixgalax

Guest
ok ill check how to do it then ill let u guys know if it worked
NEway ty
 

Tsa05

Member
Text file:
Code:
{
    "list" : [ "word1", "word2", "word3", "word4" ]
}
GameMaker code:
Code:
wordList = -1;

var fname = "nameOfYourFile.txt;
if(file_exists(fname)){    // Load existing json
    var file = file_text_open_read(fname);
    var json = "";
    while(!file_text_eof(file)){
        json+=file_text_read_string(file);
        file_text_readln(file);
    }
    file_text_close(file);
    map = json_decode(json);
        wordList = map[?"list"];
}
The text file above uses JSON formatting, which defines objects as anything between {curly brackets} and lists as anything between [square brackets]. Game Maker natively understands how to convert JSON formatted stuff into data structures, so the code provided results in a ds_list data structure with all of your words in it, named wordList.

The formatting is slightly less forgiving than the above suggestion (every word on a new line), but has the potential advantage of organization. For example, it's possible to store multiple lists in a JSON file. In the example provided, there's 1 list, named "list", but it would be simple to make a second list by simply adding it to the file. If, for example, you needed a list of nouns and a list of verbs:
Code:
{
    "nouns" : [ "noun1", "noun2", "noun3", "noun4" ],
    "verbs" : [ "verb1", "verb2", "verb3", "verb4" ]
}
Then, using the code I supplied above (which simply reads the content of the file into a string and converts it to data structures, looking up each list is simple. Each list is stored in a structure named "map", so...
nounList = map[?"nouns"];
verbList = map[?"verbs"];


The 1-word-per-line solution does work--you'll open the file for reading and loop through each line like my script, but instead of adding each line to a string of JSON, you'll add each line to a ds_list structure. The primary difference is that you'd have to have separate files for each type of list you might need.
 
F

Fenixgalax

Guest
Text file:
Code:
{
    "list" : [ "word1", "word2", "word3", "word4" ]
}
GameMaker code:
Code:
wordList = -1;

var fname = "nameOfYourFile.txt;
if(file_exists(fname)){    // Load existing json
    var file = file_text_open_read(fname);
    var json = "";
    while(!file_text_eof(file)){
        json+=file_text_read_string(file);
        file_text_readln(file);
    }
    file_text_close(file);
    map = json_decode(json);
        wordList = map[?"list"];
}
The text file above uses JSON formatting, which defines objects as anything between {curly brackets} and lists as anything between [square brackets]. Game Maker natively understands how to convert JSON formatted stuff into data structures, so the code provided results in a ds_list data structure with all of your words in it, named wordList.

The formatting is slightly less forgiving than the above suggestion (every word on a new line), but has the potential advantage of organization. For example, it's possible to store multiple lists in a JSON file. In the example provided, there's 1 list, named "list", but it would be simple to make a second list by simply adding it to the file. If, for example, you needed a list of nouns and a list of verbs:
Code:
{
    "nouns" : [ "noun1", "noun2", "noun3", "noun4" ],
    "verbs" : [ "verb1", "verb2", "verb3", "verb4" ]
}
Then, using the code I supplied above (which simply reads the content of the file into a string and converts it to data structures, looking up each list is simple. Each list is stored in a structure named "map", so...
nounList = map[?"nouns"];
verbList = map[?"verbs"];


The 1-word-per-line solution does work--you'll open the file for reading and loop through each line like my script, but instead of adding each line to a string of JSON, you'll add each line to a ds_list structure. The primary difference is that you'd have to have separate files for each type of list you might need.
does the file format have to be .json or .txt?
 

Tsa05

Member
does the file format have to be .json or .txt
Any file extension you like will work; it should just be a plain text file (ansi or utf8...basically, anything you make in something like Notepad).
You can call it a .txt file or a .json file or a .Fenix file, as long as there's just plain text in there, and you provide the correct filename when you open the file for reading.
 
F

Fenixgalax

Guest
ok... once text file is created how do i make ds list read it ?
im sorry im real noob at this
 
Top