MODULE: IniFile2 1.0.0 (AGS 3.x) - Read and write configuration to files

Started by Wyz, Sat 18/08/2012 13:35:49

Previous topic - Next topic

Wyz

I've made a module that can read and write .ini files. This can be used to store configuration for a game regardless of savefiles. It is really easy to use and self documented. Type:
Code: AGS

IniFile ini;
ini. //(a list of possible functions will appear)

When you are curious what a function does, simply select it and the auto-completer will tell you what it does.

I just noticed there is a module that does the same thing with the exact same name which I was unaware of (heck I was pretty sure I checked before I made this). The module is part of a game module I'm making for someone and if I knew there was already one it could have saved me some time... This version is made for AGS 3.x and the old one for 2.x so I thought it might still be a wise idea to publish it.

The module uses the ini format which is commonly used by windows applications. It looks like this:
Code: ini

[section1]
key1=value1
; comments can appear on empty lines, preceded by a semicolon
key2 = value2
key3 =value3

[section2]
number1=42
boolean1=1


Special characters in key and section names are not supported and spaces in values at the left and right will be trimmed. That is basically it.

I hope that it will be useful to someone. Any questions, comments or requests: feel free to post them here!
Life is like an adventure without the pixel hunts.

Construed

I felt sorry for myself because I had no shoes.
Then I met the man with no feet.

Miori

The download is no longer available i'm afraid, but i'm very interested in your module. Could you please post an new downloadlink? Pleeease :-D

Wyz

Life is like an adventure without the pixel hunts.

Miori

Ah thank you very much :D

edit: Ahm... sorry, you said it is easy to use, but maybe i'm the sort of people who need an explanation for idiots XD Could you just give me an example, so I can see how it works? :)

For example... save the string variable TEST to ini.ini, load it and show the saved string in a label? That would make it understandable for me, i think. Examples are more useful to me than long explanations XD

Wyz

Sure :)

Here is an example room script. It will load the ini file and print some values from it and then it will put some values in the ini file and save it again. Let me know if you have more questions :)

Code: ags

IniFile ini;

function LoadIni()
{
  ini.Load("ini.ini");
  
  String key1 = ini.Read("section1", "key1", "default");
  int key2 = ini.ReadInt("section1", "key2", 0);
  float key3 = ini.ReadFloat("section1", "key3", 0.0);
  bool key4 = ini.ReadBool("section1", "key4", false);
  
  Display("key1 = %s", key1);
  Display("key2 = %d", key2);
  Display("key3 = %f", key3);
  if (key4)
    Display("key4 = true");
  else
    Display("key4 = false");
}

function SaveIni()
{
  ini.Write("section1", "key1", "Test");
  ini.WriteInt("section1", "key2", 1234);
  ini.WriteFloat("section1", "key3", 12.34);
  ini.WriteBool("section1", "key4", true);
  
  ini.Save("ini.ini");
}

function room_AfterFadeIn()
{
  LoadIni();
  SaveIni();
}
Life is like an adventure without the pixel hunts.

RickJ

Wow!  What a blast from the past grin:

I wrote the old version of the module and and think it was probably the very first bit of code to use the AGS module system.  I'm glad there is a newer version ... it's something that need to be done for a long time.  Btw, I don't have a problem if anyone wants unify both versions.  The module guidelines describe a method of keeping module versions and dependencies straight (I'm sure Wyz is already knows this).  Anyway Wyz, nice work... 

Miori

Ah now I got it :D Thank you so much ^.^ And yeah, it IS really easy after all :)

Cassiebsg

Just wish to thank you for this module!
Took me a while to figure out how to set it up and make it do all I wanted, but my "skills" finally caught up and it's now working perfectly. (nod)

I did had to change Load and Save to IniLoad and IniSave, as AGS was complaining that Load and Save were already assigned (am using 3.4.0.6 Beta).
There are those who believe that life here began out there...

Wyz

Thanks for your feedback, I appreciate it! :)

Hmmm the naming clash should not occur; I saw someone else with a similar problem so I'm going to look into it. :)
Life is like an adventure without the pixel hunts.

Crimson Wizard

This resembles the long-existing problem in AGS: if you have a function in global namespace, you cannot have struct function of same name.
http://www.adventuregamestudio.co.uk/forums/index.php?issue=361.0

An interesting thing is, though, that this error seemed to have been fixed in 3.4.0.6. At least some script examples of similar nature work.
We might need to investigate this further.

Is it possible to see your project, Cassiebsg?

Gurok

Regarding Cassiebsg's error, you'll still get this in 3.4.0.6 if you define a GUI called Load. I am not sure, but I suspect that this might be Cassiebsg's situation.

My commit addressed conflicting global function names, but it seems GUIs (and perhaps other editor-defined objects) cause similar namespace issues. I would be happy to look into this, pending CW seeing the project or Cassiebsg confirming that's the problem.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Cassiebsg

I was going to say Gurok was right, but no. My GUIs are called gSaveGUI and gLoadGUI after checking... and I'm using 3.4.0.6.
However, if I type "sav" to get the tooltip to show up, I can see a Save with a blue box on the list. Same with load...

I'll zip a copy of my project and send you the url CW.
There are those who believe that life here began out there...


Cassiebsg

LOL, I do? Ack...
that's happens when you make a GUI before you realize that you should name your buttons "btn..."... (roll) I thought I had changed all the buttons... guess not. (roll)
sorry. :-[
There are those who believe that life here began out there...

CrashPL

Quote404 Not Found

:( Can I ask for a reupload?

Btw does this module works with Windows only, or can I use it on Linux/Mac as well?


Crimson Wizard

Quote from: CrashPL on Thu 23/06/2016 12:45:23
Btw does this module works with Windows only, or can I use it on Linux/Mac as well?

I believe it should work anywhere, because
a) Module is a script, and every all AGS script should work regardless of where you run it (assuming the port is done correctly).
b) INI file is not system specific, it is just format of text file, like JSON or XML.

Crimson Wizard

Sorry for necroposting, but I just decided to give this topic a bump. This module just saved me a ton of time. Needed to implement a text data file for my game, and it took 15 minutes to do with this module :).

Wyz

Thanks! You're welcome CW! :-D

I've fixed the link in the first post btw. Still a ton of broken links out there I guess. :D
Life is like an adventure without the pixel hunts.

SMF spam blocked by CleanTalk