#ifndef MWLUA_OBJECTVARIANT_H #define MWLUA_OBJECTVARIANT_H #include #include "localscripts.hpp" #include "object.hpp" namespace MWLua { class ObjectVariant { public: explicit ObjectVariant(const sol::object& obj) { if (obj.is()) mVariant.emplace(obj.as()); else if (obj.is()) mVariant.emplace(obj.as()); else if (obj.is()) mVariant.emplace(obj.as()); else throw std::runtime_error("Expected game object, got: " + LuaUtil::toString(obj)); } bool isSelfObject() const { return std::holds_alternative(mVariant); } bool isLObject() const { return std::holds_alternative(mVariant); } bool isGObject() const { return std::holds_alternative(mVariant); } SelfObject* asSelfObject() const { if (!isSelfObject()) throw std::runtime_error("Allowed only in local scripts for 'openmw.self'."); return std::get(mVariant); } const MWWorld::Ptr& ptr() const { return std::visit( [](auto&& variant) -> const MWWorld::Ptr& { using T = std::decay_t; if constexpr (std::is_same_v) return variant->ptr(); else return variant.ptr(); }, mVariant); } Object object() const { return Object(ptr()); } private: std::variant mVariant; }; } // namespace MWLua #endif // MWLUA_OBJECTVARIANT_H