Friday 22 February 2008

Event handlers

The fascinating details of JADE's event handlers started another mental ball rolling.

The post specifically (but not exclusively) concerns 'pluggable' enhancements to equipment, which in many games manifest themselves as prefixes (flaming pig-poker, holy maguffin) and suffixes (stick of southwesternesse, crown of monobrow inducement).

Of course, the general implementation of event handling using something akin to Chain of Responsibility is a good and worthy idea. But the specifics of how to do it in a general fashion made me wonder about a few things, and wondering about things makes me note them down in order to come back later and decide there is no gold amongst the copious dross of my thoughts.

First, a rough enumeration of game events we might want to handle on a creature in the game world:
  • Taking damage
  • Being healed
  • Attacking
  • Damaging
  • Magic use
  • Receiving a status effect
  • Being stripped of a status effect
  • Inflicting status effects
  • Removing/putting on equipment
  • Consuming or using items (potions/wands/rods and equivalents)
  • Being moved or moving (physically or via teleportation, voluntary or involuntary)

Quite a few, and there'll be plenty I haven't thought of. But they're all relatively straightforward. The bit that causes me to furrow my brow is the context for these events. It seems the event handlers need to know some specifics as well as having access to the world model, so they'll all need a packet of context data more or less unique to the event class.

Another thing which causes me to scratch my head is the possibility for recursive events. A spell which reflects some portion of damage taken, for example, could go horribly wrong if two mages using it start fighting. I don't really want to make any assumptions about the depth of event propagation though, so I think this is just another pitfall to edge around rather than something to code against.


Thankfully Scala's pattern matching with case classes should fit this kind of event model quite well, and provide a natural way to bundle up context data. I'll have to try it at some point.

No comments: