Finding and Managing Items
The first step in managing items is FindNearestItem(itemName)
And then you can PickUp(item) the item
var item = Hero.FindNearestItem("Gold", "Vial of Health");
if (item != null) // If we don't find any item, the method returns null, so we need to check if item != (is not) null
{
Hero.PickUp(item);
}
Previous method will find nearest gold or vial of health This way we can first pick up all the gold and then the vials of health
// First try to pick up gold
var item = Hero.FindNearestItem("Gold");
if (item != null)
{
Hero.PickUp(item);
return; // return stops the execution immediately, so the code below will not be executed, until the item is picked up
}
// Then pick up the trash
item = Hero.FindNearestItem("Vial of Health");
if (item != null)
{
Hero.PickUp(item);
}
By default, the method searches in distance of 15 units, but you can specify the distance as a second argument
var item = Hero.FindNearestItem("Gold", 20);
or as a named argument
var item = Hero.FindNearestItem("Gold", distance: 20);
Equipping
You can also Equip(item) the item
var item = Hero.FindNearestItem("Stick");
if (item != null)
{
Hero.Equip(item);
}
// Check current weapon
var weapon = Hero.Equipment.Weapon;
if (weapon != null)
{
Hero.Say($"I'm wielding a {weapon.name}");
}
Equipping the best weapon
Here we need to use a foreach loop
to go through all items on the ground and check
First we will call FindItemsOnGround() to get a List
of all the items.
We can then loop through (iterate) the list one by one, and apply the logic we want to each var item
loop variable that represents the current item in the list. You can name the variable anything you want, but it's a good practice to name it something that makes sense in the context.
foreach (var item in Hero.FindItemsOnGround()) // We loop over each item we find on the ground
{
if (item.Weapon != null) // Check if it's actually a weapon
{
if (item.Weapon.MaxDamage > Hero.Equipment.Weapon.MaxDamage) // Check if it's better than the current weapon
{
Hero.Equip(item);
Hero.Say($"I've equipped a {item.name} with {item.Weapon.MaxDamage} damage");
}
}
}
// Find a stick in the inventory and equip it
var item = Hero.FindItemInInventory("Stick");
if (item != null)
{
Hero.Equip(item);
}
Shortcut for opening inventory
if (Input.GetKeyDown(KeyCode.F2)) // check if use presses the F1 key
{
Hero.OpenInventory();
}
When you find an item, you can check if it's a weapon, shield, armor, etc. by examining its properties.
var item = Hero.FindItemsOnGround();
if (item?.Weapon != null) // Check if item exists and is a weapon
{
Hero.Say($"Found weapon: {item.name}");
Hero.Say($"Damage: {item.Weapon.MinDamage}-{item.Weapon.MaxDamage}");
Hero.Say($"Attack Speed: {item.Weapon.AttackSpeed}");
Hero.Say($"Durability: {item.Durability.CurrentDurability} / {item.Durability.MaxDurability}");
}
else if (item?.Shield != null) // Check if item exists and is a shield
{
Hero.Say($"Found shield: {item.name}");
Hero.Say($"Block Chance: {item.Shield.BlockChance}");
Hero.Say($"Durability: {item.Durability.CurrentDurability} / {item.Durability.MaxDurability}");
}
The Hero.Equipment property gives you access to what your hero has equipped.
Check what weapon, if any, your hero is wielding in any slot:
var weapon = Hero.Equipment.Weapon;
if (weapon != null)
{
Hero.Say($"I'm wielding a {weapon.name}");
}
Check what item, if any, your hero is wearing on their head:
var itemOnHead = Hero.Equipment[EquipmentSlot.Head];
if (itemOnHead != null)
{
Hero.Say($"I'm wearing a {itemOnHead.name} on my head");
}