Submitted:|| 23rd July, 2002
People seem to be unable to use INI, unsure what it does, and have a prejudice that it's hard. Really, it's quite easy, and I'll try to show you how easy it really is.
INIs are external files from your game, so they stay there and retain their values and data even if you turn off your computer. They're useful for getting data between seperate applications and over frames.
So, what does an INI look like? If you use INIs it is always useful to look at the result file, and keep an eye on it during game/application progress for debugging. Lets take a normal game save file INI:
status=I need to get the package for Mr. brown
In the square brackets [like this], is the text we call a Group. An item is to the left of the equals sig, and a value or string is to the right (item=value). In this INI, we have saved:
- Lives: How many lives left
- Health: How much health left
- Status: If we are in an RPG or adventure type game, this could be a message for the current task.
- Level number
- Which save point in that level the user saved.
You may have noticed in an INI strings do not need to have these around them. You just have to use the right function to get a string or get a value.
Getting data from an INI
In the above example file, we want to retrieve the Health saved. We want to put it in a counter.
Simple, really. Notice the 'Health' item is under the group 'Stats'. So, under the INI object in the event editor, we do the action:
Set current group to stats
We're just telling the INI we are looking at the group [stats]. Note: DON'T put in the square brackets yourself. INI Takes care of that.
OK, so we are talking about the right group. Let's get the health item.
Set current item to health
Yep, simple... just say which group and item you want. Then...
Set counter to Get Value of INI
Easy enough. You just say which group and which item you are talking about, and get that value. However it can be very cumbersome setting all these groups and items if your dealing with a lot of values. This is where MMF's great INI object comes in handy.
MMF's INI object is much more advanced than even the INI Plus object. Take a look at this:
Set counter to Groupitemvalue(INI,stats,health)
MMF does it in one. You set the counter to the value stored in item health in group stats. Cool!
Again, to get the level number, for example, we would use Jump to Frame Groupitemvalue(INI,level,level). Or, you would have to use 'Set group to level' then 'Set item to level' THEN set the counter.
Writing to an INI
Writing, or saving, to an INI, is much the same as reading. To save how much health your charactor has:
Set current group to stats
Set current item to health
Set value to Value(Health counter)
Bingo, you saved your health to INI.
Alternatively in MMF:
Set value Value(Health counter) to item health in group stats
Got the hang of it now? Simpler than you thought? To read, say which group and item you are talking about, then get that value. To write, say which group and item you are talking about, then set that value or string.
Advanced INI techniques
Say the user has a list of URLs to their favourite sites. They have a combo box displaying them, and an edit box next to a button saying 'Add'. How shall we save them all, and load them instantly?
Start of frame: Set group to urls
We always want to be talking about group urls (keep it lowercase all the time for simplicity) so we start off in our group called urls.
Now, we will start by using the button 'Add'. We will assume you are using MMF (if not, you'll have to set the group and item manually at each stage, or try INI plus).
Button 'Add' clicked:
Set string Edittext$(Edit) to item url + Str$(Itemvalue(INI,number)
Set value (Itemvalue(INI,number) + 1) in item number
Here, we have an item called number. This says how many URLs we have saved. Then, we have items url0 url1 url2 following storing each seperate URL saved.
The INI will look like this:
and have the user typed URLs saved there.
How to load them instantly? Fastloop.
We want to load them on start of frame. So:
Start of frame: Start loop slot 0 for Groupitemvalue(INI,urls,number) times
We start the loop for however many items we have saved. This loads them all instantly. If you are unsure about loops or looping read my other article on looping.
Loop Trigger #0: Add line to Combo box: Groupitemstring(INI,urls,url + Str$(Loop Step(Fastloop object, 0)))
Run the game and you will have all items instantly in the combo box.
What this does is run through each of the url0, url1, url2 etc. adding them to the combo box. Simple, but effective and useful!
Worried people can open your file and directly edit stats? DON'T try to hide it in C:\Windows\windows_system_file.ini. That only annoys people and with a quick search on files created in the last day you can find it anyway. The best way is to encrypt it. Either use String Parser 1 or the File Encryption object to encrypt the file. Then, to access it, on Start of frame do some actions in this order:
Start of frame
Decrypt the file
Set all your counters and string objects from the INI
Re-encrypt the file
That way, the file is decrypted for such a short time nobody would ever be able to access it when it was decrypted. If they edited it when it was encrypted, it would garble some parts of the INI, making it unreadable and teaching them a lesson. If they somehow saved an unencrypted version over it, decrypting something normal effectively encrypts it again so the INI is totally unreadable.
Remember, very sensative material like passwords and security info should NOT be stored in an INI, however encrypted. In fact, it shouldn't be stored at all - it is very hard to make a 100% secure system for protecting files.
INI's aren't that hard. So don't complain they're really quite easy. INIs are very powerful and definitely worth learning as they are invaluable especially in applications.
Don't forget to can keep an INI in the same directory as your application or game by leaving the INI filename blank, and use:
Start of frame: Set current file to: appdrive$ + appdir$ + myfile.ini
Combined with Fastloop INIs can provide amazing saving and loading capabilities and cool new possibilities for games and apps. On www.tigerworks.co.uk , you can event download an app I made which makes Text Adventures, saves them, and can be played by anyone with the TextVenture file reader. All done by INI! Incredible! You can even comvert your text adventures to ASP format and upload them to your webserver, so ANYONE can play them, online.
Good luck, and happy INIing!