GetUserdataType: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Documented the function)
 
m (Typo at →‎Example)
Line 47: Line 47:
                 arg:getX(),
                 arg:getX(),
                 arg:getY(),
                 arg:getY(),
                 arg.getZ and argValue:getZ() or nil,
                 arg.getZ and arg:getZ() or nil,
                 arg.getW and argValue:getW() or nil,
                 arg.getW and arg:getW() or nil,
                 -- Extra field to distinguish from normal tables
                 -- Extra field to distinguish from normal tables
                 ["isVectorWorkaround"] = true
                 ["isVectorWorkaround"] = true

Revision as of 15:42, 30 July 2016

This function gets the type of a userdata value, which is not always a element in the element tree.

Syntax

string getUserdataType ( userdata value )

Required Arguments

Returns

Returns a string containing the specified userdata's type, or false plus an error message if the given value is not userdata.

Example

This example shows a function that can be used to work around the impossibility to transfer vectors as arguments when using triggerClientEvent and triggerServerEvent, by converting them into a table which can be used safely.

function safeArgsForTransfer(...)
    local args = { ... }
    for index, arg in ipairs(args) do
        if type(arg) == "userdata" and getUserdataType(arg):match("vector") then
            -- Transform every kind of vector userdata to a table which can be transfered safely
            args[index] =
            {
                arg:getX(),
                arg:getY(),
                arg.getZ and arg:getZ() or nil,
                arg.getW and arg:getW() or nil,
                -- Extra field to distinguish from normal tables
                ["isVectorWorkaround"] = true
            }
        end
    end
    return unpack(args)
end

See Also