From 6371e002fdd4076320defb514771fd504a937f3b Mon Sep 17 00:00:00 2001 From: Petr Mikheev Date: Mon, 10 Oct 2022 22:40:33 +0200 Subject: [PATCH] [Lua] More understandable error message when trying to change stats on anything except `openmw.self` --- apps/openmw/mwlua/stats.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwlua/stats.cpp b/apps/openmw/mwlua/stats.cpp index 977d3b3eec..71e0aa8889 100644 --- a/apps/openmw/mwlua/stats.cpp +++ b/apps/openmw/mwlua/stats.cpp @@ -37,6 +37,12 @@ namespace using SelfObject = MWLua::LocalScripts::SelfObject; using StatObject = std::variant; + SelfObject* asSelfObject(const StatObject& obj) + { + if (!std::holds_alternative(obj)) + throw std::runtime_error("Changing stats allowed only in local scripts for 'openmw.self'."); + return std::get(obj); + } const MWLua::Object* getObject(const StatObject& obj) { @@ -123,7 +129,7 @@ namespace MWLua void setCurrent(const Context& context, const sol::object& value) const { - SelfObject* obj = std::get(mObject); + SelfObject* obj = asSelfObject(mObject); if (obj->mStatsCache.empty()) context.mLuaManager->addAction(std::make_unique(context.mLua, obj->id())); obj->mStatsCache[SelfObject::CachedStat{ &LevelStat::setValue, 0, "current" }] = value; @@ -183,7 +189,7 @@ namespace MWLua void cache(const Context& context, std::string_view prop, const sol::object& value) const { - SelfObject* obj = std::get(mObject); + SelfObject* obj = asSelfObject(mObject); if (obj->mStatsCache.empty()) context.mLuaManager->addAction(std::make_unique(context.mLua, obj->id())); obj->mStatsCache[SelfObject::CachedStat{ &DynamicStat::setValue, mIndex, prop }] = value; @@ -243,7 +249,7 @@ namespace MWLua void cache(const Context& context, std::string_view prop, const sol::object& value) const { - SelfObject* obj = std::get(mObject); + SelfObject* obj = asSelfObject(mObject); if (obj->mStatsCache.empty()) context.mLuaManager->addAction(std::make_unique(context.mLua, obj->id())); obj->mStatsCache[SelfObject::CachedStat{ &AttributeStat::setValue, mIndex, prop }] = value; @@ -330,7 +336,7 @@ namespace MWLua void cache(const Context& context, std::string_view prop, const sol::object& value) const { - SelfObject* obj = std::get(mObject); + SelfObject* obj = asSelfObject(mObject); if (obj->mStatsCache.empty()) context.mLuaManager->addAction(std::make_unique(context.mLua, obj->id())); obj->mStatsCache[SelfObject::CachedStat{ &SkillStat::setValue, mIndex, prop }] = value;