# sol2 3.5.0 documentation

## tutorial: quick ‘n’ dirty

### Search the Documentation

# tutorial: quick ‘n’ dirty

These are all the things. Use your browser’s search to find things you want.

Note

After you learn the basics of sol, it is usually advised that if you think something can work, you should TRY IT. It will probably work!

Note

All of the code below is available at the sol2 tutorial examples.

Note

Make sure to add `SOL_ALL_SAFETIES_ON` preprocessor define to your build configuration to turn safety on.

## asserts / prerequisites

You’ll need to `#include <sol/sol.hpp>` somewhere in your code. sol is header-only, so you don’t need to compile anything. However, Lua must be compiled and available. See the getting started tutorial for more details.

Below, you will see use of a function called `SOL_ASSERT`. This is an assert macro that comes with sol2 for the expression purpose of checking things; it’s value is immaterial.

## opening a state

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main(int, char*[]) {
     7	std::cout << "=== opening a state ===" << std::endl;
     8
     9	sol::state lua;
    10	// open some common libraries
    11	lua.open_libraries(sol::lib::base, sol::lib::package);
    12	lua.script("print('bark bark bark!')");
    13
    14	std::cout << std::endl;
    15
    16	return 0;
    17}

## using sol2 on a lua_State*

For your system/game that already has Lua or uses an in-house or pre-rolled Lua system (LuaBridge, kaguya, Luwra, etc.), but you’d still like sol2 and nice things:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int use_sol2(lua_State* L) {
     7	sol::state_view lua(L);
     8	lua.script("print('bark bark bark!')");
     9	return 0;
    10}
    11
    12int main(int, char*[]) {
    13	std::cout << "=== opening sol::state_view on raw Lua ==="
    14	          << std::endl;
    15
    16	lua_State* L = luaL_newstate();
    17	luaL_openlibs(L);
    18
    19	lua_pushcclosure(L, &use_sol2, 0);
    20	lua_setglobal(L, "use_sol2");
    21
    22	if (luaL_dostring(L, "use_sol2()")) {
    23		lua_error(L);
    24		return -1;
    25	}
    26
    27	std::cout << std::endl;
    28
    29	return 0;
    30}

## running lua code

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <fstream>
     5#include <iostream>
     6
     7int main(int, char*[]) {
     8	std::cout << "=== running lua code ===" << std::endl;
     9
    10	{
    11	lua.open_libraries(sol::lib::base);
    12
    13	// load and execute from string
    14	lua.script("a = 'test'");
    15	// load and execute from file
    16	lua.script_file("a_lua_script.lua");
    17
    18	// run a script, get the result
    19	int value = lua.script("return 54");
    20	SOL_ASSERT(value == 54);

To run Lua code but have an error handler in case things go wrong:

     1	     [](lua_State*, sol::protected_function_result pfr) {
     2		     // pfr will contain things that went wrong, for
     3		     // either loading or executing the script Can
     4		     // throw your own custom error You can also just
     5		     // return it, and let the call-site handle the
     6		     // error if necessary.
     7		     return pfr;
     8	     });
     9	// it did not work
    10	SOL_ASSERT(!bad_code_result.valid());
    11
    12	// the default handler panics or throws, depending on your
    13	return 0;
    14}

You can see more use of safety by employing the use of .safe_script, which returns a protected result you can use to properly check for errors and similar.

Note

If you have the safety definitions on, .script will call into the .safe_script versions automatically. Otherwise, it will call into the .unsafe_script versions.

## running lua code (low-level)

You can use the individual load and function call operator to load, check, and then subsequently run and check code.

Warning

