mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Updated examples and readme changes to reflect new API (userdata example kept erroneous info that userdata<T>
must be kept alive)
This commit is contained in:
parent
368d78d463
commit
ef8fa7395f
17
README.md
17
README.md
|
@ -18,6 +18,23 @@ int main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <sol.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
struct vars {
|
||||||
|
int boop = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sol::state lua;
|
||||||
|
lua.new_userdata<vars>("vars", "boop", &vars::boop);
|
||||||
|
lua.script("local beep = vars.new()\n"
|
||||||
|
"beep.boop = 1");
|
||||||
|
assert(lua.get<vars>("beep").boop == 1);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
More examples are given in the examples directory.
|
More examples are given in the examples directory.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
|
@ -32,6 +32,12 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct variables {
|
||||||
|
bool low_gravity = false;
|
||||||
|
int boost_level = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
lua.open_libraries(sol::lib::base, sol::lib::math);
|
lua.open_libraries(sol::lib::base, sol::lib::math);
|
||||||
|
@ -60,13 +66,11 @@ int main() {
|
||||||
// the way to do so is through set_userdata and creating
|
// the way to do so is through set_userdata and creating
|
||||||
// a userdata yourself with constructor types
|
// a userdata yourself with constructor types
|
||||||
|
|
||||||
|
{
|
||||||
|
// Notice the brace: this means we're in a new scope
|
||||||
// first, define the different types of constructors
|
// first, define the different types of constructors
|
||||||
sol::constructors<sol::types<>, sol::types<float>, sol::types<float, float>> ctor;
|
sol::constructors<sol::types<>, sol::types<float>, sol::types<float, float>> ctor;
|
||||||
|
|
||||||
// second, create a new userdata class
|
|
||||||
// note that you have to make sure the class is
|
|
||||||
// alive throughout the scope of the program.
|
|
||||||
|
|
||||||
// the only template parameter is the class type
|
// the only template parameter is the class type
|
||||||
// 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
|
||||||
|
@ -75,11 +79,26 @@ int main() {
|
||||||
|
|
||||||
// then you must register it
|
// then you must register it
|
||||||
lua.set_userdata(udata);
|
lua.set_userdata(udata);
|
||||||
|
// You can throw away the userdata 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_userdata
|
||||||
|
|
||||||
lua.script("v = vector.new()\n"
|
lua.script("v = vector.new()\n"
|
||||||
"v = vector.new(12)\n"
|
"v = vector.new(12)\n"
|
||||||
"v = vector.new(10, 10)\n"
|
"v = vector.new(10, 10)\n"
|
||||||
"assert(not v:is_unit())\n");
|
"assert(not v:is_unit())\n");
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// making the class from lua is simple
|
||||||
|
// same with calling member functions
|
||||||
|
lua.script("local vars = variables.new()\n"
|
||||||
|
"assert(not vars.low_gravity)\n"
|
||||||
|
"vars.low_gravity = true\n"
|
||||||
|
"local x = vars.low_gravity\n"
|
||||||
|
"assert(x)");
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user