sol2/docs/source/tutorial/cxx-in-lua.rst

129 lines
3.5 KiB
ReStructuredText
Raw Normal View History

C++ in Lua
==========
2016-05-12 03:00:11 +08:00
Using user defined types ("usertype"s, or just "udt"s) is simple with Sol. If you don't call any member variables or functions, then you don't even have to 'register' the usertype at all: just pass it through. But if you want variables and functions on your usertype inside of Lua, you need to register it. We're going to give a short example here that includes a bunch of information on how to work with things.
2016-04-26 03:58:13 +08:00
Take this ``player`` struct in C++ in a header file:
.. code-block:: cpp
:caption: test_player.hpp
struct player {
public:
int bullets;
int speed;
player()
: player(3, 100) {
}
player(int ammo)
: player(ammo, 100) {
}
player(int ammo, int hitpoints)
: bullets(ammo), hp(hitpoints) {
}
void boost () {
speed += 10;
}
bool shoot () {
if (bullets < 1)
return false;
--bullets;
return true;
}
int set_hp(int value) {
hp = value;
}
int get_hp() const {
return hp;
}
private:
int hp;
}
2016-05-12 03:00:11 +08:00
It's a fairly minimal class, but we don't want to have to rewrite this with metatables in Lua. We want this to be part of Lua easily. The following is the Lua code that we'd like to have work properly:
2016-04-26 03:58:13 +08:00
.. code-block:: lua
:caption: player_script.lua
-- call single argument integer constructor
p1 = player.new(2)
-- p2 is still here from being
2016-05-12 03:00:11 +08:00
-- set with lua["p2"] = player(0); below
2016-04-26 03:58:13 +08:00
local p2shoots = p2:shoot()
assert(not p2shoots)
-- had 0 ammo
-- set variable property setter
p1.hp = 545;
-- get variable through property getter
print(p1.hp);
local did_shoot_1 = p1:shoot()
print(did_shoot_1)
print(p1.bullets)
local did_shoot_2 = p1:shoot()
print(did_shoot_2)
print(p1.bullets)
local did_shoot_3 = p1:shoot()
print(did_shoot_3)
-- can read
print(p1.bullets)
-- would error: is a readonly variable, cannot write
-- p1.bullets = 20
p1:boost()
2016-05-12 03:00:11 +08:00
To do this, you bind things using the ``new_usertype`` and ``set_usertype`` methods as shown below. These methods are on both :doc:`table<../api/table>` and :doc:`state(_view)<../api/state>`, but we're going to just use it on ``state``:
2016-04-26 03:58:13 +08:00
.. code-block:: cpp
:caption: player_script.cpp
sol::state lua;
// note that you can set a
// userdata before you register a usertype,
// and it will still carry
// the right metatable if you register it later
2016-05-12 03:00:11 +08:00
// set a variable "p2" of type "player" with 0 ammo
lua["p2"] = player(0);
2016-04-26 03:58:13 +08:00
// make usertype metatable
lua.new_usertype<player>( "player",
// 3 constructors
sol::constructors<sol::types<>, sol::types<int>, sol::types<int, int>>(),
// typical member function that returns a variable
"shoot", &player::shoot,
// typical member function
"boost", &player::boost,
// gets or set the value using member variable syntax
"hp", sol::property(&player::get_hp, &player::set_hp),
// read and write variable
"speed", &player::speed,
// can only read from, not write to
"bullets", sol::readonly( &player::bullets )
);
lua.script_file("player_script.lua");
2016-05-12 03:00:11 +08:00
That script should run fine now, and you can observe and play around with the values. Even more stuff :doc:`you can do<../api/usertype>` is described elsewhere, like initializer functions (private constructors / destructors support), "static" functions callable with ``name.my_function( ... )``, and overloaded member functions.
This is a powerful way to allow reuse of C++ code from Lua beyond just registering functions, and should get you on your way to having more complex classes and data structures!