This is ONLY if you need some sort of fine-grained control: for 99% of cases, running lua code is preferred and avoids pitfalls in not understanding the difference between script/load and needing to run a chunk after loading it.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <fstream>
     5#include <iostream>
     6#include <cstdio>
     7
     8int main(int, char*[]) {
     9	std::cout << "=== running lua code (low level) ==="
    10	          << std::endl;
    11
    12	sol::state lua;
    13	lua.open_libraries(sol::lib::base);
    14
    15	// load file without execute
    16	sol::load_result script1
    17	     = lua.load_file("a_lua_script.lua");
    18	// execute
    19	script1();
    20
    21	// load string without execute
    22	sol::load_result script2 = lua.load("a = 'test'");
    23	// execute
    24	sol::protected_function_result script2result = script2();
    25	// optionally, check if it worked
    26	if (script2result.valid()) {
    27		// yay!
    28	}
    29	else {
    30		// aww
    31	}
    32
    33	sol::load_result script3 = lua.load("return 24");
    34	// execute, get return value
    35	int value2 = script3();
    36
    37	return 0;
    38}

You can also develop custom loaders that pull from things that are not strings or files.

## passing arguments to scripts

Arguments to Lua scripts can be passed by first loading the file or script blob, and then calling it using sol’s abstractions. Then, in the script, access the variables with a … on the left hand side of an assignment:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main(int, char*[]) {
     7	std::cout << "=== passing arguments to scripts ==="
     8	          << std::endl;
     9
    10	sol::state lua;
    11	lua.open_libraries(sol::lib::base);
    12
    13	const auto& my_script = R"(
    14local a,b,c = ...
    15print(a,b,c)
    16	)";
    17
    18	sol::load_result fx = lua.load(my_script);
    19	if (!fx.valid()) {
    20		sol::error err = fx;
    21		std::cerr << "failed to load string-based script into "
    22		             "the program"
    23		          << err.what() << std::endl;
    24	}
    25
    26	// prints "your arguments here"
    27	fx("your", "arguments", "here");
    28
    29	return 0;
    30}

## transferring functions (dumping bytecode)

You can dump the bytecode of a function, which allows you to transfer it to another state (or save it, or load it). Note that bytecode is typically specific to the Lua version!

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6
     7int main() {
     8	std::cout << "=== dump (serialize between states) ==="
     9	          << std::endl;
    10
    11	// 2 states, transferring function from 1 to another
    12	sol::state lua;
    13	sol::state lua2;
    14
    15	// we're not going to run the code on the first
    16	// state, so we only actually need
    17	// the base lib on the second state
    18	// (where we will run the serialized bytecode)
    19	lua2.open_libraries(sol::lib::base);
    20
    21	// load this code (but do not run)
    22	sol::load_result lr
    23	     = lua.load("a = function (v) print(v) return v end");
    24	// check if it's sucessfully loaded
    25	SOL_ASSERT(lr.valid());
    26
    27	// turn it into a function, then dump the bytecode
    28	sol::protected_function target
    29	     = lr.get<sol::protected_function>();
    30	sol::bytecode target_bc = target.dump();
    31
    32	// reload the byte code
    33	// in the SECOND state
    34	auto result2 = lua2.safe_script(
    35	     target_bc.as_string_view(), sol::script_pass_on_error);
    36	// check if it was done properly
    37	SOL_ASSERT(result2.valid());
    38
    39	// check in the second state if it was valid
    40	sol::protected_function pf = lua2["a"];
    41	int v = pf(25557);
    42	SOL_ASSERT(v == 25557);
    43
    44	return 0;
    45}

## set and get variables

You can set/get everything using table-like syntax.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7	lua.open_libraries(sol::lib::base);
     8
     9	// integer types
    10	lua.set("number", 24);
    11	// floating point numbers
    12	lua["number2"] = 24.5;
    13	// string types
    14	lua["important_string"] = "woof woof";
    15	// is callable, therefore gets stored as a function that can
    16	// be called
    17	lua["a_function"] = []() { return 100; };
    18	// make a table
    19	lua["some_table"] = lua.create_table_with("value", 24);

Equivalent to loading lua values like so:

     1	// equivalent to this code
     2	std::string equivalent_code = R"(
     3		t = {
     4			number = 24,
     5			number2 = 24.5,
     6			important_string = "woof woof",
     7			a_function = function () return 100 end,
     8			some_table = { value = 24 }
     9		}
    10	)";
    11
    12	// check in Lua
    13	lua.script(equivalent_code);

