调用方式
addon.Platform:GetUnitMetadataObservation(unit, kind)
可直接通过 addon 使用,无需依赖项。
作用
在检查 UnitExists 后观测一个所使用的原生单位元数据字段:class 使用 UnitClass 的第二个返回值,creature 使用本地化的 UnitCreatureType 名称,classification 使用 UnitClassification。可读且不存在的单位返回 false,不查询元数据。未使用的元组字段不影响可用性;所需的值缺失、失败、为 nil、受限或格式错误时返回 nil 及原因。不会添加玩家职业、GUID、枚举或翻译推断。
参数
| 名称 | 类型 | 作用 |
|---|---|---|
self | RMPlatform | Rotation Master 平台适配器(使用冒号调用时隐含传入)。 |
unit | string | 用于检查存在性并传给所选元数据 API 的确切单位标识。 |
kind | string | class、creature 或 classification 之一;不支持其他元数据种类。 |
返回值
| 1 | string | boolean | nil | 可读的所使用元数据字符串;已知单位不存在时为 false,所需数据不可用时为 nil。 |
|---|---|---|
| 2 | string | nil | 元数据观测为 nil 时的策略、查询验证或可用性原因。 |
版本
内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。
- 该版本不可用(原因见提示)
- 仅在暴雪的弃用兼容层开启时
秘密值(正式服和永久服)
在正式服和永久服上,游戏交给插件的是秘密值。此辅助函数会检查它读取的内容(addon.Platform:CanReadValue),遇到无法比较的秘密值时返回 nil 并附上原因(例如 restricted-value),而不是返回该秘密值。请将 nil 视为不可用,而不是假。
示例
local classification = addon.Platform:GetUnitMetadataObservation("target", "classification")
if classification == nil then return nil end
return classification == "worldboss" or classification == "elite"源码
Platform/unit_metadata.lua,第 21 行。在游戏中为只读;“复制”会生成一份可编辑的副本。
Platform.unit_metadata.GetUnitMetadataObservation(self, unit, kind)
local policy=self:GetCombatDataPolicy()
if policy~='probe' and policy~='restricted' then return nil,'unsupported-policy' end
if not self:CanReadValue(unit) or not self:CanReadValue(kind) then return nil,'restricted-value' end
if type(unit)~='string' or unit=='' then return nil,'invalid-unit' end
if kind~='class' and kind~='creature' and kind~='classification' then return nil,'invalid-unit-metadata-query' 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 false end
local api
if kind=='class' then api=UnitClass
elseif kind=='creature' then api=UnitCreatureType
else api=UnitClassification end
if type(api)~='function' then return nil,'missing-api' end
local ok,observed
if kind=='class' then ok,observed=pcall(function() return select(2,api(unit)) end)
else ok,observed=pcall(api,unit) end
if not ok then return nil,'api-error' end
if not self:CanReadValue(observed) then return nil,'restricted-value' end
if observed==nil then return nil,'unit-metadata-unavailable' end
if type(observed)~='string' or observed=='' then return nil,'invalid-api-result' end
return observed




