參考資料 › 函式參考 › Platform

Platform.casting.GetUnitCastObservation

公用

呼叫方式

addon.Platform:GetUnitCastObservation(unit, kind)

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

作用

在探測或受限政策用戶端上觀測單位的施法或引導。已知不存在的單位或可讀的非活動結果傳回 active=false。活動記錄包含 name,或在名稱為機密時包含 nameReason(單位仍在施法),以及可獨立取得的 remainingSeconds、durationSeconds、interruptible 與 spellID 欄位;選用欄位無法使用時附帶 remainingReason、durationReason、interruptibleReason 或 spellReason。存在性觀測缺少、失敗或受限時傳回 nil 及原因。

參數

名稱類型作用
selfRMPlatformRotation Master 平台轉接器(使用冒號呼叫時隱含傳入)。
unitstring要觀測其目前施法或引導的單位代碼。
kindstring觀測種類:cast 或 channel。它們使用原生可中斷元組中的不同位置。

傳回值

1table | nil新讀取的可讀記錄,包含 active,然後是 name 或 nameReason、remainingSeconds、durationSeconds、interruptible 與 spellID。剩餘時間與持續時間以秒為單位;剩餘時間最低為零。無法使用的選用欄位有其自身的原因;它不會使活動記錄失效。
2string | nil整個觀測無法使用時的可用性或查詢驗證原因。

版本

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

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

機密值(正式版與永久)

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

範例

local cast = addon.Platform:GetUnitCastObservation("target", "cast")
if not cast then return nil end
if not cast.active then return false end
if cast.interruptible == nil or cast.remainingSeconds == nil then return nil end
return cast.interruptible and cast.remainingSeconds > 0.5

相依項目

原始碼

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

Platform.casting.GetUnitCastObservation(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~='cast' and kind~='channel' then return nil,'invalid-cast-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 {active=false} end
    local fn
    if kind=='cast' then fn=UnitCastingInfo else fn=UnitChannelInfo end
    if type(fn)~='function' then return nil,'missing-api' end
    -- Capture into an owned tuple table without comparing native values. The
    -- channel flag is slot7; casting includes castID and uses slot8 instead,
    -- so the spell ID is slot9 for a cast and slot8 for a channel.
    local ok,tuple=pcall(function() return {fn(unit)} end)
    if not ok then return nil,'api-error' end
    local name=tuple[1]
    local record
    if not self:CanReadValue(name) then
        -- Retail and Forever make cast fields secret for a unit other than the
        -- player or pet unless the spell is flagged never secret
        -- (SecretWhenUnitSpellCastRestricted). A secret is a value, never nil:
        -- the unit is casting, and only the name is hidden.
        record={active=true,nameReason='restricted-value'}
    else
        if name==nil then return {active=false} end
        if type(name)~='string' or name=='' then return nil,'invalid-api-result' end
        record={active=true,name=name}
    end
    record.remainingSeconds,record.remainingReason=remainingSeconds(tuple[5])
    record.durationSeconds,record.durationReason=durationSeconds(tuple[4],tuple[5])
    record.interruptible,record.interruptibleReason=interruptibility(tuple[kind=='cast' and 8 or 7])
    record.spellID,record.spellReason=spellIdentity(tuple[kind=='cast' and 9 or 8])
    return record

返回函式參考