1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

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

Discussion in 'Programming' started by Chupiperasaurio Studios, Nov 6, 2019.

  1. Chupiperasaurio Studios

    Chupiperasaurio Studios Member

    Joined:
    May 8, 2017
    Posts:
    12
    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.
     
  2. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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.
     
  3. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    190
    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 :)
     
    nacho_chicken likes this.
  4. Chupiperasaurio Studios

    Chupiperasaurio Studios Member

    Joined:
    May 8, 2017
    Posts:
    12
    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
     
  5. Chupiperasaurio Studios

    Chupiperasaurio Studios Member

    Joined:
    May 8, 2017
    Posts:
    12
    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?
     
  6. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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.
     
  7. Chupiperasaurio Studios

    Chupiperasaurio Studios Member

    Joined:
    May 8, 2017
    Posts:
    12
    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
     
  8. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,460
    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": [...]
    }
    
     
  9. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    404
    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.

    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.

    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.
     
  10. Chupiperasaurio Studios

    Chupiperasaurio Studios Member

    Joined:
    May 8, 2017
    Posts:
    12
    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?
     
  11. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,460
    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.

    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: Nov 7, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice