Core Lua

pcall()

A standard Lua protected call that reports whether a function succeeded. On failure, the following result contains the error object.

pcall() lifecycle and behavior diagram
API concept illustration · PixelDevHub

Function syntax

local ok, result = pcall(function, ...)

Short summary

A standard Lua protected call that reports whether a function succeeded. On failure, the following result contains the error object.

Safe code example

local function readConfiguration(load)
    local ok, result = pcall(load)
    if not ok then
        return nil, "Configuration could not be loaded"
    end
    if type(result) ~= "table" then
        return nil, "Configuration must be a table"
    end
    return result
end

Practical game development scenario

Wrap an operation that may fail, then validate the returned data before using it. Give callers a useful recovery path rather than silently discarding every error.

Compatibility and safety notes

Protected calls do not make untrusted code safe, provide a timeout, or roll back side effects. Consult the standard Lua manual for the exact semantics in your runtime.

These examples demonstrate defensive patterns. They are not sandboxing mechanisms or substitutes for permission checks, server-side validation, and resource limits.

Read the official Lua 5.4 reference manual for language semantics. Engine methods require separate host documentation.

← Back to the dictionary

Read the result as a status-dependent contract

The first result tells you whether the protected operation returned normally. Interpret the following values according to that status and according to the wrapped function's own contract. A successful call can still return nil as a legitimate result, so nil alone does not establish an execution error. Likewise, a failure result should not be treated as a normal configuration table. Name the variables so the distinction remains visible and handle the status before accessing result fields. This small discipline prevents a protective wrapper from merely moving the original failure into the next line, where the caller assumes the returned value has a shape it was never guaranteed to have.

Protect an operation rather than hiding every bug

A broad protected call around an entire update loop can make a program appear resilient while silently skipping important work. Prefer a boundary whose possible failure and recovery action you can explain. For example, loading an optional configuration can fail without replacing the previous working state. A programmer error inside a pure calculation may deserve a visible diagnostic during development instead of being converted into a generic false result. Keep enough information to investigate unexpected failures without exposing sensitive details to end users. Protected execution is a control-flow tool; it does not tell you which errors are acceptable, how state should be repaired, or whether the application may safely continue.

Test successful nil results and unusual error objects

Add one test in which the wrapped function returns normally with nil, another in which it returns several ordinary values, and a third in which it raises an error. Verify that your caller distinguishes those cases instead of treating every empty-looking result as the same failure. Also make the error-reporting path tolerant of values that are not already useful display strings. Keep the test free from external services so the contract can be examined without network timing or account state. Once that behavior is clear, add the host-specific integration separately. A small reference example becomes much more dependable when its callers are tested against both the normal return contract and the protected-call status.

Find your next tool or idea

Search tools, guides, and Lua resources.

↑ ↓ Browse resultsESC to close

Choose a tool