參考資料 › 函式參考 › Platform

Platform.unit_health.GetUnitHealthObservation

公用

呼叫方式

addon.Platform:GetUnitHealthObservation(unit, includeMaximum)

可直接透過 addon 使用,不需要相依項目。

作用

在受支援的用戶端上觀測可讀的目前生命力,並可選擇性地觀測最大生命力。可讀且不存在的單位傳回 exists=false。缺少、失敗、受限或格式錯誤的觀測會傳回 nil 及原因;它們不能證明單位不存在或得出生命力。

參數

名稱類型作用
selfRMPlatformRotation Master 平台轉接器(使用冒號呼叫時隱含傳入)。
unitstring要觀測其目前生命力的單位代碼。
includeMaximumboolean | nil為 true 時還會觀測最大生命力;為 false 或 nil 時只讀取目前生命力。

傳回值

1table | nil全新的可讀記錄,包含 exists,存在時還包含目前生命力。僅在請求時才包含最大生命力。
2string | nil生命力觀測為 nil 時的政策、驗證或可用性原因。

版本

內建輔助函式在每個版本上都會註冊。不同的是它們讀取的遊戲 API;某個 API 不存在時,輔助函式會改用備用方式,或傳回 nil 並附上原因。

  • 該版本無法使用(原因請見提示)
  • 僅在暴雪的棄用相容層開啟時
遊戲 API經典時代TBC 週年版經典進程永久正式版
UnitExists經典時代TBC 週年版經典進程永久正式版
UnitHealth經典時代TBC 週年版經典進程永久正式版
UnitHealthMax經典時代TBC 週年版經典進程永久正式版
canaccessvalue經典時代TBC 週年版經典進程永久正式版
issecretvalue經典時代TBC 週年版經典進程永久正式版

機密值(正式版與永久)

在正式版與永久上,遊戲交給插件的是機密值。此輔助函式會檢查它讀取的內容(addon.Platform:CanReadValue),遇到無法比較的機密值時,會傳回 nil 並附上原因(例如 restricted-value),而不是傳回該機密值。請將 nil 視為無法使用,而非假。

範例

local health = addon.Platform:GetUnitHealthObservation("target", true)
if not health then return nil end
if not health.exists then return false end
if health.maximum == 0 then return nil end
return health.current / health.maximum < 0.2

相依項目

原始碼

Platform/unit_health.lua,第 28 行。在遊戲中為唯讀;「複製」會產生一份可編輯的複本。

Platform.unit_health.GetUnitHealthObservation(self, unit, includeMaximum)
    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(includeMaximum) then return nil,'restricted-value' end
    if type(unit)~='string' or unit=='' then return nil,'invalid-unit' end
    if includeMaximum~=nil and type(includeMaximum)~='boolean' then return nil,'invalid-health-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 {exists=false} end
    if type(UnitHealth)~='function' then return nil,'missing-api' end
    local currentOk,current=pcall(UnitHealth,unit)
    if not currentOk then return nil,'api-error' end
    if not self:CanReadValue(current) then return nil,'restricted-value' end
    -- Keep the native proof visible at the arithmetic boundary; the platform
    -- guard above also covers older clients and the issecretvalue predicate.
    if canaccessvalue and not canaccessvalue(current) then return nil,'restricted-value' end
    if current==nil then return nil,'health-data-unavailable' end
    if not finite(current) or current<0 then return nil,'invalid-api-result' end
    local record={exists=true,current=current}
    if includeMaximum then
        if type(UnitHealthMax)~='function' then return nil,'missing-api' end
        local maximumOk,maximum=pcall(UnitHealthMax,unit)
        if not maximumOk then return nil,'api-error' end
        if not self:CanReadValue(maximum) then return nil,'restricted-value' end
        if canaccessvalue and not canaccessvalue(maximum) then return nil,'restricted-value' end
        if maximum==nil then return nil,'health-data-unavailable' end
        if not finite(maximum) or maximum<0 then return nil,'invalid-api-result' end
        record.maximum=maximum
    end
    return record

返回函式參考