Check

From Multi Theft Auto: Wiki

Jump to: navigation, search

This function checks the given arguments and calls the error-function if something is wrong. Finally it makes debugging easier since you'll get useful error-messages when calling a selfmade function with wrong or malformed arguments.

Syntax

void Check( string funcname, var types1, var arg1, string argname1, [ ... ] )

Required Arguments

  • funcname: Name of the function in which the Check-function is called.
  • types1: Type(s) that arg1 should/may be of. It's a string if only one type is possible, otherwise it can be a table with strings. Possible types are "nil", "boolean", "number", "string", "function", "table", "thread" and "userdata".
  • arg1: Value of the argument that should be checked.
  • argname1: Name of the argument whose value is arg1.

Optional Arguments

NOTE: When using optional arguments, you must supply all arguments before the one you wish to use. For more information on optional arguments, see Optional Arguments.

  • ...: You may pass as many triples as you want to.

Code

Click to collapse [-]
Server- and/or clientside Script

function Check(funcname, ...)
    local arg = {...}
 
    if (type(funcname) ~= "string") then
        error("Argument type mismatch at 'Check' ('funcname'). Expected 'string', got '"..type(funcname).."'.", 2)
    end
    if (#arg % 3 > 0) then
        error("Argument number mismatch at 'Check'. Expected #arg % 3 to be 0, but it is "..(#arg % 3)..".", 2)
    end
 
    for i=1, #arg-2, 3 do
        if (type(arg[i]) ~= "string" and type(arg[i]) ~= "table") then
            error("Argument type mismatch at 'Check' (arg #"..i.."). Expected 'string' or 'table', got '"..type(arg[i]).."'.", 2)
        elseif (type(arg[i+2]) ~= "string") then
            error("Argument type mismatch at 'Check' (arg #"..(i+2).."). Expected 'string', got '"..type(arg[i+2]).."'.", 2)
        end
 
        if (type(arg[i]) == "table") then
            local aType = type(arg[i+1])
            for _, pType in next, arg[i] do
                if (aType == pType) then
                    aType = nil
                    break
                end
            end
            if (aType) then
                error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..table.concat(arg[i], "' or '").."', got '"..aType.."'.", 3)
            end
        elseif (type(arg[i+1]) ~= arg[i]) then
            error("Argument type mismatch at '"..funcname.."' ('"..arg[i+2].."'). Expected '"..arg[i].."', got '"..type(arg[i+1]).."'.", 3)
        end
    end
end

Example

Click to collapse [-]
Server- and/or clientside Example

This example shows the Check-function's usage in case of a more or less useless division function.

-- define the function
function divide(dividend, divisor, round)
    -- check the passed arguments
    Check("divide", "number", dividend, "dividend", "number", divisor, "divisor", {"nil","boolean"}, round, "round")
 
    -- error if divisor is 0
    if (divisor == 0) then error("Someone tried to divide by 0. YOU MUST NOT DO THAT!!!") end
 
    -- calculate the result
    local quotient = dividend / divisor
    -- round the result if round is true
    if (round) then quotient = math.floor(quotient + .5)
 
    -- return the result
    return quotient
end

Author: NeonBlack

See Also

  • callClientFunction » This function allows you to call any clientside function from the server's side.
  • callServerFunction » This function allows you to call any server-side function from the client's side.
  • Check » This function checks if it's arguments are of the right types and calls the error-function if one isn't.
  • doForAllElements » This function can be used to execute a specified function for all elements of a specified type.
  • iterElements » Returns an iterator for your for loops saving time typing ipairs( getElementsByType( type ) ), instead you type: iterElements( type ).
  • findRotation » Takes two points and returns the direction from point A to point B.
  • FormatDate » Formats a date on the basis of a format string and returns it.
  • getAge » This function calculates the age of a birthday.
  • IfElse » Returns one of two values based on a boolean expression.
  • isLeapYear » Checks if the given year is a leap year.
  • math.round » Rounds a number whereas the number of decimals to keep and the method may be set.
  • setVehicleGravityPoint » This clientside function sets a vehicle's gravity in the direction of a 3 dimensional coordinate with the strength specified.
  • string.explode » This function splits a string at a given separator pattern and returns a table with the pieces.
  • table.copy » This function copies a whole table and all the tables in that table.
  • table.map » This function goes through a table and replaces every field with the return of the passed function, where the field's value is passed as first argument and optionally more arguments.
  • table.size » Finds the absolute size of a table.
  • var_dump »This function outputs information about one or more variables using outputConsole().
  • RGBToHex » This function returns a string representing the color in hexadecimal.
  • onVehicleWeaponFire » This code implements an event that is triggered when a player in a vehicle fires a vehicles weapon.
  • toHex » This function converts a decimal number to a hexadecimal number, as a fix to be used clientside.
  • getElementSpeed » This function allows you to get element speed in kph or mph units.
  • setElementSpeed » This function allows you to set moving element speed in kph or mph units.
  • centerWindow » This function center the window in any resolution.
Personal tools