Pages: 1
  Print  
Author Topic: Reading .ini files  (Read 2449 times)
Offline (Unknown gender) kam86
Posted on: June 12, 2014, 09:32:24 PM
Member
Joined: Jun 2014
Posts: 5

View Profile
Hello, first time poster here. I've recently switched from Game Maker to ENIGMA, but I'm having some trouble with .ini functions. I'm attempting to access a number stored in an .ini file--here's a sample of my code:

ini_open("file.ini");
global.value=ini_read_real("value","1",0)
ini_close()


For some reason, only the default value (0) is retrieved, even though the .ini file looks like this:

[value]
0   =   400
1   =   200


Strangely enough, I seem to be able to write to the .ini file just fine. Can anyone tell me where I'm going wrong, or suggest a work around? Any help is appreciated.
« Last Edit: June 12, 2014, 09:35:06 PM by kam86 » Logged
Offline (Male) time-killer-games
Reply #1 Posted on: June 12, 2014, 10:08:30 PM

Contributor
Location: Virginia Beach
Joined: Jan 2013
Posts: 1166

View Profile Email
first of all using integers as keys (the second arg) is a very bad practice. It's one thing if you do "key0=blah; key1=blah;" but to have a number by itself as the key is very confusing and is probably the issue.

However I can't mess with ENIGMA atm, you'll need further help from someone else. inis worked just fine for me in the past but it might be broken right now so idk.. Make sure the INI is in the same directory as your gmk/exe otherwise it will be the default value 0 because it might have not been found.
« Last Edit: June 12, 2014, 10:15:31 PM by time-killer-games » Logged
Offline (Male) Goombert
Reply #2 Posted on: June 13, 2014, 02:25:59 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
All I know is they use the native functions on Win32, if GM uses those functions, there should be no reason why numerical keys would not work.

All of the functions are implemented here.
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Platforms/Win32/WINDOWSregistry.cpp

Can you provide an example GMK or something as well so that I can test?
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Male) time-killer-games
Reply #3 Posted on: June 13, 2014, 04:09:48 PM

Contributor
Location: Virginia Beach
Joined: Jan 2013
Posts: 1166

View Profile Email
So you mean this doesn't work on Linux? You know it may be easier to go lazy (like me) and rely on pre-built Win32 API but if you want to go cross-platform I know someone who made a ini reader and writer on the monkey-x forums who might be able to help if y'all need it. Just let me know. :)
Logged
Offline (Unknown gender) Darkstar2
Reply #4 Posted on: June 13, 2014, 05:14:01 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
Been following this topic and can't help but ask a question, sorry if this might seem slightly O/T, but why do people still rely on INI functions ?  There are many different ways to store settings, some beginner some more advanced.  The easy method by using your own files and using file text reading commands it's more flexible and opens the door for more advanced stuff eventually.

Logged
Offline (Male) Goombert
Reply #5 Posted on: June 14, 2014, 11:54:25 AM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Darkstar2, no idea, you're right, using ini is really unnecessary. It's extremely simple to write your own file format, just skip lines that start with '//' for comments and bam you can make a format with descriptions making it easy for end users to edit. It would probably read faster too.

If you need more advanced structure, yaml is the way to go, not ini.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #6 Posted on: June 14, 2014, 12:09:06 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
I'd like to write new functions to handle it, sort of like INI but enhanced, faster, simpler.  But I don't know how to integrate that into the extensions system.
Logged
Offline (Unknown gender) kam86
Reply #7 Posted on: June 14, 2014, 01:08:22 PM
Member
Joined: Jun 2014
Posts: 5

View Profile
first of all using integers as keys (the second arg) is a very bad practice. It's one thing if you do "key0=blah; key1=blah;" but to have a number by itself as the key is very confusing and is probably the issue.

However I can't mess with ENIGMA atm, you'll need further help from someone else. inis worked just fine for me in the past but it might be broken right now so idk.. Make sure the INI is in the same directory as your gmk/exe otherwise it will be the default value 0 because it might have not been found.

