Skip to content

Cheat sheet

Each recipe is the shortest way to do one job, in both languages. The snippets are written as they sit inside a behavior’s methods: in Luau the behavior’s entity is self.entity, in AngelScript it is the self field the starter template keeps. Writing gameplay explains each area, and the API browser on a script page lists every call.

Log.info("Hello from " .. self.entity:name()) -- also Log.warn and Log.error
local dt = Time.delta() -- seconds since the last frame
local now = Time.now() -- seconds since the run began
local roll = Random.range(1.0, 6.0) -- also Random.value(), Random.intRange(1, 6)
local x = math.clamp(roll, 2, 5) -- Luau's own math library
local p = self.entity:position()
self.entity:setPosition(p.x + 2 * dt, p.y, p.z) -- 2 units a second along x
self.entity:setRotationEuler(0, 90, 0) -- degrees: pitch, yaw, roll
self.entity:setRotation(Quaternion.FromAxisAngle(Float3.new(0, 1, 0), math.rad(45)))
self.entity:setScale(2, 2, 2)
local where = self.entity:worldPosition() -- in the world, not the parent
local a = self.entity:worldPosition()
local b = target:worldPosition()
local toward = Float3.Normalized(b - a)
local far = Float3.Distance(a, b)
local halfway = Float3.Lerp(a, b, 0.5)
local up = Float3.Cross(toward, Float3.new(1, 0, 0))

A Float3, a Color and the other plain types are values: a copy is its own. To change one field of a value an engine object holds, copy it, change it and write it back (below, under Components).

local scene = self.entity.scene
local player = scene:find("Player") -- the first entity of that name
local door = scene:findByPath("Level/Doors/Front") -- through the hierarchy
local hand = self.entity:findChildByName("Hand")
local parent = self.entity:parent()
if player:isValid() then
-- ...
end

A lookup that finds nothing gives an entity whose isValid() is false, in both languages; so does an entity property never set, and an entity destroyed since you found it. An entity is never nil in Luau, so ask isValid() rather than if player then.

local child = self.entity:firstChild()
while child:isValid() do
Log.info(child:name())
child = child:nextSibling()
end
local Spark = Guid.new("245b459c-4b93-4876-9496-6e4eb3daa145") -- right-click a prefab, Copy GUID
local spark = self.entity.scene.prefabs:spawn(Spark, self.entity:worldPosition())
self.entity:setActive(false) -- hidden and still; setActive(true) brings it back
spark:destroy() -- the entity and its children
local light = LightComponent.of(self.entity) -- nil when the entity has none
if light then
light.intensity = 3
local c = light.color -- a copy
c.r = 1
light.color = c -- written back
end
local body = RigidBodyComponent.addTo(self.entity) -- adds one, or gives the one it has
body.mass = 2
RigidBodyComponent.removeFrom(self.entity)

A component is the entity’s own: writing light.intensity changes the light. A plain value it holds, such as its color, reads as a copy, so light.color.r = 1 would change only the copy: AngelScript refuses it, and in Luau it does nothing to the light.

if Input.wasPressed("Jump") then self:jump() end -- the frame it went down
local running = Input.isDown("Sprint") -- while it is held
local move = Input.value2D("Move") -- move.x, move.y from -1 to 1
Input.rumble(0.3, 0.6, 0.2) -- low, high, seconds

The names are the project’s input actions, so players can rebind them and a gamepad works too.

local physics = self.entity.scene.physics
physics:applyImpulse(self.entity, Float3.new(0, 5, 0)) -- the entity needs a rigid body
physics:setVelocity(self.entity, Float3.new(0, 0, 3))
local hit = physics:rayCast(self.entity:worldPosition(), Float3.new(0, -1, 0), 2.0)
if hit.hit then
Log.info("ground " .. hit.distance .. " below")
end
local near = physics:overlapSphere(self.entity:worldPosition(), 5.0) -- the entities in reach
function Coin:onTriggerEnter(other)
other:send("CoinCollected", 1)
self.entity:destroy()
end
function Crate:onContactBegin(other, point, normal, speed)
if speed > 2 then Log.info("thud") end
end
player:send("Hurt", 10) -- to one entity's behaviors
self.entity.scene.events:emit("DoorOpened") -- to every script with a handler
function Player:onHurt(amount) -- a message named Hurt calls onHurt
self.health = self.health - amount
end
Audio.playOneShotPath("Audio/Coin") -- by its path in the Assets panel
Audio.playMusicPath("Audio/Theme", 2.0) -- cross-fade over two seconds
local scene = self.entity.scene
scene.audio:play(self.entity) -- the entity's own audio source
scene.animation:playOneShot(self.entity, WaveClip) -- an animation clip's ID, over the current one
scene.animation:setFloat(self.entity, "Speed", 1.5) -- an animation graph parameter
local this = self
startCoroutine(function()
waitSeconds(2.0)
this.entity:setActive(false)
end, self) -- stops with the behavior
Save.setInt("coins", self.coins)
Save.setString("level", "Hometown")
Save.flush() -- write it now; the run also writes it as it ends
local coins = Save.getInt("coins", 0) -- the second value when nothing is saved
if Save.has("level") then ... end
Label.of(gui.root():find("score")).text = "Score " .. self.score -- an element's id in the GUI
run.loadScene(Level2) -- a scene's ID
run.setTimeScale(0) -- pause; 1 is normal speed
run.requestExit()
function Mover.new(entity)
return setmetatable({
entity = entity,
speed = 2.0, -- a number, a boolean, a string or a Float3
offset = Float3.new(0, 1, 0),
}, Mover)
end

The script_declarations tool of the engine’s MCP host writes the bound API as the file an editor extension reads: as.predefined for an AngelScript extension and engine.d.luau for luau-lsp. Write it into the project’s folder, and again after updating the engine.