Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 通过用户输入将变量值保存到文件中_Lua - Fatal编程技术网

Lua 通过用户输入将变量值保存到文件中

Lua 通过用户输入将变量值保存到文件中,lua,Lua,基本上我有这个代码,当第一次运行时,它将为用户打开一个通道,在配置文件中输入变量的值 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() -- som

基本上我有这个代码,当第一次运行时,它将为用户打开一个通道,在配置文件中输入变量的值

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
上面的代码在我的主文件(“main.lua”)中,下面的代码在我的配置文件(“config.lua”)中:

我需要它做的是,在运行firstRun Channel函数之后,它将在“config.lua”文件中保存“maxMP”、“maxHP”等值,并保存firstRun=false


我无法将其保存在.txt文件中,它必须保存在“config.lua”中,我只是不知道如何使用

基本上,使用纯lua手动覆盖配置文件听起来像是一项微不足道的任务

类似这样(初始写入,对默认或当前配置进行任何更改):

此代码段和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)
生成的初始config_file.lua文件内容应如下所示:

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

return config
注意:未经测试的代码,仅展示Lua在某些配置中的易用性


注2:注意Lua模块缓存:通过

@hjpotter92我发誓我已经尝试过了,但无法理解,我只是不知道如何在用户输入中使用它channel@Mojimi啊。更新到一些功能性更强的方法,包括注释、在何处以及如何使用用户输入更改值。嗯,这应该只是一个想法,而不是完整的代码。
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)
local config = {
    ["first_run"] = true,
    ["maxMP"] = nil,
    ["maxHP"] = nil,
}

return config