2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2016-03-03 09:45:52 +08:00
|
|
|
#include <iostream>
|
2014-01-19 11:59:59 +08:00
|
|
|
|
|
|
|
inline int my_add(int x, int y) {
|
2017-07-10 00:00:57 +08:00
|
|
|
return x + y;
|
2014-01-19 11:59:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct multiplier {
|
2021-03-06 14:03:23 +08:00
|
|
|
int operator()(int x) {
|
|
|
|
return x * 10;
|
|
|
|
}
|
2014-01-19 11:59:59 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
static int by_five(int x) {
|
|
|
|
return x * 5;
|
|
|
|
}
|
2014-01-19 11:59:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== functions ===" << std::endl;
|
2016-08-11 08:39:30 +08:00
|
|
|
|
2016-02-11 01:12:09 +08:00
|
|
|
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
|
2021-03-06 14:03:23 +08:00
|
|
|
lua.set_function("mult_by_ten", multiplier {});
|
2016-02-11 01:12:09 +08:00
|
|
|
lua.set_function("mult_by_five", &multiplier::by_five);
|
|
|
|
|
|
|
|
// assert that the functions work
|
2017-12-26 21:04:54 +08:00
|
|
|
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)");
|
2016-02-11 01:12:09 +08:00
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
// using lambdas, functions can have state.
|
2016-02-11 01:12:09 +08:00
|
|
|
int x = 0;
|
|
|
|
lua.set_function("inc", [&x]() { x += 10; });
|
|
|
|
|
|
|
|
// calling a stateful lambda modifies the value
|
|
|
|
lua.script("inc()");
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(x == 10);
|
2016-03-03 09:45:52 +08:00
|
|
|
if (x == 10) {
|
|
|
|
// Do something based on this information
|
2016-08-23 10:45:06 +08:00
|
|
|
std::cout << "Yahoo! x is " << x << std::endl;
|
2016-03-03 09:45:52 +08:00
|
|
|
}
|
2016-02-11 01:12:09 +08:00
|
|
|
|
|
|
|
// this can be done as many times as you want
|
2016-08-11 08:39:30 +08:00
|
|
|
lua.script(R"(
|
|
|
|
inc()
|
|
|
|
inc()
|
|
|
|
inc()
|
|
|
|
)");
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(x == 40);
|
2016-03-03 09:45:52 +08:00
|
|
|
if (x == 40) {
|
|
|
|
// Do something based on this information
|
2016-08-23 10:45:06 +08:00
|
|
|
std::cout << "Yahoo! x is " << x << std::endl;
|
2016-03-03 09:45:52 +08:00
|
|
|
}
|
2017-07-10 00:00:57 +08:00
|
|
|
|
2016-02-11 01:12:09 +08:00
|
|
|
// retrieval of a function is done similarly
|
|
|
|
// to other variables, using sol::function
|
|
|
|
sol::function add = lua["my_add"];
|
|
|
|
int value = add(10, 11);
|
2016-08-11 08:39:30 +08:00
|
|
|
// second way to call the function
|
|
|
|
int value2 = add.call<int>(10, 11);
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(value == 21);
|
|
|
|
sol_c_assert(value2 == 21);
|
2016-08-11 08:39:30 +08:00
|
|
|
if (value == 21 && value2 == 21) {
|
2016-08-23 10:45:06 +08:00
|
|
|
std::cout << "Woo, value is 21!" << std::endl;
|
2016-03-03 09:45:52 +08:00
|
|
|
}
|
2016-02-11 01:12:09 +08:00
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
std::cout << std::endl;
|
2017-07-10 00:00:57 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-01-19 11:59:59 +08:00
|
|
|
}
|