Keep your place in this quest

Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.

Tiniest Engine uses a simple scripting language inspired by Lua and Python.

Variables

A variable is a named box that stores a value. You use variables so you can remember information and reuse it later. For example, a game might store the player's health, movement speed, score, or current inventory.

Use var to create a new variable. The name goes on the left, and the value goes on the right.

var name = "Player One"    // String
var health = 100           // Number (integer)
var speed = 2.5            // Number (float)
var isAlive = true         // Boolean
var inventory = []         // Empty list
var nothing = null         // Null value (no value)

Comments

Comments are notes for humans. The engine ignores them when running the game. Use comments to explain what a section of code is doing, leave reminders for yourself, or temporarily describe a tricky idea.

// This is a single-line comment
var x = 10  // Comment at end of line

Operators

Operators are symbols or keywords that combine values. Arithmetic operators do math, comparison operators ask questions, and logical operators combine true/false answers.

Most game logic is built from small expressions like "move left if the A key is held" or "collect a coin if the player touched it." Operators are how you write those expressions.

// Arithmetic
var sum = 5 + 3        // 8
var diff = 10 - 4      // 6
var product = 3 * 4    // 12
var quotient = 15 / 3  // 5
var remainder = 17 % 5 // 2

// Comparison
var equal = (5 == 5)       // true
var notEqual = (5 != 3)    // true
var greater = (10 > 5)     // true
var less = (3 < 7)         // true
var greaterEq = (5 >= 5)   // true
var lessEq = (3 <= 5)      // true

// Logical (use 'and' and 'or' keywords, not && or ||)
var both = true and false   // false
var either = true or false  // true
var not = !true             // false

Lists

A list stores multiple values in one variable. This is useful when you have many things of the same kind, such as enemies, bullets, coins, checkpoints, or menu options.

Use square brackets to create lists. [] creates an empty list, and [1, 2, 3] creates a list with starting values. List indexes start at 0, so the first item is items[0].

var items = []
items.add("sword")         // Add to end of list
items.add("shield")
items.add(42)

var numbers = [10, 20, 30]
var first = items[0]       // "sword"
numbers[1] = 99            // Overwrite by index
var count = items.length() // 3

items.remove(0)            // Remove element at index 0
items.clear()              // Remove all elements

Iterate through a list with an index loop:

for (var i = 0; i < items.length(); i = i + 1) {
    print(items[i])
}

If/Else Statements

An if statement lets the game choose what to do. If the condition is true, the first block runs. If it is false, the else block can run instead.

This is how games react to state: if health is zero, show game over; if a key is pressed, jump; if the score is high enough, unlock the next level.

if (health <= 0) {
    print("Game Over")
} else if (health < 25) {
    print("Low Health!")
} else {
    print("Health OK")
}

Loops

Loops repeat code. They are useful when you want to count, process every item in a list, spawn several objects, or keep doing something while a condition remains true.

Use a while loop when you want to repeat while a condition is true. Use a for loop when you know you are counting through a range or walking through a list by index.

While loop:

var i = 0
while (i < 10) {
    print(i)
    i = i + 1
}

For loop:

for (var i = 0; i < 10; i = i + 1) {
    print(i)
}

Break statement (exit loop early):

for (var i = 0; i < 100; i = i + 1) {
    if (i == 5) {
        break  // Exit the loop when i is 5
    }
    print(i)
}
// Prints: 0, 1, 2, 3, 4

Continue statement (skip to next iteration):

for (var i = 0; i < 5; i = i + 1) {
    if (i == 2) {
        continue  // Skip printing when i is 2
    }
    print(i)
}
// Prints: 0, 1, 3, 4

Functions

A function is a reusable block of code with a name. You call the function when you want that block to run. Functions help keep your script organized so you do not copy the same instructions everywhere.

Functions can receive values called parameters. They can also send a value back with return. In the example below, baseDamage and multiplier are parameters, and the function returns the final damage number.

func calculateDamage(baseDamage, multiplier) {
    return baseDamage * multiplier
}

var damage = calculateDamage(10, 1.5)  // 15