(redirected from CustomEvent)


Robo Home | Changes | Preferences | AllPages

Robocode lets you define your own events and can help in dispatching event calls to your robot. To create a customized event you make a class that sublasses Condition. The class must define an instance method named "public boolean test()". This method is called by Robocode every turn for all registrered events. The class might look like so:
class Mine extends Condition {
   private AdvancedRobot bot;
   private double x;
   private int y;

   Mine(AdvancedRobot _bot, double _x, int _y) {
      this.bot = _bot;
      this. = _x;
      this.b = _y;

   public boolean test() {
      if (Math.abs(Point2D.distance(bot.getX(), bot.getY(), x, y)) < 50) {
         return true;
      return false;

   public getCoordinates() {
      return new Point2D.Double(x, y);
If you registrer custom events that have a best-before date you must make sure you inregistrer the event. In the above example this is done when the test() method meets the true condition.

To place a mine you register a custom event from inside your bot code:

public void run() {
  registerCustomEvent(new Mine(this, 300, 300));

To check for if you have run into a mine you create a custom event handler:

public void onCustomEvent(CustomEvent event) {
   Condition condition = event.getCondition();
   if (condition instanceof Mine) {
      System.out.println("Boom!!!! Coordinates: " + ((Mine)condition).getCoordinates());
Check out VirtualBullets/VirtualBulletsBot for an example on how custom events can be used to create Waves. And fire those questions you might have on the above information. --PEZ

i have used custom events in a couple of my bots,but nothing like the kind i see PEZ using in VirtualBullets/VirtualBulletsBot and Falcon/WithoutShrinkingTricks. i understand that using them gets rid of the loop that goes through all of the virtualbullets/waves and checks to see if they hit/pass the target. after looking at this code, i don't see how it actually does that. anyone care to enlighten me?--andrew

Yes, it's quite easy actually. Robocode has that loop built in already. It loops through all registered events and runs their test() method. So by registering the Wave as a custom event you know that each turn Robocode will run its test() method. Put your VisitCountStats update inside the test() method or in your onCustomEvent() method, whichever you think is clearer. Aristocles does the former, Tityus does the latter. (At least 0.9.1 did, 0.9.6 is a failed attempt to make an inproved Aristocles. Look at the source code of those bots too if you like. -- PEZ

Good coding practice would dictate you do it in the event handler. No actions should be taken in a test method. Unless you are trying to make a small bot, and it's smaller in the test method, do it properly. -- Tango

The funny thing is that in Tityus it's smaller to do it in the event handler. But in Aristocles it's the other way around. CodeSize management is voodoo. Damn cool voodoo, but still voodoo. -- PEZ

My bot takes all sorts of action in the test method, even though I know it shouldn't, its just so darn convientient thou (other then you can't directly access the built in eventStorage (like you would a Vector or a ArrayList).) -- Chase-san

Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited November 17, 2006 1:27 EST by Chase-san (diff)