0

I want to load multiple file names from a dictionary(for example "Data/lua_files") into a C++ string, without adding them manually. For example whenever I add a new file I should be able to use it when I start my program without adding any code. Currently I use Sol2.0.

Can I save all file names into a new .txt file?(with a lua script?) Is there any way to archive that?

I checked Google but didnt find anything

Thanks!

1 Answer 1

1

If by a "dictionary" you mean a Lua table:

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

Then it's as easy as table.concat(fileNames, ","). It will return a string which you can then e.g. save into a global variable:

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

And then use Sol to read it from the C++ side. I wonder if it's necessary to go through that extra step, though; I thought that the library supported direct table access. With that in mind, it'd be enough to just:

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

for (std::string const& fileName : lua["fileNames"]) {
    // do your operation
}
Sign up to request clarification or add additional context in comments.

3 Comments

@AutobahnPolizei Oh, okay. So you want to load all lua scripts from a folder? From Lua side or C++ side? (who's gonna do the ls equivalent?)
Yes, into a C++ String/C++ side(?). And whenever I create a new .lua file the new file name should be added automaically.
@AutobahnPolizei Well then you're gonna need something like Boost.Filesystem. It's not a C++ standard feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.