参考 › 函数参考 › Platform

Platform.unit_level.GetUnitLevelObservation

公共

调用方式

addon.Platform:GetUnitLevelObservation(unit)

可直接通过 addon 使用,无需依赖项。

作用

在已知的客户端上观测可读的原始单位等级。可读且不存在的单位返回 exists=false;存在的单位要求 UnitLevel 为正的有限整数。未知的首领等级,以及缺失、失败、受限或格式错误的观测会返回 nil 及原因。这不使用有效等级或缩放后的等级。

参数

名称类型作用
selfRMPlatformRotation Master 平台适配器(使用冒号调用时隐含传入)。
unitstring要观测其原始等级的单位标识。

返回值

1table | nil全新的可读记录,包含 exists,存在时还包含正整数 level。
2string | nil等级观测为 nil 时的可用性或查询验证原因。

版本

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

  • 该版本不可用(原因见提示)
  • 仅在暴雪的弃用兼容层开启时
游戏 API经典旧世TBC 周年版经典进阶服永久服正式服
UnitExists经典旧世TBC 周年版经典进阶服永久服正式服
UnitLevel经典旧世TBC 周年版经典进阶服永久服正式服
canaccessvalue经典旧世TBC 周年版经典进阶服永久服正式服

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

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

示例

local target = addon.Platform:GetUnitLevelObservation("target")
local player = addon.Platform:GetUnitLevelObservation("player")
if not target or not player or not player.exists then return nil end
return target.exists and target.level < player.level

依赖项

源码

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

Platform.unit_level.GetUnitLevelObservation(self, unit)
    if not self:CanReadValue(unit) then return nil,'restricted-value' end
    if type(unit)~='string' or unit=='' then return nil,'invalid-unit' end
    if self:GetClientFamily()=='unknown' then return nil,'unsupported-client' end
    if type(UnitExists)~='function' then return nil,'missing-api' end
    local existsOk,exists=pcall(UnitExists,unit)
    if not existsOk then return nil,'api-error' end
    if not self:CanReadValue(exists) then return nil,'restricted-value' end
    if exists==nil then return nil,'unit-data-unavailable' end
    if type(exists)~='boolean' then return nil,'invalid-api-result' end
    if not exists then return {exists=false} end
    if type(UnitLevel)~='function' then return nil,'missing-api' end
    local levelOk,level=pcall(UnitLevel,unit)
    if not levelOk then return nil,'api-error' end
    if not self:CanReadValue(level) then return nil,'restricted-value' end
    if canaccessvalue and not canaccessvalue(level) then return nil,'restricted-value' end
    if level==nil then return nil,'level-data-unavailable' end
    if not finite(level) then return nil,'invalid-api-result' end
    if level<0 then return nil,'level-data-unavailable' end
    if level==0 or level%1~=0 then return nil,'invalid-api-result' end
    return {exists=true,level=level}

返回函数参考