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

Function main

examples/source/docs/preparing_environments.cpp:4–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include <sol/sol.hpp>
3
4int main(int, char*[]) {
5 sol::state lua;
6 lua.open_libraries();
7 sol::environment my_env(lua, sol::create);
8 // set value, and we need to explicitly allow for
9 // access to "print", since a new environment hides
10 // everything that's not defined inside of it
11 // NOTE: hiding also hides library functions (!!)
12 // BE WARNED
13 my_env["var"] = 50;
14 my_env["print"] = lua["print"];
15
16 sol::environment my_other_env(
17 lua, sol::create, lua.globals());
18 // do not need to explicitly allow access to "print",
19 // since we used the "Set a fallback" version
20 // of the sol::environment constructor
21 my_other_env["var"] = 443;
22
23 // output: 50
24 lua.script("print(var)", my_env);
25
26 // output: 443
27 lua.script("print(var)", my_other_env);
28
29 return 0;
30}

Callers

nothing calls this directly

Calls 3

open_librariesMethod · 0.80
globalsMethod · 0.80
scriptMethod · 0.80

Tested by

no test coverage detected