Your first program
Make one
Open Terminal (it's on the desktop, or in the macaw menu) and type:
new first.lua
This makes /home/first.lua from a template and opens it in Code. Press Ctrl+R (Cmd+R on a Mac) to run it. You get a window with an orange ball that you move with the arrow keys. Change something and press Ctrl+R again, and the window starts over with your change. Ctrl+S saves the file.
Terminal can start other kinds of program too. newcart mygame makes a game cart, newapp myapp.lua makes an app with buttons, and new3d my3d.lua makes a program with a 3D scene in it.
What a program looks like
WINDOW = { w = 240, h = 160, title = "Hello" }
local x = 120
function _init()
-- once, when the program starts
end
function _update()
-- 60 times a second: read the keys, move things
if btn("left") then x = x - 1 end
if btn("right") then x = x + 1 end
end
function _draw()
-- draw the window
cls(colors.navy)
text("Hello, Macaw!", 8, 8, colors.yellow)
circfill(x, 80, 8, colors.orange)
end
When the program starts, the file runs once from top to bottom. Anything at the top level, like local x = 120, is set up before _init is called. After that, Macaw OS calls these functions:
| Function | When it's called |
|---|---|
_init() | Once, after the file has run and the window is open |
_update() | 60 times a second, while the program runs |
_draw() | When the window needs drawing (see below) |
_resize(w, h) | When someone resizes the window. Its new size is w x h |
_close() | When the window is closing. Save anything you need to here |
_drop(paths, x, y) | When files are dragged onto your window, at x, y |
_display(w, h, colours) | When the screen changes size or number of colours |
You only need the ones you use. A program with just _draw works fine.
The window
WINDOW describes the window you want. Every field is optional:
| Field | What it does |
|---|---|
w, h | The size of the inside of the window, in pixels |
title | The words in the title bar (settitle(s) changes them later) |
scale | Shows every pixel 2, 3 or 4 times as big, if it fits on the screen |
realtime | true draws every frame, which games need (see below) |
resizable | false stops people resizing the window |
minw, minh | The smallest size the window can be resized to |
fullscreen | true starts the window full screen |
cursor | false hides the pointer over the window, for when you draw your own |
The top-left pixel of the window is 0, 0. x goes right and y goes down. If someone resizes the window, WINDOW.w and WINDOW.h change to match and _resize is called.
When _draw runs
Most programs only change when someone does something, so Macaw OS doesn't redraw every window every frame. _draw runs while someone is using your window, after they choose a menu item, answer a dialog or drop something on it, and every so often anyway.
- If
_updatechanges what the window shows (a timer ticked, or a download finished), callredraw()and the window is drawn on the next frame. - In a game, things move all the time. Set
realtime = trueinWINDOWand the window is drawn every frame. - Windows nobody can see aren't drawn at all. That includes hidden and rolled-up windows, and windows behind a full-screen one.
When it goes wrong
If your program has a mistake, you hear a squawk and its window shows This program crashed., with what went wrong and the line it happened on. Fix it in Code and press Ctrl+R again.
print(...)writes to Terminal, so you can watch values change.- The IDE (macaw menu, Programming Tools) runs a whole project. It checks that every file compiles, and shows each mistake with its line number and the lines that called it. Click a mistake to go to its line. The IDE also has a profiler that shows which of your functions take the most time.
- The Profiler shows how long every open window's
_updateand_drawtake.
A program can't freeze Macaw OS. If it runs for a long time without finishing, because of a loop that never ends or a big file it's loading, it's paused every 30 milliseconds and the rest of the desktop carries on. See busy programs.
Keeping things
Your program can read and write files in /home:
fs.write("/home/hello-score.txt", tostring(score))
local saved = tonumber(fs.read("/home/hello-score.txt") or "") or 0
fs.read returns nil if the file isn't there yet, so the or gives you a value to start with. See Files and the system for the other file calls.
Next
Make a game builds a whole game one step at a time. Or go straight to drawing, input and sound or 3D.