> LeekScript v2
**Warning: this page is meant for LeekScript 2 users. To learn about variables in the current version of LeekScript, please go to Variables**
To do:
----- What is a variable?
Variables are a fundamental concept in every programming language. They let you keep values ((results of expressions)) in memory so you can reuse them in calculations ((other expressions)).
LeekScript variables can hold several types of values: ((put links to the specific pages where needed, rather than saying later))
However, the type of a variable is not fixed (as in C or Java): it adapts to the data you want to store in it (as in JavaScript or PHP).
((There is no adapting going on: the type is simply checked each time the variable is used.))
To use a variable, you first need to declare it. ((Careful with the wording: it suggests that the variable can only be assigned afterwards, which is a problem with let and const. Swap this example and the next one.)) You declare it with the “var” keyword, like this:
// Declare the variable variableName var variableName;
You can also give your variable a value, like this:
// Declare the variable variableName and assign it the value 12 let variableName = 12;
Note that you can also declare several variables at once by separating them with commas:
// Declare firstVariable and secondVariable var firstVariable, secondVariable; let firstVar = 1, secondVar = 2
Note: The ; became optional with the move to LSv2. It is still useful if you want to make hard-to-read code clearer.
((Indentation makes code easier to read; the semicolon removes ambiguities for the compiler/interpreter.))
There are 4 kinds of variables. They differ in whether they can be modified and in how long they last during the fight. So we have:
Kinds of variables Mutable Constant End of turn var let End of fight global const
These variables are all declared the same way, but once a constant variable has been declared, it can hardly ((not at all, actually, unless there is some weird behavior with collections/anonymous objects.)) be modified any more (which makes sense).
((Move the examples from the var section that work with let into this section.))
((Show examples of justified reassignment, for instance with loops.))
There are several ways to use variables in your code.
You can assign them a value (that is, make them “remember” this value) with the assignment operator =:
((What is shown here is reassignment, which is specific to var and global.))
// Declare myVariable var myVariable // Assign 1 to myVariable myVariable = 1
You can do calculations by writing expressions, with the following operators:
Here is how it works:
((It's a bit dirty to keep reusing the same variable; we shouldn't show beginners dirty code. Psst: let))
// Declare the variable a var a // Do a few calculations a = 2 + 8 // a gets the value 10 a = 5 - 4 // a gets the value 1 a = 3 * 5 // a gets the value 15 a = 6 / 2 // a gets the value 3 a = 8 % 3 // a gets the value 2
// But you can also combine operators a = 1 + 2 * 3 // a gets the value 7 a = 4 / 2 + 1 // a gets the value 3
// And even use parentheses a = (2 + 3) * 2 // a gets the value 10
You can also use a variable in an expression: its value is used for the calculation. For example:
((Once again, let's avoid declaring variables in advance when there is no justified mutation.))
// Declare the variables a and b var a, b
// Assign the value 10 to a a = 10 // a gets the value 10
// Assign the value a + 5 to b b = a + 5 // b gets the value 15
You can also use strings, like this:
((Again.))
var myVariable myVariable = "I am a string"
With strings, only the + operator can be used: it concatenates pieces of strings, that is, puts them end to end. So this code:
((I know it's annoying to keep reading the same note, but beginners do what they are shown.))
// Declare str1 and str2 var str1, str2
str1 = "Hello " // str1 gets the value "Hello " str2 = str1 + "everyone" // str2 is "Hello everyone"
Note that you can concatenate a string with a number, which is handy for putting the result of a calculation into a string:
((Once again.))
// Declare a and str var a, str
a = 51 * 17 // a gets the value 867 str = "Multiplying 51 by 17 gives: " + a // str then contains "Multiplying 51 by 17 gives: 867"
((introduction before global variables))
((Not implemented yet))
((Like a let with a global scope))
((useless if no function is declared globally, that is with the syntax function name(parameters) { body }))
((Not implemented yet))
((Like a var with a global scope))
((useless if no function is declared globally, that is with the syntax function name(parameters) { body }))
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.