> Tutorial
Variables are a fundamental point in all programming languages. They are locations for storing values in memory in order to reuse them.
Variables can contain several types of values:
12, 5.59"hello"true, falsenull: the absence of a valueTo use a variable, it must be declared beforehand. In JavaScript this is done with the let keyword (for a value that can change) or const (for a value that never changes):
It is also possible to assign a value to your variable straight away:
Use const by default, and let only when you really need to reassign the variable. In TypeScript you can also annotate the type: const enemy: Entity = Fight.getNearestEnemy().
To use the value stored in a variable, we simply write its name:
Here me is your own leek, obtained with Fight.me, and enemy is the nearest enemy, obtained with Fight.getNearestEnemy(). We then pass enemy to the moveToward (move towards) and useWeapon (use the weapon) methods.
Your code is run again from the start at each turn, so variables declared with let or const only live for the current turn. To keep a value across turns, define a turn() function: the code outside turn() runs only once (when the AI loads), while turn() is replayed every turn.
In the rest of the tutorial we will mainly write simple AIs (without turn()), which run entirely at each turn.
What is a variable?
A value that changes often A place to store a value
Which code declares a foo variable?
let foo = 12 const foo = 12 foo variable foo
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.