aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sol2/examples/source/basic.cpp
diff options
context:
space:
mode:
authorAndy <drumsetmonkey@gmail.com>2019-08-29 13:07:45 -0400
committerAndy <drumsetmonkey@gmail.com>2019-08-29 13:07:45 -0400
commit4ac4b280abf2ffa28caa5a532353115a3033444f (patch)
tree2a13d658bb454360b2faf401244bb0321d3460d4 /lib/sol2/examples/source/basic.cpp
parente9758416b18b27a65337c28d9641afc0ee89b34b (diff)
parent7a46fa2dd3dad3f038bf8e7339bc67abca428ae6 (diff)
Started creating scripting library/namespace and added sol2 for interfacing
Diffstat (limited to 'lib/sol2/examples/source/basic.cpp')
-rw-r--r--lib/sol2/examples/source/basic.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/sol2/examples/source/basic.cpp b/lib/sol2/examples/source/basic.cpp
new file mode 100644
index 0000000..aefb1e3
--- /dev/null
+++ b/lib/sol2/examples/source/basic.cpp
@@ -0,0 +1,64 @@
+#define SOL_ALL_SAFETIES_ON 1
+#include <sol/sol.hpp>
+
+#include <iostream>
+#include "assert.hpp"
+
+int main() {
+ std::cout << "=== basic ===" << std::endl;
+ // create an empty lua state
+ sol::state lua;
+
+ // by default, libraries are not opened
+ // you can open libraries by using open_libraries
+ // the libraries reside in the sol::lib enum class
+ lua.open_libraries(sol::lib::base);
+ // you can open all libraries by passing no arguments
+ //lua.open_libraries();
+
+ // call lua code directly
+ lua.script("print('hello world')");
+
+ // call lua code, and check to make sure it has loaded and run properly:
+ auto handler = &sol::script_default_on_error;
+ lua.script("print('hello again, world')", handler);
+
+ // Use a custom error handler if you need it
+ // This gets called when the result is bad
+ auto simple_handler = [](lua_State*, sol::protected_function_result result) {
+ // You can just pass it through to let the call-site handle it
+ return result;
+ };
+ // the above lambda is identical to sol::simple_on_error, but it's
+ // shown here to show you can write whatever you like
+
+ //
+ {
+ auto result = lua.script("print('hello hello again, world') \n return 24", simple_handler);
+ if (result.valid()) {
+ std::cout << "the third script worked, and a double-hello statement should appear above this one!" << std::endl;
+ int value = result;
+ c_assert(value == 24);
+ }
+ else {
+ std::cout << "the third script failed, check the result type for more information!" << std::endl;
+ }
+ }
+
+ {
+ auto result = lua.script("does.not.exist", simple_handler);
+ if (result.valid()) {
+ std::cout << "the fourth script worked, which it wasn't supposed to! Panic!" << std::endl;
+ int value = result;
+ c_assert(value == 24);
+ }
+ else {
+ sol::error err = result;
+ std::cout << "the fourth script failed, which was intentional!\t\nError: " << err.what() << std::endl;
+ }
+ }
+
+ std::cout << std::endl;
+
+ return 0;
+}