What's wrong with my config.ini code?

  • #1, by UraalWednesday, 08. April 2015, 14:16 7 years ago
    Heyas,

    I tried reverse engineering the awesome script ARLFMe wrote here: http://wiki.visionaire-tracker.net/wiki/Read/Write_Config.ini_(CMS)#Main_Script

    And while I tried to be very specific and do the exact steps (used my own variable names for learning) I still get errors in log:

    https://dl.dropboxusercontent.com/u/20899096/errors.png
    https://dl.dropboxusercontent.com/u/20899096/error2.png

    Weirdest part is: there is no line 106 in the code like error log is claiming, so I don't know where to look.

    I am using Visionaire Studio 4.1 (Build 1177) -> Powered by wxWidgets 3.1.0, Lua 5.1

    --[[
    Read/Write Config.ini [v2] (02/03/2014)
    Written by AFRLme [Lee Clarke]
    -- + --
    alternatingfrequencies@hotmail.com | skype @ AFRLme
    -- + --
    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.
    --]]
     
    
    
    -- DEFAULT VALUES AS VARIABLES --
    local log_level = "Error" -- Writes errors to log-file, other options: Warning & Info
    
    
    -- FILE NAME VARIABLES -- 
    local filename_of_config_file = "config.ini"
    local vis_file_name = "Bunker.vis"
    
    
    -- OPTION VARIABLES  -- 
    local option_is_fullscreen = getObject("Conditions[Fullscreen?]")
    local option_sound_volume = getObject("Values[Sfx_volume]")
    local option_music_volume = getObject("Values[Music_volume]")
    local option_language = game:getLinks(VGameLanguages) -- Puts languages in a table
    
    
    -- FUNCTION FOR READING INI-FILE --
    function read_ini()
    
    local config_file_exist = io.open(localAppDir .. filename_of_config_file, "r") -- true if file was found
    
    if config_file_exist then
    
    -- Prints --
    print(filename_of_config_file .. " was found!")
    print(filename_of_config_file .. " is now loaded")
    
    -- Read every line on config.ini and change corresponding variable in game to match --
    for lines in io.lines(localAppDir .. filename_of_config_file) do
    
    line = string.lower(lines) -- make all lines have only lowercase characters, eliminating problems that might arise with casesensitivity
    if not line:find("#") then --incase there is no #-symbol on this line for-sentence may proceed to the next part --
    
    -- Next lines change each variable found from game to match with what is written in config.ini
    
    -- Fullscreen?--
    if line == "fullscreen = no" then option_is_fullscreen:setValue(VConditionValue, false);
    if line == "fullscreen = yes" then option_is_fullscreen:setValue(VConditionValue, true);
    
    -- Define language and change it -- 
    for i = 1, table.maxn(option_language) do if line == ("textlanguage = " .. string.lower( option_language[i]:getName() )) then game:setValue(VGameStandardLanguage, option_language[i]); print("Language is set to " .. game:getLink(VGameStandardLanguage):getName()) end end
    
    -- Read & print sound volumes --
    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("globalvolume =") then print("global volume = " .. getVolume(eGlobalVolume)) end
    
    end
    end
    
    config_file_exist:close()
    print(filename_of_config_file .. "'s settings were succesfully retrieved")
    
    else
    
    -- Prints --
    print("There is no " .. filename_of_config_file .. " at game's root folder!")
    print("To fix this, please make an empty text file with name: " .. filename_of_config_file .. " and place it to game's root folder.")
    
    end
    
    end
    
    
    -- FUNCTION FOR WRITING INI-FILE --
    
    function write_ini()
    
    local file_to_write= io.open(localAppDir .. filename_of_config_file, "w") --OPEN--
    
    -- *************************Stuff you write to the ini*********************** --
    --file_to_write:write("" .. .. "\n")  --
    
    file_to_write:write("File = " .. vis_file_name .. "\n")
    
    file_to_write:write("Fullscreen = ")
    if option_is_fullscreen:getBool(VConditionValue) then file_to_write:write("Yes\n") else file_to_write:write("No\n") end 
    
    file_to_write:write("TextLanguage = " .. game:getLink(VGameStandardLanguage):getName() .. "\n")
    
    file_to_write:write("LogLevel = " .. log_level .. "\n")
    
    file_to_write:write("MusicVolume = " .. getVolume(eMusicVolume) .. "\n")
    file_to_write:write("SoundVolume = " .. getVolume(eSoundVolume) .. "\n")   
    
    -- ************************************************************************** --
    
    print("Settings succesfully written to " .. filename_of_config_file)
    file_to_write:close() --After you are done: CLOSE--
    
    end
    

    Newbie

    93 Posts


  • #2, by afrlmeWednesday, 08. April 2015, 15:26 7 years ago
    Aye simple enough...

    It's Lua script, not java or python or whatever. Semi-colons; are not the same as end. The only reason I use them (rarely) is for defining split sections when I code multiple things on the same line...
    print(x + y); print(a + b); if x + y == a + b then print("true") end
    


    Here... (obviously I can't test if it's working or not).
    --[[
    Read/Write Config.ini [v2] (02/03/2014)
    Written by AFRLme [Lee Clarke]
    -- + --
    alternatingfrequencies@hotmail.com | skype @ AFRLme
    -- + --
    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.
    --]]
    
    -- DEFAULT VALUES AS VARIABLES --
    local log_level = "Error" -- Writes errors to log-file, other options: Warning & Info
    
    -- FILE NAME VARIABLES -- 
    local filename_of_config_file = "config.ini"
    local vis_file_name = "Bunker.vis"
    
    -- OPTION VARIABLES  -- 
    local option_is_fullscreen = getObject("Conditions[Fullscreen?]")
    local option_sound_volume = getObject("Values[Sfx_volume]")
    local option_music_volume = getObject("Values[Music_volume]")
    local option_language = game:getLinks(VGameLanguages) -- Puts languages in a table
    
    -- FUNCTION FOR READING INI-FILE --
    function read_ini()
    
    local config_file_exist = io.open(localAppDir .. filename_of_config_file, "r") -- true if file was found
    
    if config_file_exist then
    
    -- Prints --
    print(filename_of_config_file .. " was found!")
    print(filename_of_config_file .. " is now loaded")
    
    -- Read every line on config.ini and change corresponding variable in game to match --
    for lines in io.lines(localAppDir .. filename_of_config_file) do
    
    line = string.lower(lines) -- make all lines have only lowercase characters, eliminating problems that might arise with casesensitivity
    if not line:find("#") then --incase there is no #-symbol on this line for-sentence may proceed to the next part --
    
    -- Next lines change each variable found from game to match with what is written in config.ini
    
    -- Fullscreen?--
    if line == "fullscreen = no" then option_is_fullscreen:setValue(VConditionValue, false) end
    if line == "fullscreen = yes" then option_is_fullscreen:setValue(VConditionValue, true) end
    
    -- Define language and change it -- 
    for i = 1, table.maxn(option_language) do if line == ("language = " .. string.lower( option_language[i]:getName() )) then game:setValue(VGameStandardLanguage, option_language[i]); print("Language is set to " .. game:getLink(VGameStandardLanguage):getName()) end end
    
    -- Read & print sound volumes --
    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("globalvolume =") then print("global volume = " .. getVolume(eGlobalVolume)) end
    
    end
    end
    
    config_file_exist:close()
    print(filename_of_config_file .. "'s settings were succesfully retrieved")
    
    else
    
    -- Prints --
    print("There is no " .. filename_of_config_file .. " at game's root folder!")
    print("To fix this, please make an empty text file with name: " .. filename_of_config_file .. " and place it to game's root folder.")
    
    end
    
    end
    
    -- FUNCTION FOR WRITING INI-FILE --
    
    function write_ini()
    
    local file_to_write = io.open(localAppDir .. filename_of_config_file, "w") --OPEN--
    
    -- *************************Stuff you write to the ini*********************** --
    --file_to_write:write("" .. .. "\n")  --
    
    file_to_write:write("File = " .. vis_file_name .. "\n")
    
    file_to_write:write("Fullscreen = ")
    if option_is_fullscreen:getBool(VConditionValue) then file_to_write:write("Yes\n") else file_to_write:write("No\n") end 
    
    file_to_write:write("Language = " .. game:getLink(VGameStandardLanguage):getName() .. "\n")
    
    file_to_write:write("LogLevel = " .. log_level .. "\n")
    
    --file_to_write:write("MusicVolume = " .. getVolume(eMusicVolume) .. "\n")
    --file_to_write:write("SoundVolume = " .. getVolume(eSoundVolume) .. "\n")   
    
    -- ************************************************************************** --
    
    print("Settings succesfully written to " .. filename_of_config_file)
    file_to_write:close() --After you are done: CLOSE--
    
    end
    

    ... You should use smaller variable names & less whitespace - nesting is also a good idea, as it makes it easier to determine what is what. Also I don't recommend having ? marks in the object names. & final note: you don't need to add the volume stuff as it automatically gets written to the config.ini each time it's updated - don't ask me why as I don't know.

    Quick tip: write your scripts inside of a text editor like sublime text or atom. As it makes life much easier. Both of them have multi-line editing. Multi-line / word search & replace. Auto-completion, theme & color syntax. I recommend sublime text though as it's still better than atom (at the minute) as it remembers all tabs & their content when you close the app regardless of whether or not you saved.

    Imperator

    7043 Posts

  • #3, by UraalWednesday, 08. April 2015, 17:44 7 years ago
    Three times hurrah again smile, script is working now. Yeah I do use Sublime text, but I had no idea of the finer features (multiline editing) before this!

    Hmm now I keep wondering where does it save this ini-file? For some reason fullscreen on and off works but this doesn't appear in config.ini file at game's root directory.

    Newbie

    93 Posts

  • #4, by afrlmeWednesday, 08. April 2015, 17:49 7 years ago
    It won't. Since 4.x it goes into a folder somewhere in the local folder of appdata.

    c:/users/username/appdata/local/game name or company name/

    if no game or company name was set in the game tab then it will end up in same place but in a folder inside of the visioniare studio folder.

    c:/users/username/appdata/local/visionairestudio/ved name/ (usually).

    P.S: sublime is awesome. Atom isn't too far behind, but I rely heavily on the auto-tab/content remember system of sublime, to bother switching over to Atom. I would make the switch if it had that feature (well a built in version) as Atom is an open source app made by github.

    Imperator

    7043 Posts

  • #5, by UraalWednesday, 08. April 2015, 18:55 7 years ago
    Awesome, found the file. Something suspected, but had no idea where to start looking. Thanks again! Next up, I'll figure that volume slider stuff smile

    Newbie

    93 Posts