//TODO TUTORIAL INCOMPLETE, CONTAINS INCORRECT INFORMATION
While what we were doing with all the logic inside Update loop is fine, it's not very scalable and the code could become unreadable due to all the complexity in one place.
You can instead react to different events that happen in the game.
class MyHero : AutomaticHero
{
void OnItemDropped(Item item)
{
if (item.Name == "Vial of Health")
{
PickUp(item);
}
}
}
The problem with this is that trying to handle multiple events might interrupt what the hero is doing at the moment. For example, if the hero is attacking a monster and a potion of health drops, the hero will stop attacking the monster and pick up the potion.
class MyHero : AutomaticHero
{
void OnSeeMonster(Monster monster)
{
Attack(monster);
}
void OnItemDropped(Item item)
{
if (item.Name == "Potion of Health")
{
PickUp(item);
}
}
}
One way to solve this is to use a state machine.
//TODO
While with this you can make it so that events don't interrupt each other, if you are attacking a monster and see an item, you want to finish attacking the monster, and then pick up the item. So you need to queue the actions.
One way to do this is to use a queue.
//TODO