mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
make sure examples compile
This commit is contained in:
parent
a69c599f99
commit
7515b8c4ad
@ -43,15 +43,15 @@ int main() {
|
|||||||
lua.open_libraries(sol::lib::base, sol::lib::math);
|
lua.open_libraries(sol::lib::base, sol::lib::math);
|
||||||
|
|
||||||
// the simplest way to create a class is through
|
// the simplest way to create a class is through
|
||||||
// sol::state::new_userdata
|
// sol::state::new_usertype
|
||||||
// the first template is the class type
|
// the first template is the class type
|
||||||
// the rest are the constructor parameters
|
// the rest are the constructor parameters
|
||||||
// using new_userdata you can only have one constructor
|
// using new_usertype you can only have one constructor
|
||||||
|
|
||||||
|
|
||||||
// you must make sure that the name of the function
|
// you must make sure that the name of the function
|
||||||
// goes before the member function pointer
|
// goes before the member function pointer
|
||||||
lua.new_userdata<foo, std::string>("foo", "print", &foo::print, "test", &foo::test);
|
lua.new_usertype<foo, std::string>("foo", "print", &foo::print, "test", &foo::test);
|
||||||
|
|
||||||
// making the class from lua is simple
|
// making the class from lua is simple
|
||||||
// same with calling member functions
|
// same with calling member functions
|
||||||
@ -63,8 +63,8 @@ int main() {
|
|||||||
assert(y == 14);
|
assert(y == 14);
|
||||||
|
|
||||||
// if you want a class to have more than one constructor
|
// if you want a class to have more than one constructor
|
||||||
// the way to do so is through set_userdata and creating
|
// the way to do so is through set_usertype and creating
|
||||||
// a userdata yourself with constructor types
|
// a usertype yourself with constructor types
|
||||||
|
|
||||||
{
|
{
|
||||||
// Notice the brace: this means we're in a new scope
|
// Notice the brace: this means we're in a new scope
|
||||||
@ -75,15 +75,15 @@ int main() {
|
|||||||
// the first argument of construction is the name
|
// the first argument of construction is the name
|
||||||
// second is the constructor types
|
// second is the constructor types
|
||||||
// then the rest are function name and member function pointer pairs
|
// then the rest are function name and member function pointer pairs
|
||||||
sol::userdata<vector> udata("vector", ctor, "is_unit", &vector::is_unit);
|
sol::usertype<vector> udata(ctor, "is_unit", &vector::is_unit);
|
||||||
|
|
||||||
// then you must register it
|
// then you must register it
|
||||||
lua.set_userdata(udata);
|
lua.set_usertype("vector", udata);
|
||||||
// You can throw away the userdata after you set it: you do NOT
|
// You can throw away the usertype after you set it: you do NOT
|
||||||
// have to keep it around
|
// have to keep it around
|
||||||
// cleanup happens automagically
|
// cleanup happens automagically
|
||||||
}
|
}
|
||||||
// calling it is the same as new_userdata
|
// calling it is the same as new_usertype
|
||||||
|
|
||||||
lua.script("v = vector.new()\n"
|
lua.script("v = vector.new()\n"
|
||||||
"v = vector.new(12)\n"
|
"v = vector.new(12)\n"
|
||||||
@ -92,7 +92,7 @@ int main() {
|
|||||||
|
|
||||||
// You can even have C++-like member-variable-access
|
// You can even have C++-like member-variable-access
|
||||||
// just pass is public member variables in the same style as functions
|
// just pass is public member variables in the same style as functions
|
||||||
lua.new_userdata<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level);
|
lua.new_usertype<variables>("variables", "low_gravity", &variables::low_gravity, "boost_level", &variables::boost_level);
|
||||||
|
|
||||||
// making the class from lua is simple
|
// making the class from lua is simple
|
||||||
// same with calling member functions/variables
|
// same with calling member functions/variables
|
||||||
|
Loading…
x
Reference in New Issue
Block a user