Your first script
Ten minutes, from an empty level to a block that moves when the cube touches a button.
Make a script
- In the editor, start a new level (File > New) and open the Logic tab.
- Turn on Scripted logic if you have not yet, and go to the Scripts side.
- Click New script and give it a name, for example
hello.
A new script already has a line in it:
print("hello from " .. script.name)print writes to the script output. .. joins two pieces of text, and script.name is the name you just gave the script.
Run it
Press F5 to test play the level. Nothing looks different, because the script only prints. Press Esc to come back to the editor, and look at the script output, on the right:
It prints
hello from helloThat is the whole loop: write, test play, read the script output.
Notes for yourself
A line that begins with two dashes is a note. The game skips it; it is there for whoever reads the script, which is mostly you, later.
-- This is a note.
print("this runs") -- a note can follow code, tooCtrl+/ turns the lines you have selected into notes, and back.
Make something
Scripts can make things while the level runs. Replace the script's text with this:
local x, y = camera.x, camera.y
-- A strip of floor, and a block standing on it.
things.new("floor", { x = x + 4, y = y, w = 12, h = 3 })
local block = things.new("block", { x = x + 5, y = y })
print("made a " .. block.type)Test play again. A strip of floor appears to the right of the start, with a block on it.
camera.xandcamera.yare where the camera looks, in tiles. At the start of a new level that is the middle of the start pad.things.newmakes a thing. The first value says what kind ("floor","block","saw","spike","deco"or"text"), the second is a table of what it should be like: where its middle is (x,y) and how big it is (w,h).local block = ...keeps the new thing under a name, so the script can use it afterwards.
Make it move
Add one more line, so that the script ends like this:
local block = things.new("block", { x = camera.x + 5, y = camera.y })
block:tween({ y = block.y - 3 }, 2)block:tween({ y = block.y - 3 }, 2) glides the block's y to three tiles higher, over two seconds. Test play, and the block slides up.
Wait for the player
Last step: the block should move only when the cube touches a button. Here is the whole script.
local x, y = camera.x, camera.y
things.new("floor", { x = x + 4, y = y, w = 12, h = 3 })
local door = things.new("block", { x = x + 5, y = y, w = 1, h = 3 })
local button = things.new("deco", { x = x + 2, y = y })
-- The script stops on this line until the cube touches the button.
button.touched:wait()
door:tween({ y = door.y - 3 }, 1)
print("the door is open")button.touched is an event: something that happens. :wait() pauses the script until it does. Meanwhile the level carries on as always; only this script is waiting.
This is the example A button and a door from the editor's Try an example, a little shorter.
What you have used
| You wrote | It means |
|---|---|
print("...") | Write to the script output |
local name = value | Keep a value under a name |
things.new(kind, { ... }) | Make a thing |
things.find("name") | Find a thing by its name |
thing:tween({ ... }, seconds) | Glide its numbers to new values |
event:wait() | Pause until something happens |
Next: values and variables, the first page about the language itself.