参考 › 函数参考 › Platform

Platform.threat.GetUnitThreatObservation

公共

调用方式

addon.Platform:GetUnitThreatObservation(unit)

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

作用

依次观测所选单位是否存在、是否与玩家敌对,然后观测玩家在该单位上的仇恨状态。已知不存在、非敌人以及成功读取到的 nil 仇恨状态返回 false,以保留“无仇恨”判断。包括零在内的有限状态数值保留其原有的比较含义。所需信息缺失、失败、为 nil、受限或格式错误时返回 nil 及原因。不会添加可攻击、死亡、战斗或载具的推断。

参数

名称类型作用
selfRMPlatformRotation Master 平台适配器(使用冒号调用时隐含传入)。
unitstring用于检查存在性,并作为玩家敌对和仇恨查询接收者的确切单位标识。

返回值

1number | boolean | nil有限的原生仇恨状态;已知不存在/非敌人/无仇恨时为 false,观测不可用时为 nil。
2string | nil仇恨观测为 nil 时的策略、查询验证或可用性原因。

版本

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

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

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

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

示例

local status = addon.Platform:GetUnitThreatObservation("target")
if status == nil then return nil end
-- false means no target, not an enemy, or no threat; 2 and 3 mean you are tanking
return status ~= false and status >= 2

源码

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

Platform.threat.GetUnitThreatObservation(self, unit)
    local policy=self:GetCombatDataPolicy()
    if policy~='probe' and policy~='restricted' then return nil,'unsupported-policy' end
    if not self:CanReadValue(unit) then return nil,'restricted-value' end
    if type(unit)~='string' or unit=='' then return nil,'invalid-unit' 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
    if type(UnitIsEnemy)~='function' then return nil,'missing-api' end
    local enemyOk,enemy=pcall(UnitIsEnemy,'player',unit)
    if not enemyOk then return nil,'api-error' end
    if not self:CanReadValue(enemy) then return nil,'restricted-value' end
    if enemy==nil then return nil,'unit-data-unavailable' end
    if type(enemy)~='boolean' then return nil,'invalid-api-result' end
    if not enemy then return false end
    if type(UnitThreatSituation)~='function' then return nil,'missing-api' end
    local threatOk,threat=pcall(UnitThreatSituation,'player',unit)
    if not threatOk then return nil,'api-error' end
    if not self:CanReadValue(threat) then return nil,'restricted-value' end
    if canaccessvalue and not canaccessvalue(threat) then return nil,'restricted-value' end
    if threat==nil then return false end
    if type(threat)~='number' or threat~=threat or threat==math.huge or threat==-math.huge then return nil,'invalid-api-result' end
    return threat

返回函数参考