Difference between revisions of "Read/Write Config.ini (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{| class="wikitable" style="width:100%"
+
{| class="ts" style="width:100%"
 
|-
 
|-
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By
 
|-
 
|-
| Read/Write Config.ini || Definition || AFRLme
+
| Read/Write Config.ini || Definition || [https://www.patreon.com/AFRLme AFRLme]
 
|}
 
|}
  
This script allows you to read from/write to the config.ini file. This script is required for anyone wanting to create a professional game menu which stores & remembers each players option configuration. By default Visionaire Studio would load the default settings on game launch.  
+
This script allows you to read from/write to the config.ini file. This script is required for anyone wanting to create a professional game menu which stores & remembers each players option configuration. By default Visionaire Studio would load the default settings on game launch.
 +
 
 +
{| class="ts"
 +
|-
 +
| ''Please note that, not all of the options included in the config.ini script below, are official parameters. Some of them are custom options, that have just been added as an example. Custom options require you to determine what they do inside of your game, as they do not automatically affect anything. For a list of default parameters, please see [[Ini_File|here]].''
 +
|}
  
 
== Instructions ==
 
== Instructions ==
It is a little bit hard to create step by step instructions for this script. My suggestion is to: read over the script & comments I have added & then try & edit it to suit your needs. If you have any difficulties, then please feel free to contact me for help.
+
This script is a little too flexible & complicated for me to create a proper ''step by step'' guide. I would instead recommend looking over the script, & checking out the demo I have linked in the [[#Resources|resources]] section below.
  
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
Line 15: Line 20:
 
3. Create conditions & values (in vs editor) - they should reflect the conditions & values listed in the script.<br/>
 
3. Create conditions & values (in vs editor) - they should reflect the conditions & values listed in the script.<br/>
 
4. Assign default values (integer & string for values, boolean for conditions).<br/>
 
4. Assign default values (integer & string for values, boolean for conditions).<br/>
5. create a condition that will be used to determine if settings have been changed. This condition should be set to true each time an option is changed. Name it: '''cfg_update'''.<br/>
+
5. Create a condition that will be used to determine if settings have been changed. This condition should be set to <span class="green">''true''</span> each time an option is changed. Name it: '''cfg_update'''.<br/>
6. To read from the config.ini: add an '''execute a script''' action to the game launch action, or the an '''at begin of scene''' action for the menu scene, containing...
+
6. Don't forget to set the correct condition/value for edited settings; they should reflect the current settings value.<br/>
<syntaxhighlight>
+
7. To read from the config.ini: add an '''execute a script''' action to the game launch action, or inside of a '''at begin of scene''' action for the menu scene, containing...
 +
<syntaxhighlight lang="lua">
 
read_ini()
 
read_ini()
 
</syntaxhighlight>
 
</syntaxhighlight>
7. To write new settings to the config.ini: add an '''execute a script''' action to an '''at end of scene''' action for the menu scene, containing...
+
8. To write new settings to the config.ini: add an '''execute a script''' action to an '''at end of scene''' action - or left click action of a scene object - for the menu scene, containing...
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
 
if getObject("Conditions[cfg_update]"):getBool(VConditionValue) then
 
if getObject("Conditions[cfg_update]"):getBool(VConditionValue) then
 
  write_ini()
 
  write_ini()
Line 28: Line 34:
  
 
== Main Script ==
 
== Main Script ==
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
 
--[[
 
--[[
Read from config.ini [v2] (28/02/2014)
+
Read/Write Config.ini [v2.1] (12/01/2019)
 
Written by AFRLme [Lee Clarke]
 
Written by AFRLme [Lee Clarke]
 
-- + --
 
-- + --
alternatingfrequencies@hotmail.com | skype @ AFRLme
+
afrlme@outlook.com | https://de.tail.studio
 
-- + --
 
-- + --
 
This script is donation optional. In game credit is non-negotiable.
 
This script is donation optional. In game credit is non-negotiable.
Line 51: Line 57:
 
-- * fallback * --
 
-- * fallback * --
 
local lglvl = "Error" -- default value for log level
 
local lglvl = "Error" -- default value for log level
local df = "File = Data.vis" -- filename should reflect exported .vis file
+
local df = "Data.vis" -- filename should reflect exported .vis file
 
game:setValue(VGameSpeechLanguage, game:getLink(VGameStandardLanguage)) -- default speech language to stndard language
 
game:setValue(VGameSpeechLanguage, game:getLink(VGameStandardLanguage)) -- default speech language to stndard language
  
 
-- * tables * --
 
-- * tables * --
local t_res = {"Auto","desktop","640x480","800x600","1024x768","1280x720","1280x1024","1680x1050","1920x1080"} -- add available resolutions here
+
local t_res = {"Auto","Desktop","640x480","800x600","1024x768","1280x720","1280x1024","1680x1050","1920x1080"} -- add available resolutions here
 
local t_lang = game:getLinks(VGameLanguages) -- store all available languages into a table
 
local t_lang = game:getLinks(VGameLanguages) -- store all available languages into a table
  
Line 63: Line 69:
 
  -- * --
 
  -- * --
 
  if fr then -- if file exists then...
 
  if fr then -- if file exists then...
   line = fr:read() -- read currently selected line
+
   lines = fr:read() -- read currently selected line
 
   print("-- * --")
 
   print("-- * --")
 
   print(fn .. " exists")
 
   print(fn .. " exists")
 
   print("retrieving settings from " .. fn)
 
   print("retrieving settings from " .. fn)
   for lines in io.lines(fn) do
+
   for lines in io.lines(localAppDir .. fn) do
 
   line = string.lower(lines) -- convert all line content to lowercase
 
   line = string.lower(lines) -- convert all line content to lowercase
 +
  line = string.gsub(line, '\r', '') -- fallback for mac/linux
 
   if not line:find("#") then -- skip all lines containing "#"
 
   if not line:find("#") then -- skip all lines containing "#"
     if lines:find("file =") then df = line end
+
     if line:find("file =") then df = string.sub(lines, 8); print("file is currently linked to " .. df) end
 
     -- * window mode * --
 
     -- * window mode * --
 
     if line == "fullscreen = no" then wm:setValue(VConditionValue, false); print("window mode is currently set to Windowed") end
 
     if line == "fullscreen = no" then wm:setValue(VConditionValue, false); print("window mode is currently set to Windowed") end
 
     if line == "fullscreen = yes" then wm:setValue(VConditionValue, true); print("window mode is currently set to Fullscreen") end
 
     if line == "fullscreen = yes" then wm:setValue(VConditionValue, true); print("window mode is currently set to Fullscreen") end
 
     -- * resolution * --
 
     -- * resolution * --
     for i = 1, table.maxn(t_res) do if line == ("resolution = " .. string.lower( t_res[i] )) then res:setValue(VValueString, t_res[i]); res:setValue(VValueInt, i); print("resolution is currently set to " .. res:getStr(VValueString)) end end
+
     for i = 1, #t_res do if line == ("resolution = " .. string.lower( t_res[i] )) then res:setValue(VValueString, t_res[i]); res:setValue(VValueInt, i); print("resolution is currently set to " .. res:getStr(VValueString)) end end
 
     -- * subtitles * --
 
     -- * subtitles * --
     if line == "subtitles = no" then subs:setValue(VConditionValue, false); print("subtitles are currently set to off") end
+
     if line == "subtitles = no" then subs:setValue(VConditionValue, false); print("subtitles are currently set to Off") end
     if line == "subtitles = yes" then subs:setValue(VConditionValue, true); print("subtitles are currently set to on") end
+
     if line == "subtitles = yes" then subs:setValue(VConditionValue, true); print("subtitles are currently set to On") end
 
     -- * text language * --
 
     -- * text language * --
     for i = 1, table.maxn(t_lang) do if line == ("textlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameStandardLanguage, t_lang[i]); print("text language is currently set to " .. game:getLink(VGameStandardLanguage):getName()) end end
+
     for i = 1, #t_lang do if line == ("textlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameStandardLanguage, t_lang[i]); print("text language is currently set to " .. game:getLink(VGameStandardLanguage):getName()) end end
 
     -- * speech language * --
 
     -- * speech language * --
     for i = 1, table.maxn(t_lang) do if line == ("speechlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameSpeechLanguage, t_lang[i]); print("spoken language is currently set to " .. game:getLink(VGameSpeechLanguage):getName()) end end
+
     for i = 1, #t_lang do if line == ("speechlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameSpeechLanguage, t_lang[i]); print("spoken language is currently set to " .. game:getLink(VGameSpeechLanguage):getName()) end end
 
     -- * fx quality * --
 
     -- * fx quality * --
 
     if line == "fxquality = low" then fx:setValue(VValueString, "Low"); fx:setValue(VValueInt, 1); print("fx quality is currently set to " .. fx:getStr(VValueString)) end
 
     if line == "fxquality = low" then fx:setValue(VValueString, "Low"); fx:setValue(VValueInt, 1); print("fx quality is currently set to " .. fx:getStr(VValueString)) end
Line 103: Line 110:
 
   end
 
   end
 
   fr:close()
 
   fr:close()
   print('successfully retrieved settings from ' .. fn .. "\n")
+
   print("successfully retrieved settings from " .. fn)
-- * end if else * --
 
 
  else
 
  else
   print(fn  .. ' does not exist\n')
+
   print(fn  .. " does not exist")
 
  end
 
  end
 
end
 
end
Line 112: Line 118:
 
-- * function used to write data to the config.ini file * --
 
-- * function used to write data to the config.ini file * --
 
function write_ini()
 
function write_ini()
  local fw = io.open(localAppDir .. "config.ini", "w") -- write to config.ini
+
  local fw = io.open(localAppDir .. fn, "w") -- write to config.ini
 
  print("-- * --")
 
  print("-- * --")
 
  print("writing new settings to " .. fn)
 
  print("writing new settings to " .. fn)
 
  -- * data file * --
 
  -- * data file * --
  fw:write(df .. "\n")
+
  fw:write("File = " .. df .. "\n")
 
  -- * window mode * --
 
  -- * window mode * --
 
  fw:write("#\n")
 
  fw:write("#\n")
Line 180: Line 186:
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
{{i18n|Read/Write_Config.ini_(CMS)}}
+
 
 +
== Resources ==
 +
The project file .ved & resource files will be linked here.{{toc}}

Revision as of 13:05, 5 December 2020

Name Type By
Read/Write Config.ini Definition AFRLme

This script allows you to read from/write to the config.ini file. This script is required for anyone wanting to create a professional game menu which stores & remembers each players option configuration. By default Visionaire Studio would load the default settings on game launch.

Please note that, not all of the options included in the config.ini script below, are official parameters. Some of them are custom options, that have just been added as an example. Custom options require you to determine what they do inside of your game, as they do not automatically affect anything. For a list of default parameters, please see here.

Instructions

This script is a little too flexible & complicated for me to create a proper step by step guide. I would instead recommend looking over the script, & checking out the demo I have linked in the resources section below.

1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2. Edit the script to suit.
3. Create conditions & values (in vs editor) - they should reflect the conditions & values listed in the script.
4. Assign default values (integer & string for values, boolean for conditions).
5. Create a condition that will be used to determine if settings have been changed. This condition should be set to true each time an option is changed. Name it: cfg_update.
6. Don't forget to set the correct condition/value for edited settings; they should reflect the current settings value.
7. To read from the config.ini: add an execute a script action to the game launch action, or inside of a at begin of scene action for the menu scene, containing...

read_ini()

8. To write new settings to the config.ini: add an execute a script action to an at end of scene action - or left click action of a scene object - for the menu scene, containing...

if getObject("Conditions[cfg_update]"):getBool(VConditionValue) then
 write_ini()
end

Main Script

--[[
Read/Write Config.ini [v2.1] (12/01/2019)
Written by AFRLme [Lee Clarke]
-- + --
afrlme@outlook.com | https://de.tail.studio
-- + --
This script is donation optional. In game credit is non-negotiable.
You are free to: ¹ use it in your game(s). ² modify the script.
Do not remove - or edit - this comment block.
--]]

-- * local variables * --
local fn = "config.ini" -- store filename
-- * --
local wm = getObject("Conditions[cfg_wm]") -- window mode [fullscreen/windowed]
local res = getObject("Values[cfg_res]") -- resolution [fullscreen mode only]
local subs = getObject("Conditions[cfg_subs]") -- subtitles [on/off]
local fx = getObject("Values[cfg_fx]") -- fx level [low/medium/high] (controls which animations will be displayed)
local cbm = getObject("Conditions[cfg_cbm]") -- color blind mode [on/off]

-- * fallback * --
local lglvl = "Error" -- default value for log level
local df = "Data.vis" -- filename should reflect exported .vis file
game:setValue(VGameSpeechLanguage, game:getLink(VGameStandardLanguage)) -- default speech language to stndard language

-- * tables * --
local t_res = {"Auto","Desktop","640x480","800x600","1024x768","1280x720","1280x1024","1680x1050","1920x1080"} -- add available resolutions here
local t_lang = game:getLinks(VGameLanguages) -- store all available languages into a table

-- * function used to read data from the config.ini file * --
function read_ini()
 local fr = io.open(localAppDir .. fn, "r") -- read from config.ini
 -- * --
 if fr then -- if file exists then...
  lines = fr:read() -- read currently selected line
  print("-- * --")
  print(fn .. " exists")
  print("retrieving settings from " .. fn)
  for lines in io.lines(localAppDir .. fn) do
   line = string.lower(lines) -- convert all line content to lowercase
   line = string.gsub(line, '\r', '') -- fallback for mac/linux
   if not line:find("#") then -- skip all lines containing "#"
    if line:find("file =") then df = string.sub(lines, 8); print("file is currently linked to " .. df) end
    -- * window mode * --
    if line == "fullscreen = no" then wm:setValue(VConditionValue, false); print("window mode is currently set to Windowed") end
    if line == "fullscreen = yes" then wm:setValue(VConditionValue, true); print("window mode is currently set to Fullscreen") end
    -- * resolution * --
    for i = 1, #t_res do if line == ("resolution = " .. string.lower( t_res[i] )) then res:setValue(VValueString, t_res[i]); res:setValue(VValueInt, i); print("resolution is currently set to " .. res:getStr(VValueString)) end end
    -- * subtitles * --
    if line == "subtitles = no" then subs:setValue(VConditionValue, false); print("subtitles are currently set to Off") end
    if line == "subtitles = yes" then subs:setValue(VConditionValue, true); print("subtitles are currently set to On") end
    -- * text language * --
    for i = 1, #t_lang do if line == ("textlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameStandardLanguage, t_lang[i]); print("text language is currently set to " .. game:getLink(VGameStandardLanguage):getName()) end end
    -- * speech language * --
    for i = 1, #t_lang do if line == ("speechlanguage = " .. string.lower( t_lang[i]:getName() )) then game:setValue(VGameSpeechLanguage, t_lang[i]); print("spoken language is currently set to " .. game:getLink(VGameSpeechLanguage):getName()) end end
    -- * fx quality * --
    if line == "fxquality = low" then fx:setValue(VValueString, "Low"); fx:setValue(VValueInt, 1); print("fx quality is currently set to " .. fx:getStr(VValueString)) end
    if line == "fxquality = medium" then fx:setValue(VValueString, "Medium"); fx:setValue(VValueInt, 2); print("fx quality is currently set to " .. fx:getStr(VValueString)) end
    if line == "fxquality = high" then fx:setValue(VValueString, "High"); fx:setValue(VValueInt, 3); print("fx quality is currently set to " .. fx:getStr(VValueString)) end
    -- * color blind mode * ---
    if line == "colorblindmode = no" then cbm:setValue(VConditionValue, false); print("color blind mode is currently set to Off") end
    if line == "colorblindmode = yes" then cbm:setValue(VConditionValue, true); print("color blind mode is currently set to On") end
    -- * log level * --
    if line == "loglevel = error" then lglvl = "Error"; print("log level is currently set to Error") end
    if line == "loglevel = warning" then lglvl = "Warning"; print("log level is currently set to Warning") end
    if line == "loglevel = info" then lglvl = "Info"; print("log level is currently set to Info") end
    -- * sound levels * --
    if line:find("musicvolume =") then print("music volume = " .. getVolume(eMusicVolume)) end
    if line:find("soundvolume =") then print("sound volume = " .. getVolume(eSoundVolume)) end
    if line:find("speechvolume =") then print("speech volume = " .. getVolume(eSpeechVolume)) end
    if line:find("movievolume =") then print("movie volume = " .. getVolume(eMovieVolume)) end
    if line:find("globalvolume =") then print("global volume = " .. getVolume(eGlobalVolume)) end
   end
  end
  fr:close()
  print("successfully retrieved settings from " .. fn)
 else
  print(fn  .. " does not exist")
 end
end

-- * function used to write data to the config.ini file * --
function write_ini()
 local fw = io.open(localAppDir .. fn, "w") -- write to config.ini
 print("-- * --")
 print("writing new settings to " .. fn)
 -- * data file * --
 fw:write("File = " .. df .. "\n")
 -- * window mode * --
 fw:write("#\n")
 fw:write("# Fullscreen = {Yes|No}\n")
 fw:write("# Yes: starts the game in fullscreen\n")
 fw:write("# No: starts the game in a window\n")
 fw:write("Fullscreen = ")
 if wm:getBool(VConditionValue) then fw:write("Yes\n") else fw:write("No\n") end
 -- * resolution * --
 fw:write("#\n")
 fw:write("# Resolution = {Auto|Desktop|Custom}\n")
 fw:write("# Auto: wide-screen support is activated if a wide-screen display is detected\n")
 fw:write("# Desktop: current desktop resolution is used when game is started in full screen mode\n")
 fw:write("# Custom: enter a custom value eg: Resolution = 1920x1080\n")
 fw:write("Resolution = " .. res:getStr(VValueString) .. "\n")
 -- * subtitles * --
 fw:write("#\n")
 fw:write("# Subtitles = {Yes|No}\n")
 fw:write("# Yes: show subtitles during the game, cut scenes & videos\n")
 fw:write("# No: do not show subtitles during the game, cut scenes or videos\n")
 fw:write("Subtitles = ")
 if subs:getBool(VConditionValue) then fw:write("Yes\n") else fw:write("No\n") end
 -- * text language * --
 fw:write("#\n")
 fw:write("# TextLanguage = {English|French|German|Spanish}\n")
 fw:write("# this will display subtitles in the specified language\n")
 fw:write("TextLanguage = " .. game:getLink(VGameStandardLanguage):getName() .. "\n")
 -- * speech language * --
 fw:write("#\n")
 fw:write("# SpeechLanguage = {English|French|German|Spanish}\n")
 fw:write("# this will play speech files linked to the specified language\n")
 fw:write("# if no speech language is provided then the speech language will default to the standard language\n")
 fw:write("SpeechLanguage = " .. game:getLink(VGameSpeechLanguage):getName() .. "\n")
 -- * fx quality * --
 fw:write("#\n")
 fw:write("# FxQuality = {Low|Medium|High}\n")
 fw:write("# allows user to change animation/particle fx modes (for players on lower end machines)\n")
 fw:write("# Low: only show the basic required game animations/particle fx\n")
 fw:write("# Medium: only show certain animations/particle fx &/or animations containing less frames\n")
 fw:write("# High: show all animations, in all their glory\n")
 fw:write("FxQuality = " .. fx:getStr(VValueString) .. "\n")
 -- * color blind mode * --
 fw:write("#\n")
 fw:write("# ColorBlindMode = {Yes|No}\n")
 fw:write("# Yes: enable color blind support (graphic replacement, on screen indicators etc)\n")
 fw:write("# No: show the game as it's meant to be played\n")
 fw:write("ColorBlindMode = ")
 if cbm:getBool(VConditionValue) then fw:write("Yes\n") else fw:write("No\n") end
 -- * log level * --
 fw:write("#\n")
 fw:write("# LogLevel = {Info|Warning|Error}\n")
 fw:write("LogLevel = " .. lglvl .. "\n")
 -- * volume settings * --
 fw:write("#\n")
 fw:write("# MusicVolume|SoundVolume|SpeechVolume|MovieVolume|GlobalVolume = int value {0-100}\n")
 fw:write("MusicVolume = " .. getVolume(eMusicVolume) .. "\n")
 fw:write("SoundVolume = " .. getVolume(eSoundVolume) .. "\n")
 fw:write("SpeechVolume = " .. getVolume(eSpeechVolume) .. "\n")
 fw:write("MovieVolume = " .. getVolume(eMovieVolume) .. "\n")
 fw:write("GlobalVolume = " .. getVolume(eGlobalVolume) .. "\n")
 print("new settings successfully written to " .. fn)
 fw:close()
end

Resources

The project file .ved & resource files will be linked here.