From 7f0f2373ea8defed0f5c876640179e9d7b3d16ae Mon Sep 17 00:00:00 2001 From: Mads Buvik Sandvei Date: Mon, 12 Aug 2024 21:24:50 +0200 Subject: [PATCH] Use sol::optional for optional table instead of function overloading. --- apps/openmw/mwlua/animationbindings.cpp | 70 +++++++++++-------- files/data/scripts/omw/worldeventhandlers.lua | 8 +-- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/apps/openmw/mwlua/animationbindings.cpp b/apps/openmw/mwlua/animationbindings.cpp index 9d2fb38660..c2cdccad57 100644 --- a/apps/openmw/mwlua/animationbindings.cpp +++ b/apps/openmw/mwlua/animationbindings.cpp @@ -256,26 +256,31 @@ namespace MWLua return anim->getNode(bonename) != nullptr; }; - api["addVfx"] = sol::overload( - [context](const sol::object& object, const std::string& model) { + api["addVfx"] = [context]( + const sol::object& object, const std::string& model, sol::optional options) { + if (options) + { + context.mLuaManager->addAction( + [object = ObjectVariant(object), model = model, + effectId = options->get_or("vfxId", ""), loop = options->get_or("loop", false), + boneName = options->get_or("boneName", ""), + particleTexture = options->get_or("particleTextureOverride", "")] { + MWRender::Animation* anim = getMutableAnimationOrThrow(ObjectVariant(object)); + + anim->addEffect(model, effectId, loop, boneName, particleTexture); + }, + "addVfxAction"); + } + else + { context.mLuaManager->addAction( [object = ObjectVariant(object), model = model] { MWRender::Animation* anim = getMutableAnimationOrThrow(object); anim->addEffect(model, ""); }, "addVfxAction"); - }, - [context](const sol::object& object, const std::string& model, const sol::table& options) { - context.mLuaManager->addAction( - [object = ObjectVariant(object), model = model, effectId = options.get_or("vfxId", ""), - loop = options.get_or("loop", false), boneName = options.get_or("boneName", ""), - particleTexture = options.get_or("particleTextureOverride", "")] { - MWRender::Animation* anim = getMutableAnimationOrThrow(ObjectVariant(object)); - - anim->addEffect(model, effectId, loop, boneName, particleTexture); - }, - "addVfxAction"); - }); + } + }; api["removeVfx"] = [context](const sol::object& object, std::string_view effectId) { context.mLuaManager->addAction( @@ -304,23 +309,26 @@ namespace MWLua sol::table api(lua, sol::create); auto world = MWBase::Environment::get().getWorld(); - api["spawn"] = sol::overload( - [world, context](const std::string model, const osg::Vec3f& worldPos) { - context.mLuaManager->addAction( - [world, model = model, worldPos]() { world->spawnEffect(model, "", worldPos); }, - "openmw.vfx.spawn"); - }, - [world, context](const std::string& model, const osg::Vec3f& worldPos, const sol::table& options) { - bool magicVfx = options.get_or("mwMagicVfx", true); - std::string texture = options.get_or("particleTextureOverride", ""); - float scale = options.get_or("scale", 1.f); - - context.mLuaManager->addAction( - [world, model = model, texture = std::move(texture), worldPos, scale, magicVfx]() { - world->spawnEffect(model, texture, worldPos, scale, magicVfx); - }, - "openmw.vfx.spawn"); - }); + api["spawn"] + = [world, context](const std::string model, const osg::Vec3f& worldPos, sol::optional options) { + if (options) + { + bool magicVfx = options->get_or("mwMagicVfx", true); + std::string texture = options->get_or("particleTextureOverride", ""); + float scale = options->get_or("scale", 1.f); + context.mLuaManager->addAction( + [world, model = model, texture = std::move(texture), worldPos, scale, magicVfx]() { + world->spawnEffect(model, texture, worldPos, scale, magicVfx); + }, + "openmw.vfx.spawn"); + } + else + { + context.mLuaManager->addAction( + [world, model = model, worldPos]() { world->spawnEffect(model, "", worldPos); }, + "openmw.vfx.spawn"); + } + }; return api; } diff --git a/files/data/scripts/omw/worldeventhandlers.lua b/files/data/scripts/omw/worldeventhandlers.lua index 1650f9fb5f..4e7f96fcb7 100644 --- a/files/data/scripts/omw/worldeventhandlers.lua +++ b/files/data/scripts/omw/worldeventhandlers.lua @@ -6,12 +6,6 @@ return { Unpause = function(tag) world.unpause(tag) end, SetGameTimeScale = function(scale) world.setGameTimeScale(scale) end, SetSimulationTimeScale = function(scale) world.setSimulationTimeScale(scale) end, - SpawnVfx = function(data) - if data.options then - world.vfx.spawn(data.model, data.position, data.options) - else - world.vfx.spawn(data.model, data.position) - end - end, + SpawnVfx = function(data) world.vfx.spawn(data.model, data.position, data.options) end, }, }