Variables in depth

Variables in depth

> LeekScript Tutorial

The first concept you need to learn is this one: variables.

Variables let you store information. When you create a variable, you give it a name. You can then use that name to read the information it holds, or to give it new information. Variables are the foundation of any program. They're a bit like your leek's memory.

What is it?

We won't go into the details of how a computer's memory works. Just think of it this way: your leek is able to remember numbers. Lots of numbers...

When you create a variable, you reserve a spot in your leek's memory to store a piece of information. You're telling it: Remember this, you'll need it later!

You can picture variables as drawers. Imagine that your leek's head is packed full of drawers. When you want to put your socks away, you open a drawer, put your socks in it, and stick a "Socks" label on the front of the drawer.

On Leek Wars, it's the same! (Minus the socks.) If you want to keep track of your life points at a given moment, you'll need a drawer. With your code, you'll put your life points in a drawer and write "Life points" on it. When you need that information, you'll tell your leek: Give me the contents of the "Life points" drawer.

Types

In computing, variables are classified by type. You can store different kinds of information:

LeekScript is a dynamically typed language. This means you don't have to define the type of a variable yourself: the language takes care of it for you. You declare a variable and store whatever you want in it, and the variable adapts to its contents. This makes variables easier to use for beginners, but it can also trip you up if you're not careful.

Creating a variable

To start with, you need a name for your variable. But there are a few rules to follow:

So names like "lifeCount", "leek", "my_cell" and "leek1" are valid variable names. Watch out for case: "leek" and "Leek" are two different variables.

Smart names

It's best not to name your variables any old way. Naming your variables "a", "b", "c"... may seem quick and handy, especially when you're short on inspiration. Yet you'll lose more time remembering what a variable called "h" is for than you save when writing it...

Be precise: use variable names that clearly show what they're for. Abbreviations can be useful, but don't overdo them. A variable that holds the enemy's cell? "enemyCell" is a very good name. A variable that holds the distance to the enemy? A simple "distance" is enough, but you'll need to be more specific if you store several distances.

Declaring a variable

It's finally time to talk about the var keyword. This is what lets you create a variable.

var myVariable;

This code declares a variable named "myVariable". This is called a "declaration". To go back to our drawers, this code creates a drawer labeled "myVariable", and that drawer is empty. Notice the semicolon ';' at the end of the line. It marks the end of an instruction. Don't forget it!

A variable that holds nothing isn't very useful. Here's how to give our variable a value at the moment we declare it. This is called "initialization".

var myVariable = 5;

This creates a variable named "myVariable" that holds the integer value 5. Again, we create a drawer labeled "myVariable", but this time we put the value "5" in it. You could say this line tells your leek: Remember that myVariable is 5.

Let's display the contents of our variable with the debug function. To do so, we call the function like this: "debug();", and we put what it should display between the parentheses.

var myVariable = 5; debug(myVariable); //Displays: 5

This code displays the contents of "myVariable" in the fight logs. Try it in a test fight.

A variable can store just about anything. You can put integers, decimal numbers and even text in it.

var anInteger = 5; debug(anInteger); //Displays: 5

var aDecimal = 3.14; debug(aDecimal); //Displays: 3.14

var someText = "Hello!"; debug(someText); //Displays: Hello!

Note that text must be enclosed in quotes '"'.

Careful: decimal numbers are written with a point, not a comma.

Booleans

Finally, there's one last type of data you can store. These are called booleans.

A boolean is a variable that can only have two different values. No more, no less.

Whereas an integer can be 0, 1, 2, etc., a boolean can only hold the values true or false.

No shades of gray for a boolean: to it, everything is either yes or no (true or false in code).

Let's declare some booleans!

var myBoolean = true; debug(myBoolean); // Displays: true myBoolean = false; debug(myBoolean); // Displays: false

Make sure you understand that this isn't text. Notice that there are no quotes. true and false are two specific values, just like 1, -2, 3.14, "boop", etc.

Assigning a value

Creating a variable with a value is all well and good. But being able to change that value whenever we like would be even better!

And you already know the syntax to do that. Look at this code again:

var myVariable = 5;

You've learned that the var keyword creates a variable. Well, the equals sign '=' means that we're assigning a value to that variable.

So, to change the contents of a variable:

var myVariable = 5; debug(myVariable); // Displays: 5

myVariable = -10; debug(myVariable); // Displays: -10

var myVariable = 5.64; debug(myVariable); // Displays: 5.64

myVariable = "I love leeks!"; debug(myVariable); // Displays: I love leeks!

myVariable = true; debug(myVariable); // Displays: true

This is called assignment. We assign the value -10 to myVariable. You can change the contents of a variable whenever you feel like it.

Globals

There's another keyword for declaring variables: global lets you declare what are called "global" variables.

You now know that your code runs on each of your leek's turns. So your variables declared with var are recreated every turn. They're also reinitialized at the same time.

That's not the case for globals. If you declare a global variable like this:

global myVariable = 5;

This variable is created on your leek's first turn. But unlike normal variables, it keeps existing after the turn ends.

To get a better picture of what's happening, we'll jump ahead to the next chapter a little. Try this code:

var myVariable = 0; myVariable = myVariable + 1; debug(myVariable);

As you'll see in the next chapter, the line "myVariable = myVariable + 1;" increases the value of "myVariable" by 1. If "myVariable" is 1, it becomes 2. If it's 2, it becomes 3, and so on.

With this code, on turn 1 you'll see the value 1 displayed. On turn 2 too. And on every turn after that. Each turn, the variable is recreated, so it's always 1 when it gets displayed.

Now let's try with a global:

global myVariable = 0; myVariable = myVariable + 1; debug(myVariable);

On turn 1, you should see the value 1. Nothing special so far. And on turn 2... the variable is 2! And it keeps going: the value goes up every turn.

This time, the variable is created on the first turn, but not on the following ones. And it keeps existing. Note that the line "global myVariable = 0;" only runs on the first turn. The variable keeps existing, and it isn't reset to zero either.

In practice, globals let you keep information from one turn to the next. That said, don't overuse them, and be careful to keep the information you store up to date. For example, if you store your opponent's position, make sure to update it before shooting. It would be a shame to shoot at a cell your opponent has already left.

Globals have another use, but we'll talk about it later, when we cover variable scope.

Conclusion

You now know how to create variables with var and change their contents with '='.

But variables still have a few secrets in store for you. We'll talk about them later: you need to learn a thing or two first.