You can show they are equivalent:

    1	lua.script(R"(
    2		assert(t.number == number)
    3		assert(t.number2 == number2)
    4		assert(t.important_string == important_string)
    5		assert(t.a_function() == a_function())
    6		assert(t.some_table.value == some_table.value)
    7	)");

Retrieve these variables using this syntax:

     1	// implicit conversion
     2	int number = lua["number"];
     3	SOL_ASSERT(number == 24);
     4	// explicit get
     5	auto number2 = lua.get<double>("number2");
     6	SOL_ASSERT(number2 == 24.5);
     7	// strings too
     8	std::string important_string = lua["important_string"];
     9	SOL_ASSERT(important_string == "woof woof");
    10	// dig into a table
    11	int value = lua["some_table"]["value"];
    12	SOL_ASSERT(value == 24);
    13	// get a function
    14	sol::function a_function = lua["a_function"];
    15	int value_is_100 = a_function();
    16	// convertible to std::function
    17	std::function<int()> a_std_function = a_function;
    18	int value_is_still_100 = a_std_function();
    19	SOL_ASSERT(value_is_100 == 100);
    20	SOL_ASSERT(value_is_still_100 == 100);

Retrieve Lua types using `object` and other `sol::` types.

     1	sol::object number_obj = lua.get<sol::object>("number");
     2	// sol::type::number
     3	sol::type t1 = number_obj.get_type();
     4	SOL_ASSERT(t1 == sol::type::number);
     5
     6	sol::object function_obj = lua["a_function"];
     7	// sol::type::function
     8	sol::type t2 = function_obj.get_type();
     9	SOL_ASSERT(t2 == sol::type::function);
    10	bool is_it_really = function_obj.is<std::function<int()>>();
    11	SOL_ASSERT(is_it_really);
    12
    13	// will not contain data
    14	sol::optional<int> check_for_me = lua["a_function"];
    15	SOL_ASSERT(check_for_me == sol::nullopt);
    16
    17	return 0;
    18}

You can erase things by setting it to `nullptr` or `sol::lua_nil`.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7	lua.open_libraries(sol::lib::base);
     8
     9	lua.script("exists = 250");
    10
    11	int first_try = lua.get_or("exists", 322);
    12	SOL_ASSERT(first_try == 250);
    13
    14	lua.set("exists", sol::lua_nil);
    15	int second_try = lua.get_or("exists", 322);
    16	SOL_ASSERT(second_try == 322);
    17
    18	return 0;
    19}

Note that if its a userdata/usertype for a C++ type, the destructor will run only when the garbage collector deems it appropriate to destroy the memory. If you are relying on the destructor being run when its set to `sol::lua_nil`, you’re probably committing a mistake.

## tables

Tables can be manipulated using accessor-syntax. Note that sol::state is a table and all the methods shown here work with `sol::state`, too.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6
     7	sol::state lua;
     8	lua.open_libraries(sol::lib::base);
     9
    10	lua.script(R"(
    11		abc = { [0] = 24 }
    12		def = {
    13			ghi = {
    14				bark = 50,
    15				woof = abc
    16			}
    17		}
    18	)");
    19
    20	sol::table abc = lua["abc"];
    21	sol::table def = lua["def"];
    22	sol::table ghi = lua["def"]["ghi"];
    23
    24	int bark1 = def["ghi"]["bark"];
    25	int bark2 = lua["def"]["ghi"]["bark"];
    26	SOL_ASSERT(bark1 == 50);
    27	SOL_ASSERT(bark2 == 50);
    28
    29	int abcval1 = abc[0];
    30	int abcval2 = ghi["woof"][0];
    31	SOL_ASSERT(abcval1 == 24);
    32	SOL_ASSERT(abcval2 == 24);
    33
    34	sol::optional<int> will_not_error

If you’re going deep, be safe:

     1	     = lua["abc"]["DOESNOTEXIST"]["ghi"];
     2	SOL_ASSERT(will_not_error == sol::nullopt);
     3
     4	int also_will_not_error
     5	     = lua["abc"]["def"]["ghi"]["jklm"].get_or(25);
     6	SOL_ASSERT(also_will_not_error == 25);
     7
     8	// if you don't go safe,
     9	// will throw (or do at_panic if no exceptions)
    10	// int aaaahhh = lua["boom"]["the_dynamite"];
    11
    12	return 0;
    13}

