Write value in config.ini

  • #1, by wimfTuesday, 14. April 2015, 14:01 9 years ago
    Hello. I have a problem for my game...

    I created a value "Video intro" and set to 0, then the launch of my game, I run the intro video with an "if introvideo=0" and sets the value to 1.
    It allows the player to having that once the intro video.

    But I do not put this value in the config.ini has for each launch of the game, the value is read...

    Could you help me ?

    Thank in advance.

    Forum Fan

    238 Posts


  • #2, by afrlmeTuesday, 14. April 2015, 14:17 9 years ago
    Check out the config.ini script I wrote in the wiki.

    Accessing / writing to the config.ini isn't exactly that easy to do, especially if you aren't comfortable with scripting.

    P.S: technically you don't have to read / write to th e config.ini, you can create / use a different file instead with txt, ini or lua as the extension.

    Imperator

    7278 Posts

  • #3, by wimfTuesday, 14. April 2015, 14:30 9 years ago
    Hi. Thank you for your help.

    The project file .ved is not present on website (link not found ?)

    you have this file ?

    thank you

    Forum Fan

    238 Posts

  • #4, by afrlmeTuesday, 14. April 2015, 15:01 9 years ago
    There is no .ved (currently). I haven't gotten round to sorting out a .ved example of it in action.

    local fn = "config.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
     for lines in io.lines(localAppDir .. fn) do
      line = string.lower(lines) -- convert all line content to lowercase
      if not line:find("#") then -- skip all lines containing "#"
       -- * intro video * --
       if line == "introvideo = no" then Values["introvideo"].Int = 1 end
       if line == "introvideo = yes" then Values["introvideo"].Int = 0 end
      end
     end
     fr:close()
    end
    

    I can't guarantee this will work. This is only the reading part. The writing part requires you to write the entire config.ini. It might be simpler for you to create a txt file for this particular setting, that only contains a single line, as it's much easier to read & write.

    Imperator

    7278 Posts

  • #5, by wimfTuesday, 14. April 2015, 17:35 9 years ago
    Thank..

    Sorry but i have a problem to store the value video_intro into the write_ini

    a part of the modified script for the write is :

    local fn = "config.ini"
    local videointro = getObject("Values[video_intro]")

    function write_ini()

    fw:write("#\n")
    fw:write("Videointro = " .. video_intro .. "\n")


    but not work...

    I do not know enough about the values of backup script

    Forum Fan

    238 Posts

  • #6, by wimfWednesday, 15. April 2015, 12:17 9 years ago
    Hello. I have tested this :

    - In my first scene, i have a initial values "Video_intro" at "0"

    - in my menu scene, when i left clic on "quit" button, the script write_ini() is run


    the part of the script regarding the backup of the variable in config.ini is:



    local videointro = getObject("Values[video_intro]") -- intro video


    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")
    fw:write("#\n")
    fw:write("# Status Video d'intro\n")
    fw:write("videointro = " .. videointro:getStr(VValueString) .. "\n")
    fw:close()


    and after this script, my config.ini contains :

    File = Data.vis
    #
    # Status Video d'intro
    videointro =


    I do not have the value of "videointro"....

    Have you a idea where is my error ?

    thank you very much

    Forum Fan

    238 Posts

  • #7, by afrlmeWednesday, 15. April 2015, 13:53 9 years ago
    Because you are trying to write a string when it's an integer value. Values contain 2 fields. Integer, which is a number & string which is pure "text".
    fw:write("videointro = " .. videointro:getInt(VValueInt) .. "\n")
    


    P.S: could you please wrap code in code tags in the future. You can either write the code tags or you can click on the icon on the text formatting toolbar. It's the one with the little blue <> arrows.

    Imperator

    7278 Posts

  • #8, by wimfWednesday, 15. April 2015, 15:12 9 years ago
    Hello. Super. It's working smile

    Ok for the code tags, sorry.

    So, i have a videointro= no in my config.ini

    but i have more one problem..

    i have use your script :

    cal fn = "config.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
     for lines in io.lines(localAppDir .. fn) do
      line = string.lower(lines) -- convert all line content to lowercase
      if not line:find("#") then -- skip all lines containing "#"
       -- * intro video * --
       if line == "videointro = no" then Values["introvideo"].Int = 1 end
       if line == "videointro = yes" then Values["introvideo"].Int = 0 end
      end
     end
     fr:close()
    end
    


    but my value introvideo is always = 0, although in the config.ini it is "no"

    Thank you

    Forum Fan

    238 Posts

  • #9, by afrlmeWednesday, 15. April 2015, 15:53 9 years ago
    Change it to whatever you need it to be. wink
    if line == "videointro = 0" then Values["video_intro"].Int = 0 end
    if line == "videointro = 1" then Values["video_intro"].Int = 1 end
    

    Imperator

    7278 Posts

  • #10, by wimfWednesday, 15. April 2015, 16:09 9 years ago
    It's working. i have understand

    You are the best, thank you smile

    Forum Fan

    238 Posts