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

Function main

examples/source/variables.cpp:6–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6int main() {
7 std::cout << "=== variables ===" << std::endl;
8
9 sol::state lua;
10
11 // need the base library for assertions
12 lua.open_libraries(sol::lib::base);
13
14 // basic setting of a variable
15 // through multiple ways
16 lua["x"] = 10;
17 lua.set("y", "hello");
18
19 // assert values are as given
20 lua.script("assert(x == 10)");
21 lua.script("assert(y == 'hello')");
22
23
24 // basic retrieval of a variable
25 // through multiple ways
26 int x = lua["x"];
27 auto y = lua.get<std::string>("y");
28
29 int x2;
30 std::string y2;
31 std::tie(x2, y2) = lua.get<int, std::string>("x", "y");
32
33 // show the values
34 std::cout << x << std::endl;
35 std::cout << y << std::endl;
36 std::cout << x2 << std::endl;
37 std::cout << y2 << std::endl;
38 std::cout << std::endl;
39}

Callers

nothing calls this directly

Calls 4

tieFunction · 0.85
open_librariesMethod · 0.80
scriptMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected