A variable gives a value a name. Code can use the name instead of repeating the value, and a mutable variable can receive a different value later.
Declare a variable
var health = 100;
Read the declaration from left to right:
vardeclares a mutable variable.healthis its name.=supplies its initial value.100is that value.;finishes the declaration.
After this declaration, writing health reads the number currently stored under that name:
text "Health: {health}";
Type inference and annotations
Sugar recognizes that health is a number from its initial value. This is called type inference. These declarations infer three different types:
var name = "Nova";
var health = 100;
var awake = true;
You may write the type explicitly after a colon:
var name: str = "Nova";
var health: number = 100;
var awake: bool = true;
The part after : is a type annotation. It requires the initial value and later assignments to use that type.
Some declarations need an annotation because their value comes from a special input instead of an ordinary literal:
var reward: Item with {
input: true,
};
This means reward may hold an Item selected from inventory. Until someone selects one, it has no selected Item.
Ask for a value
A with block configures the input shown before the creation runs:
var difficulty: number = 5 with {
input: true,
required: true,
min: 1,
max: 10,
};
The declaration still creates one variable. Its extra pieces mean:
input: trueasks the person to supply the value.required: trueprevents an empty answer.min: 1rejects numbers below 1.max: 10rejects numbers above 10.
min and max constrain the number stored in the variable. Here, difficulty must itself be between 1 and 10.
Offer fixed choices
Use the choice type when the input should offer a fixed set of values:
var amount: choice = 20 with {
input: true,
required: true,
choices: (10, 20, 35),
};
The selected number is what amount stores. The next lesson teaches arrow syntax, including how to give these values different labels without changing what the variable stores.
One choice declaration may use strings, numbers, or booleans, but all its choice values and its initial value must have the same type. Because this example uses numbers, amount + 5 is valid number addition.
Select one or several compound values
A compound input can offer JOIs or, inside a Session, seats:
var rival: Joi = None with {
input: true,
required: true,
};
Use a list type when the person may select several:
var guests: List<Joi> = () with {
input: true,
required: true,
count: 1..=3,
};
As introduced on the previous page, List<Joi> means “list of JOIs.” The empty list () is the initial value.
count constrains the number of entries in the list. Here, guests must contain one, two, or three JOIs. It does not constrain a numeric value.
That is the exact difference:
minandmaxconstrain the value held by anumbervariable.countconstrains the length of aList<Joi>orList<Seat>variable.
Declare a calculated constant
Use const for a name whose value is recalculated from other values and cannot be assigned:
var strength = 8;
var bonus = 3;
const TOTAL_POWER = strength + bonus;
text "@1 attacks with {TOTAL_POWER} power.";
If strength changes, Sugar recalculates TOTAL_POWER. A constant cannot be an input, a memory variable, or a random calculation.
Upper snake case is recommended for constant names, but Sugar does not enforce it.
Regular var declarations begin from their initial value every time the creation runs. The Memory variables lesson later adds persistence. Next, Arrow syntax names and explains the three arrow tokens Sugar uses before the guide moves into expressions and programs.