I'll try changing things as per your suggestion, but I don't think it's using numbers as keys that's causing the problem. This is a direct port from Game Maker, and I didn't have a problem there. Also, I've set things up this way so I can generate a random number and then use that number to access a random key value. I'm not sure how else I would go about doing this. Perhaps it is a momentary issue with ENIGMA, as I haven't been able to get .ini read functions to work in any other games.
Logged
Offline (Unknown gender) kam86
Reply #8 Posted on: June 14, 2014, 01:12:29 PM
Member
Joined: Jun 2014
Posts: 5

View Profile
Darkstar2, no idea, you're right, using ini is really unnecessary. It's extremely simple to write your own file format, just skip lines that start with '//' for comments and bam you can make a format with descriptions making it easy for end users to edit. It would probably read faster too.

If you need more advanced structure, yaml is the way to go, not ini.

I'm a fairly adept Game Maker user, but I don't have a lot of experience with C++. I chose .ini files because the commands are simple in Game Maker and I needed to read and write to an external file. Can you offer some resources or advice on using yaml files? I really don't care which type of file I'm using, but I'm not familiar with anything but .ini so I could use a bit of help.
Logged
Offline (Unknown gender) kam86
Reply #9 Posted on: June 14, 2014, 08:35:41 PM
Member
Joined: Jun 2014
Posts: 5

View Profile
I was able to reproduce the problem in a test file, so I've uploaded the .gmk file and the .ini file.

This file contains a single object which reads an .ini file and displays the value--however,  because I still can't get it to read correctly I'm guessing the function isn't working in ENIGMA. Can anyone confirm this, or at least give me an alternate suggestion for how to read and write to an external file? My game is almost finished, other than this one issue.

Thanks (Y)
Logged
Offline (Male) Goombert
Reply #10 Posted on: June 14, 2014, 08:52:15 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
The issue had nothing to do with ini functions it was the file/directory name.

Change the first line to the following and it works in ENIGMA.
Code: (EDL) [Select]
ini_open(working_directory + "/test.ini")
The following will also work.
Code: (EDL) [Select]
ini_open("./test.ini")
Both of those will work in both ENIGMA and GM, so go ahead and just do that.

Apparantly the windows INI functions don't recognize the set working directory.
http://stackoverflow.com/questions/22138959/c-getprivateprofilestring-read-ini-file-from-current-directory
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx

So GM must be appending it to the functions, I have no plans to fix that. It is more of a bug in Windows than in ENIGMA.
« Last Edit: June 14, 2014, 09:00:20 PM by Robert B Colton » Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #11 Posted on: June 14, 2014, 08:57:34 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
I discovered this on my own once when playing with external files, :D

One thing to note, kam86, I noticed you assigned a sprite to an object in your test.  You don't need to do that if you don't need to use the sprite.  You can create empty object assigned to nothing, code in them will be executed, so long as they are active and placed in the room.

Also, try to get to learn GML it is much simpler than the D&D, you could have replaced the D&D actions with this:
(example to drawing at x/y 10,10)

Code: (GML) [Select]
draw_text(10, 10, string(global.display));
If you specify no fonts and colour, the default will be a white, 9 point font.
In GM you had to declare a font and color, in ENIGMA it sets a default for you.

Logged
Offline (Unknown gender) kam86
Reply #12 Posted on: June 14, 2014, 09:01:10 PM
Member
Joined: Jun 2014
Posts: 5

View Profile
The issue had nothing to do with ini functions it was the file/directory name.

Change the first line to the following and it works in ENIGMA.
Code: (EDL) [Select]
ini_open(working_directory + "/test.ini")
Apparantly the windows INI functions don't recognize the set working directory.
http://stackoverflow.com/questions/22138959/c-getprivateprofilestring-read-ini-file-from-current-directory
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx

So GM must be appending it to the functions, I have no plans to fix that.

You, Sir, are a saint. That fixed the problem.  :v:
This also explains why I was able to read and write from a text file, which I learned recently using the proper GML.
Logged
Offline (Male) Goombert
Reply #13 Posted on: June 15, 2014, 10:47:01 AM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Yeah the difference is because our file text reading functions are part of the c standard library and UNIX compatible, our INI functions only exist for Windows and are platform dependent. It's just Microsoft not following standards, if you have a problem with the inconsistency, take it up with them.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Pages: 1
  Print