Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sol.hpp>
- #include <string>
- #include <iostream>
- class Entity {
- public:
- Entity() : name("John") {}
- const std::string& getName() const { return name; }
- private:
- std::string name;
- };
- int main()
- {
- sol::state lua;
- lua.open_libraries();
- lua.new_usertype<Entity>("Entity",
- "getName", &Entity::getName,
- "extraFunc", [](Entity& e) { std::cout << "EXTRA!" << e.getName() << " is working\n"; });
- lua.do_file("script.lua");
- sol::function createHandle = lua["createHandle"];
- sol::function test = lua["test"];
- sol::function test2 = lua["test2"];
- sol::function onEntityDeleted = lua["onEntityDeleted"];
- sol::object handle(lua);
- {
- Entity e;
- handle = createHandle(e);
- test(handle); // everything ok here
- test2(e);
- // entity will be destroyed after leaving the scope, so we need to notify handle about that
- onEntityDeleted(handle);
- }
- try {
- test(handle); // this will throw error
- }
- catch (sol::error& e) {
- std::cout << e.what() << std::endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment