(This tutorial has been copied with the approval of D1st0rt from D1st0rt's website.)

Tutorial 2: Handling Events

Written by D1st0rt @ Friday, 19 November 2004
Last updated: Monday, 12 June 2006

So you've got a bot, now you need it to do something. For this, the twcore comes with an EventRequester. If you want your bot to do something when an event happens, you need to request the event and then handle the event. Lets make our bot welcome players that come in.

//Make the package name the same as the bot's name so you can spawn it
package twcore.bots.mybot;
//Import all of the TWCore classes so you can use them
import twcore.core.*;

public class mybot extends SubspaceBot
{
    //Requests all of the events from the core
    private EventRequester events;

    //Creates a new mybot
    public mybot(BotAction botAction)
    {
        //This instantiates your BotAction
        super(botAction);
        //Instantiate your EventRequester
        events = m_botAction.getEventRequester();
        //Request PlayerEntered events
        events.request(EventRequester.PLAYER_ENTERED);
    }

    //What to do when the bot logs on
    public void handleEvent(LoggedOn event)
    {
        //Get the data from mybot.cfg
        BotSettings config = m_botAction.getBotSettings();
        //Get the initial arena from config and enter it
        String initial = config.getString("InitialArena");
        m_botAction.joinArena(initial);
        //NOTE: m_botAction is inherited from SubspaceBot
    }

    //What to do when a player enters the arena
    public void handleEvent(PlayerEntered event)
    {
        //Get the name of player that just entered
        String name = event.getPlayerName();
        //Greet them
        m_botAction.sendPrivateMessage(name,"Welcome!");
    }
}

Now, every time a player enters the arena, they will be welcomed by the bot. There is lots of information that comes with events, make sure you check the docs to see what you can do. Here is a list of the events you can request:

Previous Tutorial | Next Tutorial