#1213·xLua

Prevent XLua from returning multiple nil values when C# method should return nothing.

Author: modcosCreated Feb 21, 2026Updated Feb 21, 2026

I am implementing a class with static functions, and in one of them I need to return multiple values. However, in some cases the method should return nothing.

csharp
public static void foo(out float? a, out float? b, out float? c)
{
    if (_test)
    {
        a = b = c = 0;
    }
    else
    {
        a = b = c = null;
    }
}

In this case, Lua still receives three nil values. I would like the function to return no values at all when the condition is not met.

What I actually need is behavior similar to:

csharp
public static int foo(RealStatePtr L)
{
    if (!_test)
        return 0;

    LuaAPI.lua_pushnumber(L, a);
    LuaAPI.lua_pushnumber(L, b);
    LuaAPI.lua_pushnumber(L, c);
    return 3;
}

Is it possible to override or customize the method generated in the wrapper by the code generator?

For example, to make the generator produce something like:

csharp
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int _m_foo_xlua_st_(RealStatePtr L)
{
    try 
    {
        return foo(L);
    }
    catch (System.Exception gen_e) 
    {
        return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
    }

    return LuaAPI.luaL_error(L, "invalid arguments to foo");
}

I would like the wrapper to directly delegate to my int foo(RealStatePtr L) implementation so I can explicitly control the number of returned values. How do I achieve this?