Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read from and write to .ini snippet
#1
Read from and write to .ini snippet
by Zeriab
June 24 2009

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.


Here is a small Utility module which provides method for reading from and writing to .ini files.

Code:
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 = "\" * buffer_size
    READ_INI.call(app_name, key_name, default, buffer, buffer_size - 1,
                  ".\\" + filename)
    return buffer.delete("\")
  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:
Code:
Utility.read_ini('Title')


If you want to save that Windowskin number 3 is used you can do that with:
Code:
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:
Code:
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
}




Users browsing this thread: