Variables
Name values with variables, give them types, change them, and keep the ones that shouldn't change fixed.
A variable is a name for a value. In OSL you write the type first, then the name, then the value:
string name = "Ada"
int age = 36
number height = 1.65
boolean curious = true
log name age height curiousThose four are the types you'll use most:
| Type | Holds | Examples |
|---|---|---|
string | text | "Ada", 'hi', "" |
int | whole numbers | 42, -7, 1_000_000 |
number | numbers with a fractional part | 1.65, 0.5, 3.0 |
boolean (or bool) | true or false | true |
Let the compiler work out the type#
When the value makes the type obvious, write auto and the compiler fills it in:
auto city = "Lisbon" // a string
auto population = 545000 // an int
log city ++ " has " ++ population ++ " people"auto still gives the variable one fixed type. It just saves you writing it. OSL code tends to spell types out where they help the reader, such as at the top of a file and on functions, and use auto for short-lived values in the middle.
++ joins values into text, turning numbers into strings on the way. You'll see it everywhere.
Changing a variable#
Assign a new value with =. There are shortcuts for the common changes:
int score = 10
score = 15
score += 5 // score = score + 5
score -= 2
score *= 2
score++ // add one
log score
string greeting = "Hello"
greeting ++= ", world" // greeting = greeting ++ ", world"
log greetingA variable keeps the type it was declared with. Putting a value of another type in it is an error the compiler catches before the program runs:
int score = 10
score = "ten"Declare before you use#
Every variable is declared once, with its type or auto. Assigning to a name that hasn't been declared is an error, so a typo can't quietly create a new variable:
int count = 1
cuont = count + 1The message names the mistake and shows the declaration you probably meant. If a value is computed but not needed, assign it to _ to throw it away on purpose.
Which of these lines doesn't compile?
Values that never change#
Use const<T> for a value that must stay as it is. The compiler rejects any later assignment:
const<int> maxPlayers = 4
const<string> title = "Space Race"
log title ++ " for up to " ++ maxPlayersconst<int> maxPlayers = 4
maxPlayers = 5const also protects arrays and objects from being changed through that name, which you'll meet in the lessons on arrays and types.
Names#
Names start with a letter or an underscore and continue with letters, digits and underscores. OSL code uses camelCase for variables and functions and PascalCase for types. Letters from any language are fine: string año = "2026" works.
A few words belong to the language and can't be names, such as if, for, return, class and import. The compiler tells you when you've picked one, and suggests another name.
A shopping total
Three apples cost 40 each and a loaf costs 150, so the total should be 270. This program prints 193. Fix the line that works out total.
270