1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML ini load questions... if "file exists"

Discussion in 'Programming' started by Mythi.T, Nov 7, 2018.

  1. Mythi.T

    Mythi.T Member

    Joined:
    Oct 11, 2018
    Posts:
    44
    Hello I have some questions about ini files

    in my code I have this:
    Code:
    if !file_exists("aiSide.ini"){
        needsaves_side = 1
    }
    else needsaves_side = 0
    
    if needsaves_side = 0{
        ini_open("aiSide.ini");
        global.testOneGen = ini_read_real("testOne", "gen", 0)
        global.testOneGrade = ini_read_string("testOne", "grade", "unsure...")
        global.testOneHigh = ini_read_real("testOne", "high", 0)
        ini_close()
    }
    Do I need to have the "file exists" if statement? will it work? Can I remove it and add something else? what would bet hat something else? if you can solve/answer these questions that would help bunches.

    also must I do this "file exists" if statement saving, not just loading?

    SITUATION (so I don't have to explain to the questioners later)
    Loading is automatic when the game starts
    Saving is automatic upon level end
    I Know how to read/write inside ini files, I just don't know how if there will e an error because I never made a ini file prior

    Thanks
     
  2. brupibo

    brupibo Member

    Joined:
    Oct 26, 2018
    Posts:
    9
    I think you could check if key exists, instead of file, because you're going to create it anyway.
    Use ini_open and check if testOne key exists (don't remember exactly the code). If it does, make the loading logic, if doesn't, make it write a default value for the keys.
     
  3. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    642
    From the documentation :
    [​IMG]

    So basically, when you read a value from an ini file using the ini_* scripts, you don't need to check if it exists because the other ini_ scripts allow you to provide a default value if the key doesn't eixsts.

    Also not that until you actually write a value, the file will NOT be created, so you can read a file that doesn't exists without creating one!

    In other words:
    Code:
    ini_open("aiSide.ini");
    if(ini_read_string("testOne", "grade", "") == "") {
      // The ini file doesn't exists or wasn't saved properly
    }
    else {
      // The ini file exists
    }
    
    You only need to use the if condition if you want to avoid loading anything if the file doesn't exists.
    Since the ini_ scripts have a default value parameter, then you can load data even if the file doesn't exists, the scripts will simply return the default value.

    This was specifically made to make it super easy to load/save data with GameMaker.

    I hope this answers your question.
     
  4. Mythi.T

    Mythi.T Member

    Joined:
    Oct 11, 2018
    Posts:
    44
    Yes it does. Thankyou
     
  5. Mythi.T

    Mythi.T Member

    Joined:
    Oct 11, 2018
    Posts:
    44
    oops. This message was mistake. Idk how to delete
     

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