[feature] Being Able To Export Metatables

I created this post in addition to this bug report.

Current State

So as shown in the bug report that i made a while back. I’ts not possible to export a MetaTable, according to d-bubble this is because MetaTables don’t exist in JavaScript and C#, just like Classes that don’t exist in Lua.

Feature Request

So this topic was created to ask if it would be possible to do a language check, meaning that if you export a MetaTable in a Lua file and that call this export in the Lua file that the MetaTable remains and otherwise it becomes an Array or Object Notation.

1 Like

I have a small workaround for this. If you load the class as a script you can set the data sent over the export and add the meta tables back on the data after it is passed.

2 Likes
local function refresh(self)
    local object = {}
    for k, v in pairs(self) do
        object[k] = v
    end
    for k, v in pairs(Events) do
        if type(v) == "function" and not object[k] then
            object[k] = function(...)
                return v(self, ...)
            end
        end
    end
    setmetatable(object, Events)
    return object
end

this worked for me, after which I was able to use it normally, but I don’t think it’s a good approach

I realized that when it comes to transferring this information, it is lost only as a table, but if I use getmetatable, it goes only with the methods, using the above approach worked, but it doesn’t seem good to me

1 Like

analyzing it a little further, I realized that when using the code above, this was happening:

__cfx_functionReference: rvn:491:28

which leads me to believe that it is referencing the methods and not creating copies, which in my mind leads me to believe that there would be no problems, I am analyzing the code available on github to see if I find anything else

the method below helped me, it’s the fivem standard:

function Citizen.GetFunctionReference(func)
    if type(func) == 'function' then
        return MakeFunctionReference(func)
    elseif type(func) == 'table' and rawget(func, '__cfx_functionReference') then
        return MakeFunctionReference(function(...)
            return func(...)
        end)
    end

    return nil
end