Справочник › Справочник функций › Platform

Platform.unit_health.GetUnitHealthObservation

общедоступный

Как вызывать

addon.Platform:GetUnitHealthObservation(unit, includeMaximum)

Доступен напрямую через addon; зависимость не нужна.

Что делает

Наблюдать читаемое текущее и, при необходимости, максимальное здоровье на поддерживаемых клиентах. Читаемо отсутствующий юнит возвращает exists=false. Отсутствующие, неудачные, ограниченные или повреждённые наблюдения возвращают nil с причиной; они не означают отсутствия юнита или значения здоровья.

Аргументы

ИмяТипЧто делает
selfRMPlatformАдаптер платформы Rotation Master (неявно при вызове через двоеточие).
unitstringТокен юнита, текущее здоровье которого нужно наблюдать.
includeMaximumboolean | niltrue также наблюдает максимальное здоровье; false или nil читает только текущее здоровье.

Возвращает

1table | nilСвежая читаемая запись с exists и, если юнит есть, текущим здоровьем. Максимальное здоровье включается только по запросу.
2string | nilПричина по политике, проверке или недоступности для наблюдения здоровья nil.

Издания

Встроенные помощники регистрируются во всех изданиях. Различается игровой API, который они читают; если нужного API нет, помощник использует запасной вариант или возвращает nil с причиной.

  • Недоступно в этом издании (причина — во всплывающей подсказке)
  • Только пока включены запасные реализации Blizzard для устаревших функций
Игровой APIClassic EraTBC AnniversaryClassic ProgressionForeverRetail
UnitExistsClassic EraTBC AnniversaryClassic ProgressionForeverRetail
UnitHealthClassic EraTBC AnniversaryClassic ProgressionForeverRetail
UnitHealthMaxClassic EraTBC AnniversaryClassic ProgressionForeverRetail
canaccessvalueClassic EraTBC AnniversaryClassic ProgressionForeverRetail
issecretvalueClassic EraTBC AnniversaryClassic ProgressionForeverRetail

Секретные значения (Retail и Forever)

В Retail и Forever игра передаёт аддонам секретные значения. Этот помощник проверяет то, что читает (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

Назад: Справочник функций