参考 › 函数参考 › Platform

Platform.unit_flags.GetUnitFlagObservation

公共

调用方式

addon.Platform:GetUnitFlagObservation(unit, kind)

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

作用

观测一个当前的单位标记:combat 使用 UnitAffectingCombat,enemy 使用以 player 为第一个单位的 UnitIsEnemy,pvp 使用 UnitIsPVP。可读且不存在的单位返回 false,不查询该标记。所需观测缺失、失败、受限或格式错误时返回 nil 及原因。这会保留确切的所选单位,不推断友好、可攻击或其他单位状态。

参数

名称类型作用
selfRMPlatformRotation Master 平台适配器(使用冒号调用时隐含传入)。
unitstring要观测其所选标记的确切单位标识。
kindstringcombat、enemy 或 pvp 之一;不支持其他标记种类。

返回值

1boolean | nil可读的所选标记;已知单位不存在时为 false,所需观测不可用时为 nil。
2string | nil单位标记为 nil 时的策略、查询验证或可用性原因。

版本

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

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

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

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

示例

local inCombat = addon.Platform:GetUnitFlagObservation("target", "combat")
local enemy = addon.Platform:GetUnitFlagObservation("target", "enemy")
if inCombat == nil or enemy == nil then return nil end
return enemy and not inCombat

依赖项

源码

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

Platform.unit_flags.GetUnitFlagObservation(self, unit, kind)
    local policy=readClientPolicy(self)
    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~='combat' and kind~='enemy' and kind~='pvp' then return nil,'invalid-unit-flag-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=='combat' then api=UnitAffectingCombat
    elseif kind=='enemy' then api=UnitIsEnemy
    else api=UnitIsPVP end
    if type(api)~='function' then return nil,'missing-api' end
    local ok,result
    if kind=='enemy' then ok,result=pcall(api,'player',unit)
    else ok,result=pcall(api,unit) end
    if not ok then return nil,'api-error' end
    if not self:CanReadValue(result) then return nil,'restricted-value' end
    if result==nil then return nil,'unit-flag-unavailable' end
    if type(result)~='boolean' then return nil,'invalid-api-result' end
    return result

返回函数参考