mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Fix userdata -> usertype in examples.
This commit is contained in:
parent
cd46aac23c
commit
a5e24b4fa2
|
@ -30,7 +30,7 @@ struct vars {
|
|||
|
||||
int main() {
|
||||
sol::state lua;
|
||||
lua.new_userdata<vars>("vars", "boop", &vars::boop);
|
||||
lua.new_usertype<vars>("vars", "boop", &vars::boop);
|
||||
lua.script("beep = vars.new()\n"
|
||||
"beep.boop = 1");
|
||||
assert(lua.get<vars>("beep").boop == 1);
|
||||
|
|
|
@ -50,7 +50,7 @@ args = parser.parse_args()
|
|||
# general variables
|
||||
include = [ '.', os.path.join('Catch', 'include')]
|
||||
depends = []
|
||||
cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++11' ]
|
||||
cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++11', '-Wno-unused-variable' ]
|
||||
ldflags = []
|
||||
script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||
sol_dir = os.path.join(script_dir, 'sol')
|
||||
|
|
|
@ -43,15 +43,15 @@ int main() {
|
|||
lua.open_libraries(sol::lib::base, sol::lib::math);
|
||||
|
||||
// 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 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
|
||||
// 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
|
||||
// same with calling member functions
|
||||
|
@ -63,8 +63,8 @@ int main() {
|
|||
assert(y == 14);
|
||||
|
||||
// if you want a class to have more than one constructor
|
||||
// the way to do so is through set_userdata and creating
|
||||
// a userdata yourself with constructor types
|
||||
// the way to do so is through set_usertype and creating
|
||||
// a usertype yourself with constructor types
|
||||
|
||||
{
|
||||
// Notice the brace: this means we're in a new scope
|
||||
|
@ -75,15 +75,20 @@ int main() {
|
|||
// the first argument of construction is the name
|
||||
// second is the constructor types
|
||||
// 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
|
||||
lua.set_userdata(udata);
|
||||
// You can throw away the userdata after you set it: you do NOT
|
||||
// by default, the registered name is done through demangling of T in sol::usertype<T>
|
||||
// using the demangling API provided by the compiler
|
||||
// if you don't want to use the unmangled name, you can provide your own name like so:
|
||||
// lua.set_usertype("vector", udata);
|
||||
lua.set_usertype(udata);
|
||||
|
||||
// You can throw away the usertype after you set it: you do NOT
|
||||
// have to keep it around
|
||||
// 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"
|
||||
"v = vector.new(12)\n"
|
||||
|
@ -92,7 +97,7 @@ int main() {
|
|||
|
||||
// You can even have C++-like member-variable-access
|
||||
// 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
|
||||
// same with calling member functions/variables
|
Loading…
Reference in New Issue
Block a user