MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/table_proxy.cpp:5–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5int main() {
6
7 const auto& code = R"(
8 bark = {
9 woof = {
10 [2] = "arf!"
11 }
12 }
13 )";
14
15 sol::state lua;
16 lua.open_libraries(sol::lib::base);
17 lua.script(code);
18
19 // produces table_proxy, implicitly converts to std::string,
20 // quietly destroys table_proxy
21 std::string arf_string = lua["bark"]["woof"][2];
22
23 // lazy-evaluation of tables
24 auto x = lua["bark"];
25 auto y = x["woof"];
26 auto z = y[2];
27
28 // retrivies value inside of lua table above
29 std::string value = z;
30 SOL_ASSERT(value == "arf!");
31
32 // Can change the value later...
33 z = 20;
34
35 // Yay, lazy-evaluation!
36 int changed_value = z; // now it's 20!
37 SOL_ASSERT(changed_value == 20);
38 lua.script("assert(bark.woof[2] == 20)");
39
40 lua["a_new_value"] = 24;
41 lua["chase_tail"] = [](int chasing) {
42 int r = 2;
43 for (int i = 0; i < chasing; ++i) {
44 r *= r;
45 }
46 return r;
47 };
48
49 lua.script("assert(a_new_value == 24)");
50 lua.script("assert(chase_tail(2) == 16)");
51
52 return 0;
53}

Callers

nothing calls this directly

Calls 2

open_librariesMethod · 0.80
scriptMethod · 0.80

Tested by

no test coverage detected