## make tables

There are many ways to make a table. Here’s an easy way for simple ones:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7	lua.open_libraries(sol::lib::base);
     8
     9	lua["abc_sol2"] = lua.create_table_with(0, 24);
    10
    11	sol::table inner_table = lua.create_table_with("bark",
    12	     50,
    13	     // can reference other existing stuff too
    14	     "woof",
    15	     lua["abc_sol2"]);
    16	lua.create_named_table("def_sol2", "ghi", inner_table);
    17
    18	std::string code = R"(
    19		abc = { [0] = 24 }
    20		def = {
    21			ghi = {

Equivalent Lua code, and check that they’re equivalent:

     1				bark = 50,
     2				woof = abc
     3			}
     4		}
     5	)";
     6
     7	lua.script(code);
     8	lua.script(R"(
     9		assert(abc_sol2[0] == abc[0])
    10		assert(def_sol2.ghi.bark == def.ghi.bark)
    11	)");
    12
    13	return 0;
    14}

You can put anything you want in tables as values or keys, including strings, numbers, functions, other tables.

Note that this idea that things can be nested is important and will help later when you get into namespacing.

## functions

They’re easy to use, from Lua and from C++:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7	lua.open_libraries(sol::lib::base);
     8
     9	lua.script("function f (a, b, c, d) return 1 end");
    10	lua.script("function g (a, b) return a + b end");
    11
    12	// sol::function is often easier:
    13	// takes a variable number/types of arguments...
    14	sol::function fx = lua["f"];
    15	// fixed signature std::function<...>
    16	// can be used to tie a sol::function down
    17	std::function<int(int, double, int, std::string)> stdfx
    18	     = fx;
    19
    20	int is_one = stdfx(1, 34.5, 3, "bark");
    21	SOL_ASSERT(is_one == 1);
    22	int is_also_one = fx(1, "boop", 3, "bark");
    23	SOL_ASSERT(is_also_one == 1);
    24
    25	// call through operator[]
    26	int is_three = lua["g"](1, 2);
    27	SOL_ASSERT(is_three == 3);
    28	double is_4_8 = lua["g"](2.4, 2.4);
    29	SOL_ASSERT(is_4_8 == 4.8);
    30
    31	return 0;
    32}

If you need to protect against errors and parser problems and you’re not ready to deal with Lua’s `longjmp` problems (if you compiled with C), use sol::protected_function.

You can bind member variables as functions too, as well as all KINDS of function-like things:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6void some_function() {
     7	std::cout << "some function!" << std::endl;
     8}
     9
    10void some_other_function() {
    11	std::cout << "some other function!" << std::endl;
    12}
    13
    14struct some_class {
    15	int variable = 30;
    16
    17	double member_function() {
    18		return 24.5;
    19	}
    20};
    21
    22int main(int, char*[]) {
    23	std::cout << "=== functions (all) ===" << std::endl;
    24
    25	sol::state lua;
    26	lua.open_libraries(sol::lib::base);
    27
    28	// put an instance of "some_class" into lua
    29	// (we'll go into more detail about this later
    30	// just know here that it works and is
    31	// put into lua as a userdata
    32	lua.set("sc", some_class());
    33
    34	// binds a plain function
    35	lua["f1"] = some_function;
    36	lua.set_function("f2", &some_other_function);
    37
    38	// binds just the member function
    39	lua["m1"] = &some_class::member_function;
    40
    41	// binds the class to the type
    42	lua.set_function(
    43	     "m2", &some_class::member_function, some_class {});
    44
    45	// binds just the member variable as a function
    46	lua["v1"] = &some_class::variable;
    47
    48	// binds class with member variable as function
    49	lua.set_function(
    50	     "v2", &some_class::variable, some_class {});

The lua code to call these things is:

     1	lua.script(R"(
     2	f1() -- some function!
     3	f2() -- some other function!
     4
     5	-- need class instance if you don't bind it with the function
     6	print(m1(sc)) -- 24.5
     7	-- does not need class instance: was bound to lua with one
     8	print(m2()) -- 24.5
     9
    10	-- need class instance if you
    11	-- don't bind it with the function
    12	print(v1(sc)) -- 30
    13	-- does not need class instance:
    14	-- it was bound with one
    15	print(v2()) -- 30
    16
    17	-- can set, still
    18	-- requires instance
    19	v1(sc, 212)
    20	-- can set, does not need
    21	-- class instance: was bound with one
    22	v2(254)
    23
    24	print(v1(sc)) -- 212
    25	print(v2()) -- 254
    26	)");
    27
    28	std::cout << std::endl;
    29
    30	return 0;
    31}

