参考 › 函数参考 › Platform

Platform.spells.copy

依赖项

调用方式

Platform.spells.copy(record, fields)

记录依赖项 Platform.spells.copy,然后按该名称调用。带命名空间的调用不会传入 self:请使用冒号调用,或自行传入该对象。

作用

Copies the expected fields of a native result table into a new table, checking each type. fields maps a key to its Lua type name ("icon" accepts a number or a string). Returns the copy with absent fields omitted, or nil with "restricted-value" when the record or a field is secret, or "invalid-api-result" when record is not a table, a field has the wrong type or a number is NaN or infinite.

参数

名称类型作用
recordanyNative result table to copy.
fieldstableMap of field name to expected type name.

返回值

1table | nilNew table holding only the expected, present fields; nil on failure.
2string | nilReason on failure.

版本

内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。

它不直接读取任何游戏 API。

秘密值(正式服和永久服)

在正式服和永久服上,游戏交给插件的是秘密值。此辅助函数会检查它读取的内容(addon.Platform:CanReadValue),遇到无法比较的秘密值时返回 nil 并附上原因(例如 restricted-value),而不是返回该秘密值。请将 nil 视为不可用,而不是假。

插件如何调用它

依赖项

源码

Platform/spells.lua,第 35 行。在游戏中为只读;“复制”会生成一份可编辑的副本。

Platform.spells.copy(record, fields)
    if not platform:CanReadValue(record) then return nil, "restricted-value" end
    if type(record) ~= "table" then return nil, "invalid-api-result" end
    local result = {}
    for key, kind in pairs(fields) do
        local value = record[key]
        if not platform:CanReadValue(value) then return nil, "restricted-value" end
        if value ~= nil then
            if type(value) ~= kind and not (kind == "icon" and (type(value) == "number" or type(value) == "string")) then
                return nil, "invalid-api-result"
            end
            if type(value) == "number" and (value ~= value or value == math.huge or value == -math.huge) then
                return nil, "invalid-api-result"
            end
            result[key] = value
        end
    end
    return result

返回函数参考