Полный Reflex-модуль
Соберём HealthWarning: сервер следит за здоровьем локального Agent, а клиент показывает предупреждение. Клиент отправляет порог, сервер проверяет его и возвращает только подтверждённое состояние.
Структура
Заголовок раздела «Структура»HealthWarning/├── module.json├── config.json└── scripts/ ├── main.lua └── server.luaВ module.json должно быть "IsReflex": true.
config.json
Заголовок раздела «config.json»Это обычная конфигурация модуля в формате Config:
[ { "label": "Show warning", "key": "Enabled", "type": "bool", "value": true }, { "label": "Health threshold", "key": "Threshold", "type": "number", "value": 30, "min": 1, "max": 100 }]scripts/main.lua
Заголовок раздела «scripts/main.lua»local ServerState = { health = 0, low = false}
local LastEnabled = nillocal LastThreshold = nil
local function SendSettings() LastEnabled = Config.Enabled LastThreshold = Config.Threshold
Network:SendTable({ kind = "set_settings", enabled = Config.Enabled, threshold = Config.Threshold })
Network:SendTable({ kind = "request_state" })end
Network:OnTableReceived(function(data) if data.kind == "health_state" then ServerState.health = tonumber(data.health) or 0 ServerState.low = data.low == true endend)
Scheduler:Schedule(0.5, SendSettings)
Scheduler:OnFrame(function() if LastEnabled ~= Config.Enabled or LastThreshold ~= Config.Threshold then SendSettings() end
if Config.Enabled and ServerState.low then ImGui:DrawText( "LOW HEALTH: " .. tostring(ServerState.health), Rect.new((Screen.Width - 360) * 0.5, 90, 360, 48), 24, Color.new(1, 0.25, 0.2, 1), TextAnchor.MiddleCenter ) endend)scripts/server.lua
Заголовок раздела «scripts/server.lua»local State = { enabled = true, threshold = 30, health = -1, low = false}
local function SendState() Network:SendTable({ kind = "health_state", health = State.health, low = State.low })end
Network:OnTableReceived(function(data) if data.kind == "set_settings" then local threshold = tonumber(data.threshold)
if threshold ~= nil then State.threshold = math.max(1, math.min(100, threshold)) end
State.enabled = data.enabled == true elseif data.kind == "request_state" then SendState() endend)
Scheduler:OnTick(function() local agent = Agents:GetLocalAgent() local health = 0 local low = false
if agent ~= nil and agent.Health ~= nil then health = agent.Health.CurrentHealth low = State.enabled and agent.Health.IsAlive and health <= State.threshold end
if health ~= State.health or low ~= State.low then State.health = health State.low = low SendState() endend)Что важно в примере
Заголовок раздела «Что важно в примере»- клиент отвечает за настройки и отображение;
- сервер ограничивает порог диапазоном
1…100; - состояние отправляется только при изменении;
- API-объекты не передаются через
Network; - обе стороны имеют безопасные значения до первой синхронизации.
После сохранения выберите HealthWarning активным Reflex-модулем и зайдите на пользовательский сервер.
Справочник примера: Network, Scheduler, Agent, Config и ImGui.