mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-23 16:11:33 +00:00
Add function to replace duplicated code
This commit is contained in:
parent
97203f2706
commit
5f9acbd0f0
1 changed files with 19 additions and 38 deletions
|
@ -53,6 +53,20 @@ namespace sol
|
||||||
namespace MWLua
|
namespace MWLua
|
||||||
{
|
{
|
||||||
|
|
||||||
|
auto getGlobalVariableValue(const std::string_view globalId) -> float
|
||||||
|
{
|
||||||
|
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
|
||||||
|
if (varType == 'f')
|
||||||
|
{
|
||||||
|
return MWBase::Environment::get().getWorld()->getGlobalFloat(globalId);
|
||||||
|
}
|
||||||
|
else if (varType == 's' || varType == 'l')
|
||||||
|
{
|
||||||
|
return static_cast<float>(MWBase::Environment::get().getWorld()->getGlobalInt(globalId));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
sol::table initMWScriptBindings(const Context& context)
|
sol::table initMWScriptBindings(const Context& context)
|
||||||
{
|
{
|
||||||
sol::table api(context.mLua->sol(), sol::create);
|
sol::table api(context.mLua->sol(), sol::create);
|
||||||
|
@ -130,15 +144,7 @@ namespace MWLua
|
||||||
auto g = store.search(ESM::RefId::deserializeText(globalId));
|
auto g = store.search(ESM::RefId::deserializeText(globalId));
|
||||||
if (g == nullptr)
|
if (g == nullptr)
|
||||||
return sol::nullopt;
|
return sol::nullopt;
|
||||||
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
|
return getGlobalVariableValue(globalId);
|
||||||
if (varType == 's' || varType == 'l')
|
|
||||||
{
|
|
||||||
return static_cast<float>(MWBase::Environment::get().getWorld()->getGlobalInt(globalId));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return MWBase::Environment::get().getWorld()->getGlobalFloat(globalId);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[](const GlobalStore& store, int index) -> sol::optional<float> {
|
[](const GlobalStore& store, int index) -> sol::optional<float> {
|
||||||
if (index < 1 || index >= store.getSize())
|
if (index < 1 || index >= store.getSize())
|
||||||
|
@ -147,15 +153,7 @@ namespace MWLua
|
||||||
if (g == nullptr)
|
if (g == nullptr)
|
||||||
return sol::nullopt;
|
return sol::nullopt;
|
||||||
std::string globalId = g->mId.serializeText();
|
std::string globalId = g->mId.serializeText();
|
||||||
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
|
return getGlobalVariableValue(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]
|
globalStoreT[sol::meta_function::new_index]
|
||||||
|
@ -163,15 +161,7 @@ namespace MWLua
|
||||||
auto g = store.search(ESM::RefId::deserializeText(globalId));
|
auto g = store.search(ESM::RefId::deserializeText(globalId));
|
||||||
if (g == nullptr)
|
if (g == nullptr)
|
||||||
throw std::runtime_error("No variable \"" + std::string(globalId) + "\" in GlobalStore");
|
throw std::runtime_error("No variable \"" + std::string(globalId) + "\" in GlobalStore");
|
||||||
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
|
return getGlobalVariableValue(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] = [](const GlobalStore& store) {
|
globalStoreT[sol::meta_function::pairs] = [](const GlobalStore& store) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
@ -185,16 +175,7 @@ namespace MWLua
|
||||||
return sol::nullopt;
|
return sol::nullopt;
|
||||||
|
|
||||||
std::string globalId = global->mId.serializeText();
|
std::string globalId = global->mId.serializeText();
|
||||||
char varType = MWBase::Environment::get().getWorld()->getGlobalVariableType(globalId);
|
float value = getGlobalVariableValue(globalId);
|
||||||
float value;
|
|
||||||
if (varType == 's' || varType == 'l')
|
|
||||||
{
|
|
||||||
value = static_cast<float>(MWBase::Environment::get().getWorld()->getGlobalInt(globalId));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value = MWBase::Environment::get().getWorld()->getGlobalFloat(globalId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_tuple(globalId, value);
|
return std::make_tuple(globalId, value);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue