Lua · Examples

Drawing

Shapes, text, images, fonts and 3D, all from on_frame.

Each snippet runs as-is: copy it, press From Clipboard on the Lua page, turn it on. Quick start

#Centred text

Measure first, then draw at half the width to the left.

drawing_1.lua
local white = color(255, 255, 255)

function on_frame()
    local w, h = draw.screen()
    local text = "CENTRE"
    local tw, th = draw.text_size(text, 20)
    draw.text(w / 2 - tw / 2, h * 0.3 - th / 2, text, white, 20)
end

#Progress bar

A background, a filled part and a label.

drawing_2.lua
local bg, fill, white = color(12, 14, 18, 210), color(110, 200, 140), color(240, 242, 246)

local function bar(x, y, w, frac, label)
    draw.rect_filled(x, y, w, 8, bg, 4)
    draw.rect_filled(x, y, w * math.clamp(frac, 0, 1), 8, fill, 4)
    draw.text(x, y - 18, label, white, 13)
end

function on_frame()
    local me = game.local_player()
    if me then bar(40, 300, 160, me.health / 100, "Health") end
end

#Image from the data folder

Put logo.png in scripts/data/<script>/. Load it once.

drawing_3.lua
local logo = draw.load_image("logo.png")

function on_frame()
    if not logo then return end
    local iw, ih = draw.image_size(logo)
    draw.image(logo, 20, 20, iw / 2, ih / 2, color(255, 255, 255, 230), 6)
end

#Custom font

Any installed font. It is ready one frame later; until then the menu font is used.

drawing_4.lua
local big = draw.load_font("segoeuib.ttf", 32) or 0

function on_frame()
    draw.text_font(big, 40, 120, "Adolfjon", color(255, 255, 255))
end

#3D ring at your feet

3D helpers take vectors.

drawing_5.lua
function on_frame()
    local me = game.local_player()
    if me then draw.circle3d(me.pos, 40, color(120, 200, 255, 200), 2) end
end

#Arrow with a polygon

Convex shapes only. Flat list of points.

drawing_6.lua
function on_frame()
    local w, h = draw.screen()
    local x, y = w / 2, h / 2 + 60
    draw.polygon({ x, y - 10, x + 8, y + 6, x - 8, y + 6 }, color(255, 200, 60))
end

Full scripts

Complete scripts with a header and settings, ready to use or change.