Skip to content

Your first game in Gama ​

This tutorial builds CrateCatch, a small game where crates fall from the sky and you catch them with a paddle. It takes about fifteen minutes and shows the parts of Gama a game uses most: trees of parts and UI, events, a class, a module and physics. The finished game is in Examples/CrateCatch.

You need a build of Gamma with the window (any preset but headless); see Getting started. The commands below use Gamma for the built executable, such as build/debug/Gamma.

1. Make the project ​

Gamma new CrateCatch
Gamma play CrateCatch

Gamma new writes main.gma, an assets folder and a .luaurc that turns on strict type checking. Gamma play opens a window that says hello. Open the folder in VS Code with the Gama extension for highlighting, completion and errors as you type.

2. A floor and a camera ​

Replace main.gma with:

gama
Scene.PhysicsEnabled = true
let camera = Scene.CurrentCamera :: Camera
camera.Transform = Transform.lookAt(<0, 14, 34>, <0, 10, 0>)

Part {
	Name = "Floor",
	Size = <40, 1, 10>,
	Position = <0, -0.5, 0>,
	Anchored = true,
	Color = $3a3f4b,
	Parent = Scene,
}
  • Scene.PhysicsEnabled = true turns on the physics simulation, so parts that are not anchored fall.
  • <0, 14, 34> is a Vector3 and $3a3f4b a colour.
  • Part { ... } is a tree: it makes a part and sets its properties. Anchored = true keeps the floor still.
  • Scene.CurrentCamera :: Camera tells the type checker the camera is there, as it is in every new Scene.

3. A paddle you can move ​

Add the paddle under the floor, and move it every frame while an arrow key is held:

gama
let paddle = Part {
	Name = "Paddle",
	Size = <6, 1, 4>,
	Position = <0, 2, 0>,
	Anchored = true,
	Color = $6d8dff,
	Parent = Scene,
}

const speed = 20

on Run.Heartbeat(deltaTime)
	let direction = 0
	if Input:IsKeyDown(Enum.KeyCode.Left) then
		direction -= 1
	end
	if Input:IsKeyDown(Enum.KeyCode.Right) then
		direction += 1
	end
	let x = math.clamp(paddle.Position.X + direction * speed * deltaTime, -17, 17)
	paddle.Position = <x, 2, 0>
end

let declares a variable and const one that never changes. on Run.Heartbeat(deltaTime) ... end connects a function to the event that fires every frame, with the seconds since the last one. The paddle is anchored, so physics leaves it where the script puts it, but falling crates still land on it.

4. Crates, in a module of their own ​

Make src/Crate.gma:

gama
let random = Random.new()

export class Crate
	part: Part
	caught = false

	fn new(paddle: BasePart, onCaught: () -> ())
		self.part = Part {
			Size = <2, 2, 2>,
			Position = <random:NextNumber(-10, 10), 30, 0>,
			Color = Color.fromHSV(random:NextNumber(), 0.6, 0.9),
			Parent = Scene,
		}
		on self.part.Touched(other)
			if other == paddle and not self.caught then
				self.caught = true
				onCaught()
				self.part:Destroy()
			end
		end
	end
end
  • A class has fields, with a type, a value or both, and methods. fn new is the constructor, called as Crate.new(...), and self is the new crate.
  • The crate is not anchored, so it falls once it is in the Scene.
  • Touched fires when the simulation finds the crate touching another part, and passes that part. caught makes sure a crate counts once, even if it bounces.
  • export makes Crate part of what the module gives to the files that require it.

5. Drop crates and keep score ​

Back in main.gma, require the module at the top:

gama
let Crates = require("./src/Crate")

and add a score label and a timer at the bottom:

gama
let score = 0
let label = Text { Text = "Caught: 0", FontSize = 24, Color = $ffffff }
Surface { Target = "Window", Padding = 20, Parent = Scene, label }

every 1.5 now do
	Crates.Crate.new(paddle, fn()
		score += 1
		label.Text = `Caught: {score}`
	end)
end

Surface draws UI on the window, and Text is a node in it. every 1.5 now do ... end runs its body straight away and then every 1.5 seconds, without holding up the rest of the file. fn() ... end is a function passed to the crate, which it calls when it is caught. `Caught: {score}` is an interpolated string.

Run it with Gamma play CrateCatch, and catch what you can.

6. Check it ​

Gamma check CrateCatch

type checks every .gma file in the project, as the editor does, and exits with 1 on errors, so it can run in CI. Try writing paddle.Positon and see what it says.

Where to go next ​

  • gama.md has the whole language, with a cheat sheet at the end.
  • projects.md covers project folders, physics, settings and exporting a game as one executable.
  • Make the paddle a class, give crates a chance to be gold and worth more with a match, or weld two parts together with a WeldConstraint to make a wider paddle.

Apache License 2.0