> LeekScript Tutorial
We are now going to talk about the functions of the LeekScript!
Functions are an essential aspect of programming. They allow you to memorize a series of actions that you can reuse whenever you want.
On Leek Wars, we can distinguish two types of functions:
We will only talk about native functions for now.
The native functions are the functions that the LeekScript makes available to you, they are the ones that will allow you to obtain a lot of information about the fight. It is also thanks to them that you will be able to control the actions of your leek.
Functions are quickly recognizable by their syntax:
functionName(parameters);
You have already seen them in the basic AI during the chapter on the Editor. As a reminder :
// Four very useful functions var enemy = getNearestEnemy(); setWeapon(WEAPON_PISTOL); moveToward(enemy); useWeapon(enemy);
getNearestEnemy, setWeapon, moveToward and useWeapon are four functions that the LeekScript makes available to you.
A minimal understanding can help to understand the usefulness of a function without having to consult the documentation. Function names are usually self-explanatory and show what they are used for.
What is the "return" of a function? It's not systematic, but a function can return information.
This is the case in this example:
var enemy = getNearestEnemy();
If you paid attention to the previous chapter, you understand that an "enemy" variable is created. And at the same time, it is assigned a value with the equal symbol '='.
But what exactly is assigned to him? This is where the getNearestEnemy function comes in. This function returns the id of the nearest enemy.
So, when your leek will execute this code, it will call the getNearestEnemy function. This will return the identifier of the closest leek which will then be stored in the "enemy" variable;
If you're curious, you can add a debug:
var enemy = getNearestEnemy(); debug(enemy);
If you run this code in a 1v1 test fight, you should see the number 1, or 0. Leek IDs in combat are nothing more than numbers. For a fight of 4 leeks, these would be numbered from 0 to 3.
As you can see, debug is a native feature of LeekScript. You may be wondering why we put the "enemy" variable in parentheses? We come there.
That's what you call what's inside the parentheses: parameters. These little beasts are very useful. The action a function will perform will depend on its parameters.
To return to the debug function, it displays a message in the combat logs. And this message, it is you who indicate its content, thanks to a parameter.
If you give the value 5 in parameter to debug, the message will be "5". If you give the text "I like leek soup" to debug, the message will be "I like leek soup".
This is what happens with these functions:
var enemy = getNearestEnemy(); setWeapon(WEAPON_PISTOL); moveToward(enemy); useWeapon(enemy);
setWeapon makes your leek equip a weapon. To indicate to him which weapon he must equip, you must pass the identifier of the weapon as a parameter. Here it's "WEAPON_PISTOL", it's a constant that designates the Pistol weapon. This constant corresponds to the number 37, the identifier of the gun. You could write 37 instead of WEAPON_PISTOL, it would have the same effect. But using the constant makes the code more understandable, and avoids having to memorize the identifiers of each weapon/chip.
moveToward allows you to move your leek towards another. The function needs the id of a target leek for this. This is where you pull out your "enemy" variable. Remember that it now contains the ID of your closest opponent. Passing this variable to moveToward will direct you to that nasty leek.
With useWeapon your leek will try to shoot a leek. In the same way, you tell the function that you want to shoot your enemy.
Some functions do not return a value, nor do they expect one. But they are not useless!
For example, the pause function. It allows you to pause the fight when you watch it. (It has no impact on the fight, it's the same as manually pressing the "Pause" button or the
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.