参考 › 函数参考 › Platform

Platform.unit_attackability.GetUnitAttackabilityObservation

公共

调用方式

addon.Platform:GetUnitAttackabilityObservation(unit)

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

作用

使用确切的 UnitExists、UnitCanAttack 和 UnitIsDead 判断,观测玩家是否可以攻击一个未死亡的所选单位。已知不存在或可读的不可攻击结果会返回 false,不查询死亡状态。所需观测缺失、失败、受限或格式错误时返回 nil 及原因。这不检测灵魂状态、距离、敌对状态或技能可用性,也不重定向单位所有权。

参数

名称类型作用
selfRMPlatformRotation Master 平台适配器(使用冒号调用时隐含传入)。
unitstring确切的所选单位标识;攻击者始终为 player。

返回值

1boolean | nil可读、可攻击且未死亡的单位为 true;结论为否定时为 false;所需观测不可用时为 nil。
2string | nil可攻击性为 nil 时的策略、查询验证或可用性原因。

版本

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

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

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

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

示例

local attackable = addon.Platform:GetUnitAttackabilityObservation("target")
if attackable == nil then return nil end
return attackable

依赖项

源码

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

Platform.unit_attackability.GetUnitAttackabilityObservation(self, unit)
    local policy=readClientPolicy(self)
    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(UnitCanAttack)~='function' then return nil,'missing-api' end
    local attackOk,attack=pcall(UnitCanAttack,'player',unit)
    if not attackOk then return nil,'api-error' end
    if not self:CanReadValue(attack) then return nil,'restricted-value' end
    if attack==nil then return nil,'attackability-data-unavailable' end
    if type(attack)~='boolean' then return nil,'invalid-api-result' end
    if not attack then return false end
    if type(UnitIsDead)~='function' then return nil,'missing-api' end
    local deadOk,dead=pcall(UnitIsDead,unit)
    if not deadOk then return nil,'api-error' end
    if not self:CanReadValue(dead) then return nil,'restricted-value' end
    if dead==nil then return nil,'death-data-unavailable' end
    if type(dead)~='boolean' then return nil,'invalid-api-result' end
    return not dead

返回函数参考