1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GML [SOLVED] Reading sections of .ini file 'in order'

Discussion in 'Programming' started by Ehkber, Jun 19, 2018.

Tags:
  1. Ehkber

    Ehkber Member

    Joined:
    Jun 19, 2018
    Posts:
    4
    Hi! I recently created a little system for visual novel-style cutscenes that works by reading values from an .ini file like a play script. It looks like this:
    Code:
    [1]
    S = Justina
    T = Is nine fifty seven. Do we risk turning off the grill?
    A1 = Justina
    A2 = Dee
    A1Flip = true
    A1x = 400
    A2x = 0
    A1i = 0
    A2i=0
    
    [2]
    S = Dee
    T = "If ten bloody years at this job has taught me anything it's that nine fifty seven is the perfect time for some drunk guy to decide he wants a burger.#Peak moron time y'getme. "
    A1Flip = false
    A2 = Dee
    A2x = 550
    A2i = 1
    
    [3]
    S = Justina
    A1Flip = 1
    T = I see. I see.
    It works pretty well but I have one problem that's impeding my efficiency; it requires every section to be manually numbered.
    Every time I write one of these things I either accidentally repeat a number or decide I want to break up a section and have to renumber everything after that point.

    I didn't find anything like this in the .ini section of the manual, but can anyone think of a way where I can count through the sections of an ini file in the order they're written in? I'd love if if I could just leave the sections blank and write without having to think about what number 'scene' I was on.

    Thanks in advance for any help!
     
  2. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    618
    Why aren't you just using file_text_* functions, then?
     
  3. Ehkber

    Ehkber Member

    Joined:
    Jun 19, 2018
    Posts:
    4
    Short answer: Because I can't figure out the first thing about how to devise a way of reading them :v

    I was hoping there would be a way of making this improvement without starting my learning from scratch but if that's the best solution I'll start looking into it
     
  4. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    618
    Well, there are always multiple ways to approach the problem. The INI method isn't a bad one.

    I suppose you could have a "DUMMY" INI entry where you have a key for every section that you need. Then for the key's value it stores the name of the section.

    This way you just read through the dummy keys in order to get the appropriate section name to read from for your text. It would allow you to add/remove pieces and have them out of order since the order would be stored in your dummy section.
     
  5. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    Use json
    buffer_load to open the file into a tmp buffer
    buffer_read with buffer_string to read the file into a string
    json_decode to convert the string into a ds_map based data structure holding the json content
    buffer_delete to free the buffer.

    keep the ds_map with its content and use ds_map_destroy when done with it

    Forget this ini bs man seriously, this aint 1993
     
  6. YanBG

    YanBG Member

    Joined:
    Jun 25, 2016
    Posts:
    576
    Were ini used in 1993? Given they are slower nowadays.
     
  7. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    INI is a windows thing, and they improved its performance on windows 3 or 95 if I remember right, by adding caching so it's faster than straight up text file, still not the best solution for efficiency. Also Yoyo's implementation is NOT the windows version last I tested.

    Still, people should stop thinking ini and start thinking json :)
     
  8. YanBG

    YanBG Member

    Joined:
    Jun 25, 2016
    Posts:
    576
    I plan to use json for save files. Learned ini in 2015 and stopped using it last year.

    Right now i have only txt turned to csv files, which i load to grids(thanks to Juju for the script). So that data isn't hardcoded.
     
  9. Ehkber

    Ehkber Member

    Joined:
    Jun 19, 2018
    Posts:
    4
    The main reason I was drawn to using inis was that the documentation page had a very clear example of how one would lay them out, and it looked like a really quick and initiative way to get words on a page that were usable for my game but also easy to read back for me, a human. Looking at JSON files, it already looks like they have a few more complications and caveats that make me reluctant to switch over.

    However your suggestion use DS_maps did give me an idea for a solution which I just spent the evening spaghetti-ing into existence and seems to be working pretty great!

    Basically I realised that if two sections in a ini file have the same name, Game Maker always favours reading the bottom one. Knowing this I told my cutscene object's creation event to copy the ini containing my dialogue to a new file then use a for loop to grab values from last section labelled "" , add them to lists and the delete that section. My click event then reads through those lists backwards until it runs out of items at which point it deletes the new file and the lists.

    EDIT: For whatever reason this doesn't work when compiled for HTML5 Blerrr X{

    Now to write cutscenes I can just write like this
    Code:
    []
    S = Dee
    T = Yeah, well... somefink like that.
    
    []
    S = Justina
    T = Alright, Dee! Is deal! We will count down the second hands together!
    
    []
    T = Is thirty seconds to go!
    
    []
    T = Is twenty second now, Dee!
    
    []
    T = Dee is almost time!
    
    which is... a super fast way of working! So if living in 1993 is the price to pay for that, I'll take it :v

    Thanks for everyone's replies on this!
     
    Last edited: Jun 22, 2018
  10. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    686
    Not meaning for this comment to be nasty, but you obviously are going to release this in english, and you'll maybe want someone proofreading it. Is this character deliberately speaking in what is referred to as "pidgin english"? From your post it's all correct, but in the code text it's somewhat wrong. If this is intentional, then ignore this :) otherwise these are incorrect:

    is thirty seconds to go
    Dee is almost time
    Alright Dee! is deal
    is twenty seconds to go

    This is the correct way in english:

    it's thirty seconds to go
    Dee it's almost time
    Alright Dee! it's a deal
    it's twenty seconds to go
     
  11. Ehkber

    Ehkber Member

    Joined:
    Jun 19, 2018
    Posts:
    4
    It IS intentional! The character Justina speaks English as a second language, glad it came across as such :p
     
  12. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    686
    No worries! I did wonder about that, and wanted to draw attention to it just in case. There's a czech chap who is my neighbour, and that's how he speaks in english. You can get the intent, even though it's not quite structured properly. So, yeah - you have captured that :)
     

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