2017-08-25 02:39:02 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS 1
|
2014-01-19 11:59:59 +08:00
|
|
|
#include <sol.hpp>
|
2017-04-03 04:10:00 +08:00
|
|
|
|
2014-01-19 11:59:59 +08:00
|
|
|
#include <cassert>
|
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 {
|
|
|
|
int operator()(int x) {
|
|
|
|
return x * 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int by_five(int x) {
|
|
|
|
return x * 5;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
2016-08-11 08:39:30 +08:00
|
|
|
std::cout << "=== functions example ===" << std::endl;
|
|
|
|
|
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
|
|
|
|
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)");
|
|
|
|
|
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()");
|
|
|
|
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()
|
|
|
|
)");
|
2016-02-11 01:12:09 +08:00
|
|
|
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);
|
2016-02-11 01:12:09 +08:00
|
|
|
assert(value == 21);
|
2016-08-11 08:39:30 +08:00
|
|
|
assert(value2 == 21);
|
|
|
|
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
|
|
|
}
|