c++/Lua - 将多个 lua 文件名加载到 c++ 字符串中[自动]

c++/Lua - Load multiple lua file names into a c++ string[automatically]

我想将字典中的多个文件名(例如 "Data/lua_files")加载到 C++ 字符串中,而不是手动添加它们。例如,每当我添加一个新文件时,我应该能够在启动我的程序时使用它而无需添加任何代码。目前我使用的是Sol2.0.

我可以将所有文件名保存到一个新的 .txt 文件中吗?(使用 lua 脚本?) 有什么办法可以存档吗?

我检查了 Google 但没有找到任何东西

谢谢!

如果 "dictionary" 你的意思是 Lua table:

fileNames = { 
    "file1.txt",
    "file2.txt",
    "file3.txt"
}

那就和table.concat(fileNames, ",")一样简单了。它将 return 一个字符串,然后您可以使用该字符串,例如保存到全局变量中:

fileNamesString = table.concat(fileNames, ",")

然后用Sol从C++端读取。不过,我想知道是否有必要完成这个额外的步骤;我以为图书馆支持直接 table 访问。考虑到这一点,只需:

sol::lua_state lua;
// read your script file here

for (std::string const& fileName : lua["fileNames"]) {
    // do your operation
}