参考 › 函数参考 › Platform

Platform.unit_states.GetUnitStateObservation

公共

调用方式

addon.Platform:GetUnitStateObservation(unit, kind)

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

作用

在探测或受限策略客户端上,观测确切的所选单位是否死亡或处于灵魂状态,或是否对玩家友好。所需观测缺失、失败、受限或格式错误时为不可用。单位不存在时为不可用,而不是否定匹配,在 NOT 下同样如此。不会保留跨轮询的状态。

参数

名称类型作用
selfRMPlatformRotation Master 平台适配器(使用冒号调用时隐含传入)。
unitstring要观测的确切非空单位标识。
kindstring只能是 dead(对应 UnitIsDeadOrGhost)或 friendly(对应 UnitIsFriend(player, unit))。

返回值

1boolean | nil可读的当前状态;不可用时为 nil。
2string | nil无法读取当前状态时的可用性或查询验证原因。

版本

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

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

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

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

示例

local friendly = addon.Platform:GetUnitStateObservation("target", "friendly")
local dead = addon.Platform:GetUnitStateObservation("target", "dead")
if friendly == nil or dead == nil then return nil end
return friendly and dead -- a friend to resurrect

源码

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

Platform.unit_states.GetUnitStateObservation(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~='dead' and kind~='friendly' then return nil,'invalid-unit-state-query' end
    local existsAPI=UnitExists
    if type(existsAPI)~='function' then return nil,'missing-api' end
    local existsOK,exists=pcall(existsAPI,unit)
    if not existsOK then return nil,'api-error' end
    if not self:CanReadValue(exists) then return nil,'restricted-value' end
    if exists==nil or exists==false then return nil,'unit-data-unavailable' end
    if type(exists)~='boolean' then return nil,'invalid-api-result' end
    local api
    if kind=='dead' then api=UnitIsDeadOrGhost else api=UnitIsFriend end
    if type(api)~='function' then return nil,'missing-api' end
    local ok,result
    if kind=='friendly' 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-state-unavailable' end
    if type(result)~='boolean' then return nil,'invalid-api-result' end
    return result

返回函数参考