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 it will result in an error.
To prevent that, we use the Elvis ?.
operator, which allows you to safely access null properties and let the compiler do checks automatically. We can also solve this using an if
condition.
Conditions
If you need to do something only if a certain condition is met, you can use an if
statement.
var item = Hero.FindNearestItem();
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.
// You can combine these to methods 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.
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
//TODO: this is an old section from when the game used classes default for bots, it needs cleanup
Classes version
If you don't know what classes are, read this first Classes
class MyHero : AutomaticHero
{
// This is an Update method and it's called by the game all the time (Approximately 60 times per second). Whatever you put here, the bot will be doing it.
void Update()
{
if (IsBotting) // The Update method is called even if you pause the bot, this check is to make sure the bot is running. I'll be skipping it in tutorials if not relevant.
{
FollowCursorAndAttack(); // Makes your hero follow your mouse cursor and attack nearby enemies
}
}
}
In this method we are checking a where IsBotting is true.
Note: When you click the
Play
button or pressF5
the game will set IsBotting to true.
If you don't include the if
statement, the bot will follow your cursor even when you pause it.
Run around in area
class MyHero : AutomaticHero
{
void Update()
{
if (!IsBotting) // alternatively you can check if you are NOT botting
{
return; // and use the return statement to exit the method prematurely. The method will stop executing and nothing below this line will be executed.
}
RunAroundInArea(); // Note that this method just runs around in area and doesn't attack anyone
}
}
The return
statement is used to stop the execution of the method and return to the caller.
Note:This method has a return type
void
so you don't need to write what value you want to return in the return statement.
Killing monsters in the area
You can use method bool AttackNearestEnemy() to attack nearby enemies. This method has a return type "boolean" which means it returns true/false whether it was able to find some enemy and attack it or not. If you're not able to find the enemy, you will RunAroundInArea() to find the enemy.
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.
class MyHero : AutomaticHero
{
void Update()
{
if (!IsBotting)
{
return;
}
if (!AttackNearestEnemy())
{
RunAroundInArea();
}
}
}
GoTo
Vector3 Location
FindRandomPositionInArea()
FollowStoryPath()
What to do next?