【问题标题】:How can I write a file with containing a lua table using sol2如何使用 sol2 编写包含 lua 表的文件
【发布时间】:2016-04-28 19:07:24
【问题描述】:

在看到 this 之类的帖子并喜欢其语法后,我决定使用 lua 作为我的程序的配置管理,而 sol2 最近发布了,所以我正在使用它。

所以我的问题是,如何获取我的 lua 状态中的所有变量并将它们吐出到一个文件中?

说,

sol::state lua;
lua["foo"]["bar"] = 2;
lua["foo"]["foobar"] = lua.create_table();

最终会吐出

foo = {
    bar = 2
    foobar = {}
}

这有可能吗?如果可以,怎么做?

【问题讨论】:

标签: c++ lua


【解决方案1】:

我用this serializer序列化我的表格并打印出来,真的很简单!

这是我想出来的

std::string save_table(const std::string& table_name, sol::state& lua)
{
    auto table = lua["serpent"];
    if (!table.valid()) {
        throw std::runtime_error("Serpent not loaded!");
    }
    if (!lua[table_name].valid()) {
        throw std::runtime_error(table_name + " doesn't exist!");
    }
    std::stringstream out;
    out << table_name << " = ";
    sol::function block = table["block"];
    std::string cont = block(lua[table_name]);
    out << cont;
    return std::move(out.str());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2021-03-14
    • 2015-01-22
    • 2021-11-09
    • 1970-01-01
    • 2015-03-08
    • 2019-06-04
    相关资源
    最近更新 更多