GMS 2 Most elegant way to get a string array from a file?

I am making a system that displays my game's conversations on a textbox and I think the only way to make it elegant and efficient is to stream all the needed text from a file every time a dialogue object is created so that each time a conversation happens I just have a not-so-long text array and display each element on a single screen. I plan to have a global variable that knows on which line of the file did the previous conversation end and use it to read the next chuck chunk on the next conversation.

My main doubt is wether to use an ini file or a txt and with what functions.

Additionally I'd want to know how do I keep people from just opening the file and transform my script into an adults movie or something.
 

Catastrophe

Member
It's probably good to have it in files anyways in case you want fan-made translations.

Inis are useful for selecting specific lines, so it sounds like the best way to handle it if you're loading on the fly. Break up your game in some way (chapters, characters, etc) and have an ini for each.

Alternatively, use a text file and load it all into game at once at the start. Isn't good for memory, but is just fine for modding/organization.
 

Bart

WiseBart
As another option you could store the conversations in JSON and load them with json_decode.

And you don't have to "stream" from a file. No need to optimize where you probably don't have to :)
 
As another option you could store the conversations in JSON and load them with json_decode.

And you don't have to "stream" from a file. No need to optimize where you probably don't have to :)
This way I'd have constantly in memory a quite big ds_map with all the information of the json file. It would be basically the same as with a txt file, fut faster to accses. I'd prefer to just load the dialogue per chapters or something like that
 
It's probably good to have it in files anyways in case you want fan-made translations.

Inis are useful for selecting specific lines, so it sounds like the best way to handle it if you're loading on the fly. Break up your game in some way (chapters, characters, etc) and have an ini for each.

Alternatively, use a text file and load it all into game at once at the start. Isn't good for memory, but is just fine for modding/organization.
I like the idea of loading chapters or characters. The thing is; do I load these parts from sections of an ini, froms different inis, from different txt or even from different json files?
 

Catastrophe

Member
If you aren't loading it all at once, then separate files no doubt. As for ini/txt/json, kinda your choice, but ini/json is mostly helpful if you want to store it into a ds_map or look up bits at a time. txt is only going to be useful if you want to load it all at once and just store each piece of dialogue by a simple numerical index, not elegant but sounds like something I'd do lol.
 
As for ini/txt/json, kinda your choice, but ini/json is mostly helpful if you want to store it into a ds_map or look up bits at a time. txt is only going to be useful if you want to load it all at once and just store each piece of dialogue by a simple numerical index, not elegant but sounds like something I'd do lol.
Well, a simple array of string is elegant if you only have to display the elements seguentially. You don't need to have keys if all you are going to do is increment a numeric variable and get the next element all the time
 

GMWolf

aka fel666
If you don't have to type it all by hand, or can write a quite formatting tool, I would use JSON.

You could use a single JSON like so:
Code:
{
    "Chapter 1": [
         "First line of chapter 1",
         "Second line of chapter 1",
         "Final line of chapter 1"
     ],
    "Chapter 2": [
        "Here is chapter 2!",
        "It's more interesting than chapter 1!",
        "However it isn't any longer."
    ]
}
Then load the whole JSON and keep it all in memory (it's small, don't worry. Actually streaming the data from the file is not worth it.).

In order to avoid loading your entire game text at once, you could split it up into multiple JSON files, and have a root JSON that tells you where to find the other chapters.
Root JSON:
Code:
{
     "chapter 1" : {
           "file": "chapter_1_and_2.json",
            "Name": "chapter 1"
      },
      "chapter 2" : {
            "file": "chapter_1_and_2.json",
            "Name" : "chapter 2_a"
      }
      "chapter 3": {
            "file": "foobar.json",
            "Name": "bazbaz"
       }
}
chapter_1_and_2.json
Code:
{
    "chapter 1" : [...],
    "chapter 2_a" : [...]
}
foobar.json
Code:
{
    "bazbaz": [...]
}
 
I am making a system that displays my game's conversations on a textbox and I think the only way to make it elegant and efficient is to stream all the needed text from a file every time a dialogue object is created so that each time a conversation happens I just have a not-so-long text array and display each element on a single screen. I plan to have a global variable that knows on which line of the file did the previous conversation end and use it to read the next chuck chunk on the next conversation.
This is almost 100% exactly how I did it in my textbox engine. You're definitely on the right track. It got used in VA11 HALL-A, so if you want to take a look at the resulting files, give the files of that game a peek.

My main doubt is wether to use an ini file or a txt and with what functions.
There's no good reason to use .ini in this situation when JSON exists. I probably would've used JSON over .txt files for my engine, but built-in JSON functions weren't in GameMaker when I wrote it.

Additionally I'd want to know how do I keep people from just opening the file and transform my script into an adults movie or something.
Honestly, don't do anything about this. The negatives to encrypting your text files far outweigh the benefits. You'll lose out on people that are more "casual" about modding and would be willing to translate your game for others, and also more innocent text mods. The people that make "adult" text mods that add things like changing everyone's name to swears or adding crude references every other sentence generally do so for personal enjoyment.
 
Okay so the conclusion I extract from everyone's anwers is that: json is better and I should only load the text I need each time.
Now I have doubts on the internal structure to acces text. My idea, based on what you said, is to use a ds_map to save the text and an numerical index variable that increments to get the text from the map. Is it the most fitting approach?
 

GMWolf

aka fel666
I should only load the text I need each time.
You should strike a balance between only loading the text you need, and loading enough text at a time to stay efficient.
I think loading entire chapters at a time is probably a good balance.

My idea, based on what you said, is to use a ds_map to save the text and an numerical index variable that increments to get the text from the map.
Loading JSON will give you nested data structures of maps and lists.
If you structure it like I'm my post, you will have a top level map, containing lists of strings.

You probably want to keep your strings in a list rather than a map if you want to access it with a numerical index.
Then you can have a map of chapters to lists, and you can add or remove chapters from the map as needed.
 
Last edited:
Top