Skip to content

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

@modcos

Description

@modcos

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.

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:

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:

[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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions