Method Idle
- Namespace
- GrindFest
- Assembly
- GrindFest.dll
Idle(string)
A simple bot that handles basic combat, looting, and survival in a specified area. Call this method repeatedly to make your hero automatically fight and survive.
Copying the source code of this method to your own script is a good starting point for creating your own bot.
Remember that this method is implemented inside the AutomaticHero class. If you are not using classes but a single script with top-level methods, you need to use Hero static class that is representing your currently selected hero. And prefix all methods calls and properties with Hero. like this: CurrentArea?.Root.Name
becomes Hero.CurrentArea?.Root.Name
and GoToArea("Stony Plains")
becomes Hero.GoToArea("Stony Plains")
.
public static void Idle(string area = "Stony Plains")
Parameters
area
stringThe area where the bot should operate. Defaults to "Stony Plains".
Remarks
See the following tutorials for more information:
This bot will:
- Navigate to the specified area if not already there
- Use health potions in two scenarios:
- When below 30% health during combat
- When health is not full and no enemies are around
- Attack nearest enemies when health is good
- Pick up nearby items when no enemies around
- Run around exploring when nothing else to do
using UnityEngine;
namespace GrindFest
{
public partial class AutomaticHero
{
/// <summary>
/// A simple bot that handles basic combat, looting, and survival in a specified area.
/// Call this method repeatedly to make your hero automatically fight and survive.
///
/// Copying the source code of this method to your own script is a good starting point for creating your own bot.
/// Remember that this method is implemented inside the <see cref="GrindFest.AutomaticHero"/> class. If you are not using classes but a single script with top-level methods, you need to use <see cref="GrindFest.Hero"/> static class that is representing your currently selected hero. And prefix all methods calls and properties with <see cref="GrindFest.Hero">Hero.</see> like this: <c>CurrentArea?.Root.Name</c> becomes <c>Hero.CurrentArea?.Root.Name</c> and <c>GoToArea("Stony Plains")</c> becomes <c>Hero.GoToArea("Stony Plains")</c>.
/// </summary>
/// <param name="area">The area where the bot should operate. Defaults to "Stony Plains".</param>
/// <remarks>
/// See the following tutorials for more information:
/// <list type="bullet">
/// <item><description><see href="/tutorials/first-bot.html">Getting Started with Bots</see></description></item>
/// <item><description><see href="/tutorials/movement.html">Creating Your First Combat Bot</see></description></item>
/// <item><description><see href="/tutorials/classes.html">Creating a Bot with Classes (Advanced)</see></description></item>
/// </list>
///
/// This bot will:
/// - Navigate to the specified area if not already there
/// - Use health potions in two scenarios:
/// * When below 30% health during combat
/// * When health is not full and no enemies are around
/// - Attack nearest enemies when health is good
/// - Pick up nearby items when no enemies around
/// - Run around exploring when nothing else to do
///
/// [!code-csharp[Source](../../GrindFest/Assets/_GrindFest/Scripts/BotApi/AutomaticHero.Idle.cs)]
///
/// <example>
/// Usage:
/// <code>
/// // Just call Idle() to use the basic bot
/// Idle();
/// </code>
/// </example>
/// <example>
/// Advanced usage:
/// <code>
/// // Idle in specific area
/// Idle("Crimson Meadows");
/// </code>
/// </example>
/// </remarks>
public void Idle(string area = "Stony Plains")
{
// Ensure hero is in the specified area
if (CurrentArea?.Root.Name != area)
{
GoToArea(area);
return;
}
// Health management - drink potions if health is low, only if we have potions
if (HasHealthPotion())
{
// if there are no enemies around and health is not full, drink potions
if (Health < MaxHealth && FindNearestEnemy(5) == null)
{
DrinkHealthPotion();
RunAwayFromNearestEnemy();
return; // Drinking potions to full health is a priority
}
// drink potions if below 30% or continue drinking until full
if (Health < MaxHealth * 0.3f)
{
DrinkHealthPotion();
RunAwayFromNearestEnemy();
return; // Drinking potions is a priority
}
}
// Combat - attack enemies if health is good
if (AttackNearestEnemy())
{
return; // Currently fighting an enemy
}
// Looting - pick up nearby items when not fighting
var item = FindNearestItemOnGround();
if (item != null)
{
if (PickUp(item)) // go to and pick up the item, return false until the item is picked up
{
Say($"Found {(item.Amount > 1 ? item.Amount + " " : "")} {item.name}!"); // make the hero say what they found and how many, taking into account stackable items like gold coins
return;
}
return; // Currently moving to item
}
// Exploration - run around when nothing else to do
RunAroundInArea();
}
}
}
// Just call Hero.Idle() to use the basic bot
Hero.Idle();
// Hero.Idle in specific area
Hero.Idle("Crimson Meadows");