You can use `sol::readonly( &some_class::variable )` to make a variable readonly and error if someone tries to write to it.

## self call

You can pass the `self` argument through C++ to emulate ‘member function’ calls in Lua.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main() {
     7	std::cout << "=== self_call ===" << std::endl;
     8
     9	sol::state lua;
    10	lua.open_libraries(
    11	     sol::lib::base, sol::lib::package, sol::lib::table);
    12
    13	// a small script using 'self' syntax
    14	lua.script(R"(
    15	some_table = { some_val = 100 }
    16
    17	function some_table:add_to_some_val(value)
    18	    self.some_val = self.some_val + value
    19	end
    20
    21	function print_some_val()
    22	    print("some_table.some_val = " .. some_table.some_val)
    23	end
    24	)");
    25
    26	// do some printing
    27	lua["print_some_val"]();
    28	// 100
    29
    30	sol::table self = lua["some_table"];
    31	self["add_to_some_val"](self, 10);
    32	lua["print_some_val"]();
    33
    34	std::cout << std::endl;
    35
    36	return 0;
    37}

## multiple returns from lua

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7
     8	lua.script("function f (a, b, c) return a, b, c end");
     9
    10	std::tuple<int, int, int> result;
    11	result = lua["f"](100, 200, 300);
    12	// result == { 100, 200, 300 }
    13	int a;
    14	int b;
    15	std::string c;
    16	sol::tie(a, b, c) = lua["f"](100, 200, "bark");
    17	SOL_ASSERT(a == 100);
    18	SOL_ASSERT(b == 200);
    19	SOL_ASSERT(c == "bark");
    20
    21	return 0;
    22}

## multiple returns to lua

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main(int, char*[]) {
     6	sol::state lua;
     7	lua.open_libraries(sol::lib::base);
     8
     9	lua["f"] = [](int a, int b, sol::object c) {
    10		// sol::object can be anything here: just pass it
    11		// through
    12		return std::make_tuple(a, b, c);
    13	};
    14
    15	std::tuple<int, int, int> result = lua["f"](100, 200, 300);
    16	const std::tuple<int, int, int> expected(100, 200, 300);
    17	SOL_ASSERT(result == expected);
    18
    19	std::tuple<int, int, std::string> result2;
    20	result2 = lua["f"](100, 200, "BARK BARK BARK!");
    21	const std::tuple<int, int, std::string> expected2(
    22	     100, 200, "BARK BARK BARK!");
    23	SOL_ASSERT(result2 == expected2);
    24
    25	int a, b;
    26	std::string c;
    27	sol::tie(a, b, c) = lua["f"](100, 200, "bark");
    28	SOL_ASSERT(a == 100);
    29	SOL_ASSERT(b == 200);
    30	SOL_ASSERT(c == "bark");
    31
    32	lua.script(R"(
    33		a, b, c = f(150, 250, "woofbark")
    34		assert(a == 150)
    35		assert(b == 250)
    36		assert(c == "woofbark")
    37	)");
    38
    39	return 0;
    40}

## C++ classes from C++

Everything that is not a:

> - primitive type: `bool`, `char/short/int/long/long long`, `float/double`

> - string type: `std::string`, `const char*`

> - function type: function pointers, `lua_CFunction`, `std::function`, sol::function/sol::protected_function, sol::coroutine, member variable, member function

> - designated sol type: sol::table, sol::thread, sol::error, sol::object

> - transparent argument type: sol::variadic_arg, sol::this_state, sol::this_environment

