參考資料 › 函式參考 › 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

返回函式參考