# sol2 3.5.0 documentation

## this_state

### Search the Documentation

# this_state

transparent state argument for the current state

    struct this_state;

This class is a transparent type that is meant to be gotten in functions to get the current lua state a bound function or usertype method is being called from. It does not actually retrieve anything from lua nor does it increment the argument count, making it “invisible” to function calls in lua and calls through `std::function<...>` and sol::function on this type. It can be put in any position in the argument list of a function:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main() {
     6	sol::state lua;
     7
     8	lua.set_function(
     9	     "bark", [](sol::this_state s, int a, int b) {
    10		     lua_State* L = s; // current state
    11		     return a + b + lua_gettop(L);
    12	     });
    13
    14	lua.script(
    15	     "first = bark(2, 2)"); // only takes 2 arguments, NOT 3
    16
    17	// Can be at the end, too, or in the middle: doesn't matter
    18	lua.set_function(
    19	     "bark", [](int a, int b, sol::this_state s) {
    20		     lua_State* L = s; // current state
    21		     return a + b + lua_gettop(L);
    22	     });
    23
    24	lua.script("second = bark(2, 2)"); // only takes 2 arguments
    25	int first = lua["first"];
    26	SOL_ASSERT(first == 6);
    27	int second = lua["second"];
    28	SOL_ASSERT(second == 6);
    29
    30	return 0;
    31}