调用方式
addon:GetConditionUnitLevel(unit, cache)
可直接通过 addon 使用,无需依赖项。
作用
为条件评估解析可读的原始单位等级。已知不存在时返回 false;观测不可用时返回 nil 及原因。在所提供的评估轮次内缓存观测。只有有效的玩家等级会在轮次之间保留,直到技能、配置文件或等级刷新。
参数
| 名称 | 类型 | 作用 |
|---|---|---|
self | RMAddon | Rotation Master 插件实例(使用冒号调用时隐含传入)。 |
unit | string | 要解析其原始等级的单位标识。 |
cache | table | nil | 共享的评估缓存;为 nil 时直接观测,不使用每轮次的缓存。 |
返回值
| 1 | number | boolean | nil | 正整数等级;已知单位不存在时为 false,不可用时为 nil。 |
|---|---|---|
| 2 | string | nil | 等级为 nil 时的可用性或查询验证原因。 |
版本
内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。
它不直接读取任何游戏 API。
秘密值(正式服和永久服)
在正式服和永久服上,游戏交给插件的是秘密值。此辅助函数会检查它读取的内容(addon.Platform:CanReadValue),遇到无法比较的秘密值时返回 nil 并附上原因(例如 restricted-value),而不是返回该秘密值。请将 nil 视为不可用,而不是假。
插件如何调用它
源码
Core/level_conditions.lua,第 21 行。在游戏中为只读;“复制”会生成一份可编辑的副本。
Core.level_conditions.GetConditionUnitLevel(self, unit, cache)
if not self.Platform:CanReadValue(unit) then return nil,'restricted-value' end
if type(unit)~='string' or unit=='' then return nil,'invalid-unit' end
if self.Platform:GetClientFamily()=='unknown' then return nil,'unsupported-client' end
local longterm=self.longtermCache
if unit=='player' and type(longterm)=='table' then
local level=longterm.conditionPlayerLevel
if not self.Platform:CanReadValue(level) then return nil,'restricted-value' end
if level~=nil then
if type(level)~='number' or level~=level or level<=0 or level==math.huge or level%1~=0 then return nil,'invalid-api-result' end
return level
end
end
local record,reason=addon.Functions.cache.getCached(cache,self.Platform.GetUnitLevelObservation,self.Platform,unit)
if not record then return nil,reason end
if not record.exists then return false end
if unit=='player' and type(longterm)=='table' then longterm.conditionPlayerLevel=record.level end
return record.level