Skip to Content

Examples

Complete scripts: hit log, health and armour overlay, chat message filter and timed reminder.


Hit log

Writes damage dealt by the local player to hits.txt in the script's data directory.

-- @name        Hit log
-- @version     1.0
-- @description Logs damage dealt by the local player.

local function now()
    return os.date('%Y-%m-%d %H:%M:%S')
end

events.player_hurt(function(event)
    local me = entitylist.local_controller()
    local attacker = event.attacker
    if not me or not attacker then return end
    if attacker ~= me then return end

    local line = ('%s  %d damage, %d health left, hitgroup %d\n'):format(
        now(), event.dmg_health, event.health, event.hitgroup)

    local ok, reason = fs.append('hits.txt', line)
    if not ok then
        print('hit log: ' .. reason)
    end
end)

Health and armour overlay

Adds an enable switch, corner selection and colour control to the script's settings sub-tab. Control values are persisted by their IDs.

-- @name        Health box
-- @version     1.0
-- @description Displays local health and armour.

local panel = assert(ui.panel('Health box'))
local enabled = panel:checkbox('enabled', 'Enabled', true)
local corner = panel:combo('corner', 'Corner', {
    'Top left', 'Top right', 'Bottom left', 'Bottom right'
}, 1)
local tint = panel:color_edit('tint', 'Colour', {
    r = 80, g = 200, b = 120, a = 230
})

corner:visible_if(function() return enabled:get() end)

local size = vector2d(150, 40)
local margin = 12
local background = color(15, 15, 15, 190)
local white = color(255, 255, 255)

events.render(function()
    if not enabled:get() or not engine.is_in_game() then return end

    local pawn = entitylist.local_pawn()
    if not pawn then return end

    local screen = render.screen_size()
    local selected = corner:get()
    local x = (selected == 1 or selected == 3)
        and (screen.x - size.x - margin) or margin
    local y = selected >= 2 and (screen.y - size.y - margin) or margin
    local position = vector2d(x, y)

    render.rect(position, size, background, 6)
    render.rect_outline(position, size, tint:get(), 1, 6)
    render.text(('HP %d   AP %d'):format(pawn.m_iHealth or 0, pawn.m_ArmorValue or 0),
        position + vector2d(12, 12), white)
end)

Chat message filter

Replaces the display of a received !roll message with a local dice-roll result. This filters HUD display; it does not prevent the original message from being sent to the server.

-- @name        Chat roll
-- @version     1.0
-- @description Replaces !roll messages in the local HUD chat.

events.player_chat(function(msg)
    if msg:get_message() ~= '!roll' then return end

    engine.chat_print(('%s rolled %d'):format(msg:get_name(), math.random(1, 6)))
    return false
end)

Timed reminder

Recreates a repeating timer when the interval changes. The interval is stored with the control's value in the configuration; no separate settings file is needed.

-- @name        Reminder
-- @version     1.0
-- @description Displays a notification at a configurable interval.

local panel = assert(ui.panel('Reminder'))
local minutes = panel:slider_int('minutes', 'Interval (minutes)', 10, 1, 60)
local timer_id

local function schedule()
    if timer_id then
        timer.cancel(timer_id)
    end
    timer_id = timer.every(minutes:get() * 60, function()
        notify.show('Reminder', 'Reminder interval elapsed.', 6)
    end)
end

minutes:on_change(schedule)
schedule()

On this page