Mojimi
Mojimi

Reputation: 3161

Save variable value in a file by user input

Basically I have this code which when ran for the first time, it will open a channel for the user to input the value of the variables in the configuration file

if (firstRun) then 
    Channel.New("First Configurations")
    Channel:SendYellowMessage("Console","Running configuration sequence...\n How many potions would you like to buy?")
    maxMP = io.read()
-- some more variables later
    firstRun = false
end

The code above is in my main file("main.lua"), this below is in my configuration file ("config.lua"):

firstRun = true -- Change it to false if you want to manually configure
maxMP = nil     -- How many MP potions would you like to buy?
maxHP = nil     -- How many HP potions would you like to buy?
-- couple more variables

What I need it to do is, after running the firstRun Channel function, it would save the value of "maxMP" , "maxHP", etc in the "config.lua" file and also saving firstRun = false.

And i can't save it in a .txt file, it has to be in the "config.lua" i just don't know how

Upvotes: 1

Views: 579

Answers (1)

Kamiccolo
Kamiccolo

Reputation: 8507

Basically, manually overwriting config file sounds like a trivial task using pure Lua.

Something like this (initial write, do whatever changes to default or current config):

local function flush_config_to_file(configTable, configFile)
    local tmpFileHandle
    local configFileLines = {}

    --start file with array declaration
    table.insert(configFileLines, "local config = {")

    --iterate default configs and put string analogical lines into table:
    for key, value in pairs(defaultConfig) do
        table.insert(configFileLines, "\t[\"key\"] = " .. tostring(value) .. ",")
    end

    --closing array:
    table.insert(configFileLines, "}")

    --returning table of config values:
    table.insert("return config")

    tmpFileHandle = io.open(configFile, "w")

    --concat lines with \n separator and write those to file:
    tmpFileHandle:write(table.concat(configFileLines, "\n"))
    tmpFileHandle:close()
end

local function create_initial_config_file(configFile)
    local defaultConfig = 
    {
        "first_run" = true,
        "maxMP" = nil,
        "maxHP" = nil

        -- etc etc etc
    }

    flush_config_to_file(defaultConfig, configFile)
end

local function get_config_from_module(configModule)
    local tmpConfigTable

    package.loaded[configModule] = nil
    tmpConfigTable = require(configModule)

    return tmpConfigTable
end

Usage of the this snippet and config_file.lua:

local configModule = "config_file"
local configFile = configModule .. ".lua"
local tmpConfig = nil

create_initial_config_file(configFile)

-- Wall of text and logics I do not care about

-- Then user requires some configuration from file (module):
tmpConfig = getConfigFromModule(configModule)

-- User interaction or whatever which updates config:
tmpConfig.maxHP = io.read()
tmpConfig.first_run = false
-- etc etc 

-- Rewrite config file with updated values:
flush_config_to_file(tmpConfig, configFile)

Generated initial config_file.lua file content should be something like this:

local config = {
    ["first_run"] = true,
    ["maxMP"] = nil,
    ["maxHP"] = nil,
}

return config

NOTE: code untested, only show-off of easy usage of Lua for some kind of configuration.

NOTE2: be aware of Lua modules cache'ing: ref by Seth Carnegie

Upvotes: 1

Related Questions