Closed diath closed 3 months ago
This would work, so take sol::optional<std::shared_ptr<Widget>>
as argument or make a wrapper for the binding if you cant change setActiveWidget.
#include <memory>
#include <limits>
#include <sol/sol.hpp>
struct Widget
{
};
std::shared_ptr<Widget> active;
void setActiveWidget(sol::optional<std::shared_ptr<Widget>> w)
{
if (w)
active = *w;
else
active.reset();
}
int main(int, char **)
{
sol::state state;
state.new_usertype<Widget>("Widget",
sol::factories([]() {
return std::make_shared<Widget>();
})
);
state["setActiveWidget"] = &setActiveWidget;
state.script("setActiveWidget(Widget.new())");
state.script("setActiveWidget(nil)");
return 0;
}
I can change the API, so that works, just a bit unfortunate that I need to include the sol header in that class now which adds a bit of compile time overhead. Thanks.
You can also just do this instead to avoid the compile overhead but have a wrapper.
state["setActiveWidget"] = [](sol::optional<std::shared_ptr<Widget>> w){ return setActiveWidget(w ? *w : std::shared_ptr<Widget>()); };
Hi, I have a C++ function that accepts
std::shared_ptr<Type>
as an argument, I want to also be able to call the function withnil
from Lua, to reset the said pointer, however it's not possible because sol expects actual userdata type on the stack, is there a way to work around this without adding a separate function for resetting the pointer?Example: https://godbolt.org/z/s964fx36e