Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Zeriab on June 10, 2009, 05:36:52 am

Title: Read from and write to .ini snippet
Post by: Zeriab on June 10, 2009, 05:36:52 am
Here is a small Utility module which provides method for reading from and writing to .ini files.

module Utility
  #############
  # DLL STUFF #
  #############
  READ_INI         = Win32API.new('kernel32',  'GetPrivateProfileStringA',
                                  %w(p p p p l p), 'l')
  WRITE_INI        = Win32API.new('kernel32',  'WritePrivateProfileStringA',
                                  %w(p p p p), 'l')
  ##
  # Read from system ini
  #
  def self.read_ini(key_name, app_name = 'Game', filename = 'Game.ini',
                    buffer_size = 256, default = '')
    buffer = "\0" * buffer_size
    READ_INI.call(app_name, key_name, default, buffer, buffer_size - 1,
                  ".\\" + filename)
    return buffer.delete("\0")
  end
 
  ##
  # Write to system ini
  #
  def self.write_ini(key_name, value, app_name = 'Game', filename = 'Game.ini')
    return WRITE_INI.call(app_name, key_name, value.to_s, ".\\" + filename)
  end
end


This can for example be used to store configuration information in Game.ini instead of storing it in a separate file. Configuration data the player should be able to change that is.
You can of course also mess around with the already existing data in Game.ini.

If you for example want to read the title of the game you can do that with:
Utility.read_ini('Title')


If you want to save that Windowskin number 3 is used you can do that with:
Utility.write_ini('Windowskin', '3')


Now you may want that setting to be save specific and you should not write save specific details to the .ini file. You can of course, but you really should think it through before doing so.
A global setting is whether to start the game up in fullscreen or not which for example could be done with:
Utility.write_ini('Fullscreen', '1', 'Window')


Notice the new argument I have put there.
Try and see what the effects are in the ini file.
There are also other arguments which I have not covered which you can experiment with.
Just note that the values are strings

*hugs*
- Zeriab
Title: Re: Read from and write to .ini snippet
Post by: Blizzard on June 10, 2009, 05:46:21 am
I was just going to say "Why don't you use the API calls?", but then I saw your code. xD *levels up* <3
Title: Re: Read from and write to .ini snippet
Post by: Zeriab on June 10, 2009, 05:55:56 am
XD. All I've done is just to wrap the API calls in :P
<3
Title: Re: Read from and write to .ini snippet
Post by: fugibo on June 10, 2009, 10:47:59 am
Methinks you should rearrange the arguments, it's too much hassle to specify buffer size etc. when all you want to do is save to a different file :/
Title: Re: Read from and write to .ini snippet
Post by: Zeriab on June 10, 2009, 10:53:06 am
Good point. I have changed it ^_^
Title: Re: Read from and write to .ini snippet
Post by: fugibo on June 10, 2009, 10:56:51 am
*levels up nowz*

This is definitely one of the highest-potential scripts for RMXP, solely because it allows for the user to have complete control over any configuration that supports it. The fact that it can be used for global configuration (as opposed to per-save configuration) is also very useful.

*hearts*
Title: Re: Read from and write to .ini snippet
Post by: Zeriab on June 10, 2009, 11:57:25 am
Thanks for your comments WcW :3
I don't know what I was thinking with putting the filename after the buffer size >_<

Note that you can change the title of the game if you want. (requires restart)
I agree that the biggest strength is that users can open the ini-file and modify it themselves.

*hugs*