[This is preliminary documentation and is subject to change.]

GreeterBots are one of the most fundamental examples of using the Active Worlds SDK. They demonstrate the usage of attributes, methods, and events.

Greeter Bot

This greeterbot example is compiled as a console application. It takes input from the user about their citizen number, password, and world. It spawns a greeterbot at the GZ of the specified world and begins greeting users via whispers. This example demonstrates how to use events with the SDK.

C# Copy
using System;
using AW;

namespace CSGreeter
{
    class GreeterBot
    {
        //Private static Instance variable named greeter.
        private static Instance greeter;

        static void Main(string[] args)
        {
            //Declares several local variables
            int owner;
            string password;
            string world;

            //Prompt the user for their citizen number
            Console.Write("Enter citizen number: ");
            owner = int.Parse(Console.ReadLine());

            //Prompt the user for the privilege password
            Console.Write("Enter privilege password: ");
            password = Console.ReadLine();

            //Prompt the user for the world to greet in
            Console.Write("Enter world name: ");
            world = Console.ReadLine();

            //As of Beta 5, you are now required to check for exceptions coming from the SDK
            //This means that ALL calls to methods associated with the Instance class must 
            //be encased in a try block with the appropriate catch block for InstanceException
            try
            {
                //Instantiate a new greeter instance
                greeter = new Instance();

                //This is where C# differs from VB when handling events.  C# must use delegates
                //to handle events.  The below statement adds a new delegate the EvenAvatarAdd
                //to handle when this event is triggered.
                greeter.EventAvatarAdd += new Instance.Event(HandleAvatarAdd);

                //Set the login attributes and log the bot into the universe
                greeter.SetString(Attributes.LoginName, "GreeterBot");
                greeter.SetString(Attributes.LoginPrivilegePassword, password);
                greeter.SetInt(Attributes.LoginOwner, owner);
                greeter.Login();

                //Have the bot enter the specified world
                greeter.Enter(world);

                //Have the bot change state to 0n 0w 0a
                greeter.SetInt(Attributes.MyX, 0); //X position of the bot (E/W)
                greeter.SetInt(Attributes.MyY, 0); //Y position of the bot (height)
                greeter.SetInt(Attributes.MyZ, 0); //Z position of the bot (N/S)
                greeter.StateChange();

            }
            catch (InstanceException ex)
            {
                Console.WriteLine(ex.Message);
            }

            //Enter an infinite loop to dispatch events.
            while (Utility.Wait(0) == 0) ;
        }

        //This method handles avatars entering the world.  The greeter has had a delegate
        //referencing this method created and added to its EventAvatarAdd event's list of
        //delegates.
        static void HandleAvatarAdd(Instance sender)
        {
            try
            {
                //Store session and name for later use.  Generally storing attributes
                //at the start of an event is best practice when operating with the SDK
                int session = sender.GetInt(Attributes.AvatarSession);
                string name = sender.GetString(Attributes.AvatarName);

                //Send a whisper to the avatar entering the world
                sender.Whisper(session, "Greetings, {0}!", name);
            }
            catch (InstanceException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
		
Visual Basic Copy
Imports AW

Module GreeterBot
    'Declare a variable named greeter with the type of Instance.
    'For simplified event handling (no use of delegates) declare it using
    'the WithEvents keyword
    Private WithEvents greeter As Instance

    Sub Main()
        'Declare some local variables used to log the bot into the AW universe.
        Dim owner As Integer
        Dim password As String
        Dim world As String

        'Prompt the user for their citizen number
        Console.Write("Enter citizen number: ")
        owner = Integer.Parse(Console.ReadLine())

        'Prompt the user for the privilege password
        Console.Write("Enter privilege password: ")
        password = Console.ReadLine()

        'Prompt the user for the world to greet in
        Console.Write("Enter world name: ")
        world = Console.ReadLine()

        'As of Beta 5, you are now required to check for exceptions coming from the SDK
        'This means that ALL calls to methods associated with the Instance class must 
        'be encased in a Try block with the appropriate Catch for InstanceException
        Try
            'Instantiate a new instance
            greeter = New Instance()

            'Set the login attributes and log the bot into the universe
            greeter.SetString(Attributes.LoginName, "GreeterBot")
            greeter.SetString(Attributes.LoginPrivilegePassword, password)
            greeter.SetInt(Attributes.LoginOwner, owner)
            greeter.Login()

            'Have the bot enter the specified world
            greeter.Enter(world)

            'Have the bot change state to 0n 0w 0a
            greeter.SetInt(Attributes.MyX, 0) 'X position of the bot (E/W)
            greeter.SetInt(Attributes.MyY, 0) 'Y position of the bot (height)
            greeter.SetInt(Attributes.MyZ, 0) 'Z position of the bot (N/S)
            greeter.StateChange()

            'Catches any exceptions thrown by the wrapper and outputs them.
        Catch ex As InstanceException
            Console.WriteLine(ex.Message)
        End Try

        'Enter an infinite loop
        While Utility.Wait(0) = 0
        End While
    End Sub

    'Here we declare a private method for handling avatars entering the world
    'The best method for handling events with the SDK is now to use Delegates
    'like one would in C#, but rather to use the Handles keyword.  Here the
    'method handles the EventAvatarAdd method of the instance greeter.
    '
    'There are several other ways to handle events like this in VB, but this
    'is the simplest way to do so.
    Private Sub HandleAvatarAdd(ByVal sender As Instance) Handles greeter.EventAvatarAdd
        Try
            'Store session and name for later use.  Generally storing attributes
            'at the start of an event is best practice when operating with the SDK
            Dim session As Integer = sender.GetInt(Attributes.AvatarSession)
            Dim name As String = sender.GetString(Attributes.AvatarName)

            'Send a whisper to the avatar entering the world
            sender.Whisper(session, "Greetings, {0}!", name)
        Catch ex As InstanceException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Module