Movement
The automation of your hero starts with making it move.
You can make your hero follow your mouse cursor and attack nearby enemies, using the following code:
Hero.FollowCursorAndAttack(); // Hero will follow your mouse cursor and attack nearby enemies
Hero.RunAroundInArea(); // Run around in area
Variables
Let's keep the bot running and just change the code.
var item = Hero.FindNearestItem(); // Returns either nearest item or null if there is no item
Hero.Say(item?.name);
You can then declare a local variable
, a temporary storage for this item, using var variableName
and assign the resulting item from the method to it with =
You can then reference this variable from other parts of code, and access its properties, like name
, using the dot .
operator.
Note that there might not be any item, in that case the method will return null. Null values represent nothing so accessing proprties like .name
or trying to call methods on them will result in an error.
To prevent that, we use the Elvis ?.
operator here, which allows you to safely access null properties and let the compiler do checks automatically. We can also solve this using an if
condition.
Reference to a FindNearestItem method.
Time to make a decision
If you need to do something only if a certain condition
is met, you can use an if
statement.
var item = Hero.FindNearestItem(); // Returns either nearest item or null if there is no item
if (item != null) // if there is an item
{
Hero.Say(item.name);
}
if
is a conditional statement that checks if the condition inside the parentheses is true. If it is, the code inside the curly brackets will be executed. If it's false, the code will be skipped.
When you are comparing two values, you use comparison operators like ==
(equal), !=
(not equal), >
, <
, >=
, <=
.
In this case, we use the !=
operator to compare the item with null to check if there is any nearby item.
You can also use else
statement to execute code when the condition is not met.
var item = Hero.FindNearestItem();
if (item != null)
{
Hero.Say(item.name);
}
else
{
Hero.Say("No items nearby");
}
Method Hero.AttackNearestEnemy returns a boolean
. A value that is either true
or false
representing logical truth.
Operators like ==
, !=
, >
, <
, >=
, <=
are used to compare values and return a boolean value.
if (Hero.Health < Hero.MaxHealth / 2) // If health is less than half of max health
{
Hero.RunAwayFromNearestEnemy();
}
We can store this truth value in a variable and use it later.
var hasLowHealth = Hero.Health < Hero.MaxHealth / 2;
if (hasLowHealth)
{
Hero.RunAwayFromNearestEnemy();
}
Note that if (variable)
is equivalent to if (variable == true)
You can combine methods Hero.AttackNearestEnemy and Hero.RunAroundInArea to make hero run around in area and attack nearby enemies
if (!Hero.AttackNearestEnemy()) // AttackNearestEnemy returns true or false based on whether it was able to find some enemy and attack it or not
{
Hero.RunAroundInArea();
}
The !
symbol in front of the AttackNearestEnemy() call is a logical NOT operator. It inverts the value of the expression. If the expression is true, it will return false and vice versa.
The condition above is equivalent to if (Hero.AttackNearestEnemy() == false)
and to if (Hero.AttackNearestEnemy() != true)
.
So the condition checks if hero was not (!) able to attack enemy, and if so, it runs around in area.
Hero.RunAroundInAreaAndAttack(); // Or simply use this method prepared for you, if you don't need any additional logic, like drinkning potions or picking up items