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

Function main

examples/source/c_array.cpp:14–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12};
13
14int main() {
15
16 std::cout << "=== c arrays (works with Visual C++ too) ==="
17 << std::endl;
18
19 sol::state lua;
20 lua.open_libraries(sol::lib::base);
21
22 lua.new_usertype<something>(
23 "something", "arr", sol::property([](something& s) {
24 return std::ref(s.arr);
25 }));
26 lua.script(R"(s = something.new()
27 print(s.arr[3])
28 s.arr[3] = 40
29 print(s.arr[3])
30 )");
31
32 something& s = lua["s"];
33 SOL_ASSERT(s.arr[0] == 5);
34 SOL_ASSERT(s.arr[1] == 6);
35 SOL_ASSERT(s.arr[2] == 40);
36 SOL_ASSERT(s.arr[3] == 8);
37
38 std::string string_array[] = {
39 "first string",
40 "second string",
41 "third string",
42 };
43 lua["str_arr"] = std::ref(string_array);
44 // or:
45 // lua["str_array"] = &string_array;
46 lua.script(R"(
47 print(str_arr[3])
48 str_arr[3] = str_arr[3] .. ': modified'
49 print(str_arr[3])
50 )");
51
52 SOL_ASSERT(string_array[2] == "third string: modified");
53
54 return 0;
55}

Callers

nothing calls this directly

Calls 3

propertyFunction · 0.85
open_librariesMethod · 0.80
scriptMethod · 0.80

Tested by

no test coverage detected