Your first script
In this tutorial you make a script, run it, give it a setting and break it on purpose so you know what an error looks like. It takes about five minutes.
1. Make the file
Open the menu, go to Scripts and press + in the Local box. Type hello and press Create. evasive writes hello.lua into your scripts folder and opens it in the editor:
-- @name hello
-- @author
-- @version 1.0
-- @description
local enabled = ui.checkbox("Enabled", true)
events.on("paint", function()
if not enabled:get() then return end
local w, h = draw.screen_size()
draw.text(w / 2, 40, "hello", color(255, 255, 255), {align = "center", outline = true})
end)2. Run it
Press F5 in the editor. The word "hello" shows up at the top of your screen, and hello.lua turns green in the list.
Here is what each part does:
ui.checkbox("Enabled", true)adds a checkbox to the script's box on the Settings page. It returns a control, andenabled:get()reads its value.events.on("paint", ...)runs the function on every frame the overlay draws. All drawing has to happen inside a paint handler.draw.screen_size()gives the overlay's width and height in pixels.draw.textdraws the text.color(255, 255, 255)is white, and the last table centres the text onxand gives it an outline.
Open Scripts > Settings and untick Enabled. The text goes away.
3. Let the user pick the text
Replace the file with this and press F5 again:
-- @name Hello
-- @description A label in the top left corner.
local label = ui.text_input("Text", "hello from Lua", 32)
events.on("paint", function()
draw.text(20, 20, label:get(), ui.accent(), { shadow = true })
end)The Settings page now has a text field. Whatever you type there is drawn in the top left corner, in the menu's accent colour from ui.accent(). The value is saved in your config, so it is still there after a restart.
4. Print something
Add this line at the end of the file and run it:
print("hello.lua loaded, the label reads " .. label:get())Open Scripts > Console. Your line is there, tagged with the script's file name. print is the same as log.info. Use log.warn and log.error for lines that should stand out.
5. Break it
Move the drawing out of the paint handler, at the top level of the file:
local label = ui.text_input("Text", "hello from Lua", 32)
draw.text(20, 20, label:get(), ui.accent())Press F5. The console shows reload failed: with the line number and the message draw.* only works inside paint handlers. A reload that fails leaves the last working version running, so the label stays on screen. A script that was not loaded at all does not start, and its name turns red.
Put the call back inside events.on("paint", ...) and run it again. The console says reloaded.
Most mistakes work the same way. The console tells you the line, you fix it, save and run.
6. Use your own editor
Turn on Reload on file change in Scripts > Other, then open Documents\evasive\scripts\hello.lua in any text editor. Every save now reloads the script in game.
Next
- How scripts run explains the events and the rules every script lives by.
- Drawing on screen goes through shapes, text, colours and images.