diff --git a/examples/basic.cpp b/examples/basic.cpp new file mode 100644 index 00000000..4852aa8e --- /dev/null +++ b/examples/basic.cpp @@ -0,0 +1,14 @@ +#include + +int main() { + // create an empty lua state + sol::state lua; + + // by default, libraries are not opened + // you can open libraries by using open_libraries + // the libraries reside in the sol::lib enum class + lua.open_libraries(sol::lib::base); + + // call lua code directly + lua.script("print('hello world')"); +} \ No newline at end of file diff --git a/examples/config.cpp b/examples/config.cpp index fb92eb68..9ab7a8e3 100644 --- a/examples/config.cpp +++ b/examples/config.cpp @@ -1,9 +1,9 @@ -// shows how to load basic data to a struct - #include #include #include +// shows how to load basic data to a struct + struct config { std::string name; int width; diff --git a/examples/functions.cpp b/examples/functions.cpp new file mode 100644 index 00000000..ee85ebf3 --- /dev/null +++ b/examples/functions.cpp @@ -0,0 +1,69 @@ +#include +#include + +inline int my_add(int x, int y) { + return x + y; +} + +struct multiplier { + int operator()(int x) { + return x * 10; + } + + static int by_five(int x) { + return x * 5; + } +}; + +int main() { + sol::state lua; + lua.open_libraries(sol::lib::base); + + // setting a function is simple + lua.set_function("my_add", my_add); + + // you could even use a lambda + lua.set_function("my_mul", [](double x, double y) { return x * y; }); + + // member function pointers and functors as well + lua.set_function("mult_by_ten", multiplier{}); + lua.set_function("mult_by_five", &multiplier::by_five); + + // assert that the functions work + lua.script("assert(my_add(10, 11) == 21)"); + lua.script("assert(my_mul(4.5, 10) == 45)"); + lua.script("assert(mult_by_ten(50) == 500)"); + lua.script("assert(mult_by_five(10) == 50)"); + + // using lambdas, functions could have state. + int x = 0; + lua.set_function("inc", [&x]() { x += 10; }); + + // calling a stateful lambda modifies the value + lua.script("inc()"); + assert(x == 10); + + // this can be done as many times as you want + lua.script("inc()\ninc()\ninc()"); + assert(x == 40); + + // retrieval of a function is done similarly + // to other variables, using sol::function + sol::function add = lua["my_add"]; + assert(add.call(10, 11) == 21); + + // multi-return functions are supported using + // std::tuple as the interface. + lua.set_function("multi", []{ return std::make_tuple(10, "goodbye"); }); + lua.script("x, y = multi()"); + lua.script("assert(x == 10 and y == 'goodbye')"); + + auto multi = lua.get("multi"); + int first; + std::string second; + std::tie(first, second) = multi.call(); + + // assert the values + assert(first == 10); + assert(second == "goodbye"); +} \ No newline at end of file diff --git a/examples/variables.cpp b/examples/variables.cpp new file mode 100644 index 00000000..d627980d --- /dev/null +++ b/examples/variables.cpp @@ -0,0 +1,34 @@ +#include +#include + +int main() { + sol::state lua; + + // need the base library for assertions + lua.open_libraries(sol::lib::base); + + // basic setting of a variable + // through multiple ways + lua["x"] = 10; + lua.set("y", "hello"); + + // assert values are as given + lua.script("assert(x == 10)"); + lua.script("assert(y == 'hello')"); + + + // basic retrieval of a variable + // through multiple ways + int x = lua["x"]; + auto y = lua.get("y"); + + int x2; + std::string y2; + std::tie(x2, y2) = lua.get("x", "y"); + + // assert the values + assert(x == 10); + assert(y == "hello"); + assert(x2 == 10); + assert(y2 == "hello"); +} \ No newline at end of file