> - usertype<T> class: sol::usertype

Is set as a userdata + usertype.

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6struct Doge {
     7	int tailwag = 50;
     8
     9	Doge() {
    10	}
    11
    12	Doge(int wags) : tailwag(wags) {
    13	}
    14
    15	~Doge() {
    16		std::cout << "Dog at " << this
    17		          << " is being destroyed..." << std::endl;
    18	}
    19};
    20
    21int main(int, char*[]) {
    22	std::cout << "=== userdata ===" << std::endl;
    23
    24	sol::state lua;
    25
    26	Doge dog { 30 };
    27
    28	// fresh one put into Lua
    29	lua["dog"] = Doge {};
    30	// Copy into lua: destroyed by Lua VM during garbage
    31	// collection
    32	lua["dog_copy"] = dog;
    33	// OR: move semantics - will call move constructor if
    34	// present instead Again, owned by Lua
    35	lua["dog_move"] = std::move(dog);
    36	lua["dog_unique_ptr"] = std::make_unique<Doge>(25);
    37	lua["dog_shared_ptr"] = std::make_shared<Doge>(31);
    38
    39	// Identical to above
    40	Doge dog2 { 30 };
    41	lua.set("dog2", Doge {});
    42	lua.set("dog2_copy", dog2);
    43	lua.set("dog2_move", std::move(dog2));
    44	lua.set("dog2_unique_ptr",
    45	     std::unique_ptr<Doge>(new Doge(25)));
    46	lua.set("dog2_shared_ptr",
    47	     std::shared_ptr<Doge>(new Doge(31)));
    48
    49	// Note all of them can be retrieved the same way:
    50	Doge& lua_dog = lua["dog"];
    51	Doge& lua_dog_copy = lua["dog_copy"];
    52	Doge& lua_dog_move = lua["dog_move"];
    53	Doge& lua_dog_unique_ptr = lua["dog_unique_ptr"];
    54	Doge& lua_dog_shared_ptr = lua["dog_shared_ptr"];
    55	SOL_ASSERT(lua_dog.tailwag == 50);
    56	SOL_ASSERT(lua_dog_copy.tailwag == 30);
    57	SOL_ASSERT(lua_dog_move.tailwag == 30);
    58	SOL_ASSERT(lua_dog_unique_ptr.tailwag == 25);
    59	SOL_ASSERT(lua_dog_shared_ptr.tailwag == 31);
    60
    61
    62	std::cout << std::endl;
    63
    64	return 0;
    65}

`std::unique_ptr`/`std::shared_ptr`’s reference counts / deleters will be respected.

If you want it to refer to something, whose memory you know won’t die in C++ while it is used/exists in Lua, do the following:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6struct Doge {
     7	int tailwag = 50;
     8
     9	Doge() {
    10	}
    11
    12	Doge(int wags) : tailwag(wags) {
    13	}
    14
    15	~Doge() {
    16		std::cout << "Dog at " << this
    17		          << " is being destroyed..." << std::endl;
    18	}
    19};
    20
    21int main(int, char*[]) {
    22	std::cout << "=== userdata memory reference ==="
    23	          << std::endl;
    24
    25	sol::state lua;
    26	lua.open_libraries(sol::lib::base);
    27
    28	Doge dog {}; // Kept alive somehow
    29
    30	// Later...
    31	// The following stores a reference, and does not copy/move
    32	// lifetime is same as dog in C++
    33	// (access after it is destroyed is bad)
    34	lua["dog"] = &dog;
    35	// Same as above: respects std::reference_wrapper
    36	lua["dog"] = std::ref(dog);
    37	// These two are identical to above
    38	lua.set("dog", &dog);
    39	lua.set("dog", std::ref(dog));
    40
    41
    42	Doge& dog_ref = lua["dog"];     // References Lua memory
    43	Doge* dog_pointer = lua["dog"]; // References Lua memory
    44	Doge dog_copy = lua["dog"]; // Copies, will not affect lua

You can retrieve the userdata in the same way as everything else. Importantly, note that you can change the data of usertype variables and it will affect things in lua if you get a pointer or a reference:

     1	lua.new_usertype<Doge>("Doge", "tailwag", &Doge::tailwag);
     2
     3	dog_copy.tailwag = 525;
     4	// Still 50
     5	lua.script("assert(dog.tailwag == 50)");
     6
     7	dog_ref.tailwag = 100;
     8	// Now 100
     9	lua.script("assert(dog.tailwag == 100)");
    10
    11	dog_pointer->tailwag = 345;
    12	// Now 345
    13	lua.script("assert(dog.tailwag == 345)");
    14
    15	std::cout << std::endl;
    16
    17	return 0;
    18}

