Skip to content

Commit

Permalink
Merge branch 'lua_global_new' into 'master'
Browse files Browse the repository at this point in the history
Add global variable access to world.mwscript (#7597)

See merge request OpenMW/openmw!3491
  • Loading branch information
petrmikheev committed Oct 24, 2023
2 parents e1973f3 + db42a91 commit 31f5c88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
51 changes: 51 additions & 0 deletions apps/openmw/mwlua/mwscriptbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../mwbase/scriptmanager.hpp"
#include "../mwbase/world.hpp"
#include "../mwscript/globalscripts.hpp"
#include "../mwworld/esmstore.hpp"

#include "object.hpp"

Expand Down Expand Up @@ -43,6 +44,10 @@ namespace sol
struct is_automagical<MWLua::MWScriptVariables> : std::false_type
{
};
template <>
struct is_automagical<ESM::Global> : std::false_type
{
};
}

namespace MWLua
Expand Down Expand Up @@ -76,6 +81,7 @@ namespace MWLua
// api["getGlobalScripts"] = [](std::string_view recordId) -> list of scripts
// api["getLocalScripts"] = [](const GObject& obj) -> list of scripts

sol::state_view& lua = context.mLua->sol();
sol::usertype<MWScriptRef> mwscript = context.mLua->sol().new_usertype<MWScriptRef>("MWScript");
sol::usertype<MWScriptVariables> mwscriptVars
= context.mLua->sol().new_usertype<MWScriptVariables>("MWScriptVariables");
Expand Down Expand Up @@ -108,6 +114,51 @@ namespace MWLua
"No variable \"" + std::string(var) + "\" in mwscript " + s.mRef.mId.toDebugString());
};

using GlobalStore = MWWorld::Store<ESM::Global>;
sol::usertype<GlobalStore> globalStoreT = lua.new_usertype<GlobalStore>("ESM3_GlobalStore");
const GlobalStore* globalStore = &MWBase::Environment::get().getWorld()->getStore().get<ESM::Global>();
globalStoreT[sol::meta_function::to_string] = [](const GlobalStore& store) {
return "ESM3_GlobalStore{" + std::to_string(store.getSize()) + " globals}";
};
globalStoreT[sol::meta_function::length] = [](const GlobalStore& store) { return store.getSize(); };
globalStoreT[sol::meta_function::index]
= sol::overload([](const GlobalStore& store, std::string_view globalId) -> sol::optional<float> {
auto g = store.search(ESM::RefId::deserializeText(globalId));
if (g == nullptr)
return sol::nullopt;
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
if (varType == 's' || varType == 'l')
{
return static_cast<float>(MWBase::Environment::get().getWorld()->getGlobalInt(globalId));
}
else
{
return MWBase::Environment::get().getWorld()->getGlobalFloat(globalId);
}
});
globalStoreT[sol::meta_function::new_index]
= sol::overload([](const GlobalStore& store, std::string_view globalId, float val) {
auto g = store.search(ESM::RefId::deserializeText(globalId));
if (g == nullptr)
return;
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
if (varType == 's' || varType == 'l')
{
MWBase::Environment::get().getWorld()->setGlobalInt(globalId, static_cast<int>(val));
}
else
{
MWBase::Environment::get().getWorld()->setGlobalFloat(globalId, val);
}
});
globalStoreT[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>();
globalStoreT[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>();
api["getGlobalVariables"] = [globalStore](sol::optional<GObject> player) {
if (player.has_value() && player->ptr() != MWBase::Environment::get().getWorld()->getPlayerPtr())
throw std::runtime_error("First argument must either be a player or be missing");

return globalStore;
};
return LuaUtil::makeReadOnly(api);
}

Expand Down
6 changes: 6 additions & 0 deletions files/lua_api/openmw/world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
-- @param openmw.core#GameObject player (optional) Will be used in multiplayer mode to get the script if there is a separate instance for each player. Currently has no effect.
-- @return #MWScript, #nil

---
-- Returns mutable global variables. In multiplayer, these may be specific to the provided player.
-- @function [parent=#MWScriptFunctions] getGlobalVariables
-- @param openmw.core#GameObject player (optional) Will be used in multiplayer mode to get the globals if there is a separate instance for each player. Currently has no effect.
-- @return #list<#number>

---
-- Returns global mwscript with given recordId. Returns `nil` if the script doesn't exist or is not started.
-- Currently there can be only one instance of each mwscript, but in multiplayer it will be possible to have a separate instance per player.
Expand Down

0 comments on commit 31f5c88

Please sign in to comment.