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>
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
struct test {
|
|
|
|
static int muh_variable;
|
|
|
|
};
|
|
|
|
int test::muh_variable = 25;
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== static_variables ===" << std::endl;
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries();
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.new_usertype<test>("test",
|
|
|
|
"direct",
|
|
|
|
sol::var(2),
|
|
|
|
"global",
|
|
|
|
sol::var(test::muh_variable),
|
|
|
|
"ref_global",
|
|
|
|
sol::var(std::ref(test::muh_variable)));
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
int direct_value = lua["test"]["direct"];
|
|
|
|
// direct_value == 2
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(direct_value == 2);
|
2016-08-23 10:45:06 +08:00
|
|
|
std::cout << "direct_value: " << direct_value << std::endl;
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
int global = lua["test"]["global"];
|
|
|
|
int global2 = lua["test"]["ref_global"];
|
2016-08-23 10:45:06 +08:00
|
|
|
// global == 25
|
2016-08-11 08:39:30 +08:00
|
|
|
// global2 == 25
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(global == 25);
|
|
|
|
sol_c_assert(global2 == 25);
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
std::cout << "First round of values --" << std::endl;
|
|
|
|
std::cout << global << std::endl;
|
|
|
|
std::cout << global2 << std::endl;
|
|
|
|
|
|
|
|
test::muh_variable = 542;
|
|
|
|
|
|
|
|
global = lua["test"]["global"];
|
|
|
|
// global == 25
|
|
|
|
// global is its own memory: was passed by value
|
|
|
|
|
|
|
|
global2 = lua["test"]["ref_global"];
|
|
|
|
// global2 == 542
|
|
|
|
// global2 was passed through std::ref
|
|
|
|
// global2 holds a reference to muh_variable
|
|
|
|
// if muh_variable goes out of scope or is deleted
|
|
|
|
// problems could arise, so be careful!
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(global == 25);
|
|
|
|
sol_c_assert(global2 == 542);
|
2016-08-23 10:45:06 +08:00
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
std::cout << "Second round of values --" << std::endl;
|
|
|
|
std::cout << "global : " << global << std::endl;
|
|
|
|
std::cout << "global2: " << global2 << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|