sol2 repository

Sol 2.19

a fast, simple C++ and Lua Binding

When you need to hit the ground running with Lua and C++, Sol is the go-to framework for high-performance binding with an easy to use API.

“I need feature X, maybe you have it?”

Take a look at the Features page: it links to much of the API. You can also just straight up browse the api or ease in with the tutorials. To know more about the implementation for usertypes, see here To know how function arguments are handled, see this note. Don’t see a feature you want? Send inquiries for support for a particular abstraction to the issues tracker.

the basics:

Note

More examples can be found in the examples directory

functions
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <sol.hpp>
#include <cassert>

int main() {
        sol::state lua;
        int x = 0;
        lua.set_function("beep", [&x]{ ++x; });
        lua.script("beep()");
        assert(x == 1);

        sol::function beep = lua["beep"];
        beep();
        assert(x == 2);

        return 0;
}
linking C++ structures to Lua
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <sol.hpp>
#include <cassert>

struct vars {
        int boop = 0;

        int bop () const {
                return boop + 1;
        }
};

int main() {
        sol::state lua;
        lua.new_usertype<vars>("vars",
                "boop", &vars::boop
                "bop", &vars::bop);
        lua.script("beep = vars.new()\n"
                "beep.boop = 1\n"
                "bopvalue = beep:bop()");

        vars& beep = lua["beep"];
        int bopvalue = lua["bopvalue"];

        assert(beep.boop == 1);
        assert(lua.get<vars>("beep").boop == 1);
        assert(beep.bop() == 2);
        assert(bopvalue == 2);

        return 0;
}

helping out

You can support the library by submitting pull requests to fix anything (the code, typos, even contribute your own examples).

You can support me and my family by donating a little something here.

Thank you for using sol2!

Indices and tables