ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.16k stars 504 forks source link

Function arguments as a table #1381

Closed sizeoftrickster closed 2 years ago

sizeoftrickster commented 2 years ago

Greetings, comrades. I want to make a function whose argument is a table, and get data from this table.

local library = require("Sol2module")

library.testFunction( 
    "SomeData", --[[ First argument ]]--
    { --[[ Second argument ]]
        First = "1",
        Second = "2",
        Third = "3"
    }
)

So how do we get the data of the second argument?

module.set_function("testFunction", []( sol::this_state ts, std::string firstAgrument, ... secondArgument ) {
    sol::state_view lua{ ts };
    lua["print"]( secondArgument["First"] ); // should be 1
});
MJCollett commented 2 years ago

Your ... should be sol::table. Or if you want maximum speed and promise to be a good boycomrade, sol::stack_table.