Skip to content

Examples

Basic text

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-- declare a forward
function on_paint()
    -- call renderer.text function
    renderer.text(
        50, 50,                     -- x, y
        color.new(255, 255, 255),   -- white color (r, g, b)
        'Hello world!',             -- text to print
        fonts.default               -- use default font (verdana 13px)
        )
end

Looping through players

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-- player's class is CCSPlayer, so we can do this:
for i, player in ipairs(entity_list.get_all('CCSPlayer')) do
    -- let's print a player name to console

    -- get player info
    local player_info = engine_client.get_player_info(player:index())

    -- print (hint: use .. as string concatenation)
    print('A player: ' .. player_info.name .. '\n')
end

Creating menu elements

Result:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-- as config var we prefer to name like this: autor_scriptname_varname, but of course it's only your preference
local my_cb = gui.new_checkbox('My checkbox', 'pf_example_mycheckbox', false)

function on_paint()
    -- print checkbox value
    renderer.text(
        50, 50, color.new(255, 255, 255), 
        'My checkbox: ' .. tostring(my_cb:get_value()), -- We use tostring here to turn a bool into a string
        fonts.default)
end

Using timers

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
-- function that will be called on timer tick
local function tick()
    print('Tick!\n')
end

-- creating a timer
local my_timer = utils.new_timer(
    500, -- delay, in milliseconds
    tick -- function to call
)

-- start the timer
my_timer:start()

Using draggables & drawables

Result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- create a draggable object
local my_draggable = gui.new_draggable(
    50,                     -- initial X
    50,                     -- initial Y
    color.new(27, 27, 27),  -- background color
    false                   -- should draw texture
)

-- create a drawable object
local my_drawable = gui.new_drawable()
-- attach drawable to draggable object
my_drawable:attach(my_draggable)

-- update function to update drawable size, position, etc
function my_drawable:update()
    -- set parameters
    self.text = 'Hello ev0lve!'
    self.size_x, self.size_y = renderer.get_text_size(self.text, fonts.default)

    -- set padding
    self.size_x = self.size_x + 8
    self.size_y = self.size_y + 8
end

-- paint function to render drawable
function my_drawable:paint(x, y)
    renderer.text(
        x + 4, -- render according to
        y + 4, -- padding
        color.new(220, 220, 220), self.text, fonts.default)
end

-- paint callback
function on_paint()
    -- render draggable (renders all attached drawables)
    my_draggable:draw()
end