## C++ classes put into Lua

See this section here. Also check out a basic example, special functions example and initializers example! There are many more examples that show off the usage of classes in C++, so please peruse them all carefully as it can be as simple or as complex as your needs are.

## namespacing

You can emulate namespacing by having a table and giving it the namespace names you want before registering enums or usertypes:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4#include <iostream>
     5
     6int main() {
     7	std::cout << "=== namespacing ===" << std::endl;
     8
     9	struct my_class {
    10		int b = 24;
    11
    12		int f() const {
    13			return 24;
    14		}
    15
    16		void g() {
    17			++b;
    18		}
    19	};
    20
    21	sol::state lua;
    22	lua.open_libraries();
    23
    24	// "bark" namespacing in Lua
    25	// namespacing is just putting things in a table
    26	// forces creation if it does not exist
    27	auto bark = lua["bark"].get_or_create<sol::table>();
    28	// equivalent-ish:
    29	// sol::table bark = lua["bark"].force(); // forces table
    30	// creation equivalent, and more flexible: sol::table bark =
    31	// lua["bark"].get_or_create<sol::table>(sol::new_table());
    32	// equivalent, but less efficient/ugly:
    33	// sol::table bark = lua["bark"] = lua.get_or("bark",
    34	// lua.create_table());
    35	bark.new_usertype<my_class>("my_class",
    36	     "f",
    37	     &my_class::f,
    38	     "g",
    39	     &my_class::g); // the usual
    40
    41	// can add functions, as well (just like the global table)
    42	bark.set_function("print_my_class", [](my_class& self) {
    43		std::cout << "my_class { b: " << self.b << " }"
    44		          << std::endl;
    45	});
    46
    47	// this works
    48	lua.script("obj = bark.my_class.new()");
    49	lua.script("obj:g()");
    50
    51	// calling this function also works
    52	lua.script("bark.print_my_class(obj)");
    53	my_class& obj = lua["obj"];
    54	SOL_ASSERT(obj.b == 25);
    55
    56	std::cout << std::endl;
    57
    58	return 0;
    59}

This technique can be used to register namespace-like functions and classes. It can be as deep as you want. Just make a table and name it appropriately, in either Lua script or using the equivalent sol code. As long as the table FIRST exists (e.g., make it using a script or with one of sol’s methods or whatever you like), you can put anything you want specifically into that table using sol::table’s abstractions.

## there is a LOT more

Some more things you can do/read about:
  - the usertypes page lists the huge amount of features for functions

      - unique usertype traits allows you to specialize handle/RAII types from other libraries frameworks, like boost and Unreal, to work with sol. Allows custom smart pointers, custom handles and others

  - the containers page gives full information about handling everything about container-like usertypes

  - the functions page lists a myriad of features for functions

      - variadic arguments in functions with `sol::variadic_args`.

      - also comes with variadic_results for returning multiple differently-typed arguments

      - this_state to get the current `lua_State*`, alongside other transparent argument types

  - metatable manipulations allow a user to change how indexing, function calls, and other things work on a single type.

  - ownership semantics are described for how Lua deals with its own internal references and (raw) pointers.

  - stack manipulation to safely play with the stack. You can also define customization points for `stack::get`/`stack::check`/`stack::push` for your type.

  - make_reference/make_object convenience function to get the same benefits and conveniences as the low-level stack API but put into objects you can specify.

  - stack references to have zero-overhead sol abstractions while not copying to the Lua registry.

  - resolve overloads in case you have overloaded functions; a cleaner casting utility. You must use this to emulate default parameters.