Debugging Your Code

An Example: Scratch Spell

OK, let's take a look at some sample code. This is a fairly sophisticated example. You should make sure you've gone through the Wiz Tutorials before continuing.

This example is a little spell that makes the target do various mildly embarrassing things every 20 seconds. It works by setting a repeating timer, and whenever the timer goes off, it makes the target do something random from a list.

"""
scratch.py

An example spell, for demonstrating logging. There is
no logging code in this version of the spell.

Copyright 2003 Cabochon Technologies, Inc.
Author: Steve Yegge
"""

from java.lang import String
from wyvern.lib import Timed, Range, Kernel
from wyvern.lib.classes.magic import Spell

class scratch(Spell, Timed):

def initialize(self):
self.super__initialize()
self.setIntProperty('lore', 1)
self.setProperty('short', 'scratch')

def getArt(self):
return self.INCANTATION

def getElement(self):
return self.SPIRIT

def start(self):
# look for target, or use caster if there isn't one
self.target = self.findTargetObject(1)
if (self.target == None):
return

self.timer = Kernel.setRepeatingTimer ( 20000, self )

self.makeDispellable()

self.getAgent().message('You cast scratch on ' + str(self.target))

def timerExpired(self):
target = self.target
roll = Range.percent()
pronoun = target.getGenderString()
name = target.getName()

if roll < 20:
target.message ( "You scratch yourself." )
target.broadcast ( name + " scratches " +
pronoun + "self vigorously." )
elif roll < 40:
target.message ( "You rub your nose." )
target.broadcast ( name + " rubs " +
pronoun + " nose for a long while." )
elif roll < 60:
target.message ( "You scratch your ear." )
target.broadcast ( name + " sticks " +
pronoun + " finger in " + pronoun +
" ear and scratches an itch." )
elif roll < 80:
target.message ( "You suddenly belch." )
target.broadcast ( name + " belches loudly, " +
"and looks very embarrassed about it." )
else:
target.broadcast ( name + " smells terrible today." )

def dispel(self):
self.super__dispel()
Kernel.killTimer(self.timer)
self.target.message ( "You suddenly feel more sophisticated." )

def __repr__(self):
return self.getProperty('short')

def toString(self):
return self.__repr__()

def getSpellDescription(self):
return ("This " + self.getDescString() +
" lowers the target several rungs on the Social Ladder.")

First this spell finds a target object. If you type "cast scratch on muffin", and the player Muffin is standing next to you, then Muffin will be afflicted with the scratch. If Muffin is not there, YOU will be afflicted. That's what happens when you pass 1 (i.e, true) to Spell.getTargetObject().

Then the spell sets a repeating timer to go off every 20 seconds (20000 milliseconds), and when it goes off, it issues different messages to the target and to everyone around the target. Not much to it.

This version of the spell should work correctly, but if you were writing it from, er, scratch, then you might want to put in some logging statements to see what's going on inside the spell. Let's see how to do that, in the next lesson.

<< Previous Chapter Next Chapter >>