• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

(SOLVED)reading/writing to ini files

Adjud

Member
Hi,

back in GM7 i was able to read and write to .ini files in lets say "C:\game\gameload.ini"
its apparently different with the new edition of gm2..

so steer me in the right direction, I disabled sandbox in windows for gm2 then I made this code to test if it works and nothing happens:

Code:
ini_open("C:\rpg\gameload.ini");
score = ini_read_real("pref", "score", 0 );
ini_close();
I simply want to be able to access my ini file, read it and write to it if needed. im going to work on a save/load system using ini's but i need to make sure i get it right before attempting that.

Thank you for any help and steering me in the right direction of how to achieve this.
 

curato

Member
the folder may be the issue. by default in gms2 they save to the sandbox (the pre approved area) by default. which in windows would be C:\Users\<username>\AppData\Local\<gamename> where <username> and <gamename> would be replaced with the appropriate values. If you have a specific reason to save other places you may need to disable the sandbox. I think the default setting is perfect for user setting like volume graphic setting, control settings etc. if you are looking to save and load a bunch on game data especially if you need security on it dynamic data types like ds_map are a good way to go it. It even has a secure save and secure load command that takes care of basic security for you.
 
Is "C:\rpg\" the same directory the game is located in? Because GMS2 is stupid about this and still sandboxes that folder and any subdirectories even if you disable the sandbox.
 

Catastrophe

Member
Right, disabling the sandbox has, unfortunately, been acknowledged by the staff to basically do absolutely nothing. As for how to do this in GMS2 these days, I'm not sure. There is a file extension system on the marketplace that I'm going to try out that claims to work around the whacky file system, but I don't know if it works yet. But that's your best bet. You're like the tenth person. myself included, that hates this, maybe they'll do something if these topics keep getting made >.>

Edit: This is assuming you want to write outside the sandboxed area. If you just want to read/write somewhere, just leave out program_directory/working_directory/etc and you'll find your file waiting for you in %Appdata%/local/yourgame
 
Last edited:

Adjud

Member
hmmm, im completely clueless on this new system, with ini files.

so how would i add in a file in program directory?
Code:
ini_open("program_directory"+"gameload.ini");
ini_write_real(pref,score,0);
ini_close();
I don't really care where it stores, just as long as I can store data in it that can be reused when the game is opened again.
 

Catastrophe

Member
tl;dr you can't to the program_directory.

But what you can do is

ini_open("gameload.ini");
ini_write_real(pref,score,0);
ini_close();

ini_open("gameload.ini");
ini_read_stuff_im_too_lazy_to_look_it_up();
ini_close();

And this will simply work. Because your file is getting created, just in a stupid location ;)
 

Adjud

Member
I used this code
Code:
ini_open("gameload.ini");
ini_write_real("pref","score","0");
ini_close();
after creating a UTF-8 ini file in datafiles, its empty inside then i added it to included files
after running the game and closing it I check the ini file in the datafiles location and it is blank inside..
 

Catastrophe

Member
Right. Datafiles I believe is inside your project directory, according to the manual, which you cannot write to. As we've said. We're just saying if you want to make a file at runtime and read from it you can do so. If, however, you want to write to a file you export the game with then, well, join the club man. I think you can read from it, though. Like try reading from project_directory + yourfilehere with sandbox disabled. I haven't tried though.
 
I used this code
Code:
ini_open("gameload.ini");
ini_write_real("pref","score","0");
ini_close();
after creating a UTF-8 ini file in datafiles, its empty inside then i added it to included files
after running the game and closing it I check the ini file in the datafiles location and it is blank inside..
Using this code will write the ini file to C:\Users\<username>\AppData\Local\<gamename>
 

Adjud

Member
Okay,

so I had no problem reading from the ini, but i made a collision event for a boss character, its supposed to write everything into the ini file, then activate the boss battle, after the battle if the character survives its suppose to read everything from the ini to r"reload" the character back where it was before the boss battle. but its giving me an error as attached in image below.

Step Event for objcontroller:

Code:
with objchar
    if place_meeting(x, y, objpviper)
        with other {
            ini_open("gameload.ini");
            ini_write_real("Character","hp",hp)
            ini_write_real("Character","mp",mp)
            ini_write_real("Character","ap",global.ap)
            ini_write_real("Character","str",global.str)
            ini_write_real("Character","dex",global.dex)
            ini_write_real("Character","mag",global.mag)
            ini_write_real("Character","def",def)
            ini_write_real("Character","mdef",mdef)
            ini_write_real("Character","expreq",expreq)
            ini_write_real("Character","x",objchar.x)
            ini_write_real("Character","y",objchar.y)
            ini_write_real("Character","wepdmg",wepdmg)
            ini_write_real("Character","level",global.lvl)
            ini_write_real("Character","Att",global.att)
            ini_write_real("Character","hexp",global.hexp)
            ini_write_real("Game","Day",day)
            ini_write_real("Game","Hour",chour)
            ini_write_real("Game","Min",cmin)
            ini_write_real("Game","Sec",csec)
            ini_write_real("Game","AMPM",mn)
            ini_write_real("Game","cs",clockset)
            ini_close();
        }
any idea why its causing an error and not writing any of the data to the ini file? and I did check the right file in "C:\ windows \ <user> \ appdata \ local \ <mygame> gameload.ini"
Thanks for any help :)
 

Attachments

chamaeleon

Member
any idea why its causing an error and not writing any of the data to the ini file?
Because mn is a string containing am or pm and you are calling a function that expects a number instead of a string as argument, per the error message?
 

Adjud

Member
mn just tells the game weather its AM or PM, so mn="AM" in a step event and alarm event it changes to "PM" so thats why its having an issue, i just didn't think it had to be a string unless you put string()
 

chamaeleon

Member
mn just tells the game weather its AM or PM, so mn="AM" in a step event and alarm event it changes to "PM" so thats why its having an issue, i just didn't think it had to be a string unless you put string()
Use the aptly named ini_write_string() function.
 
Top