How to create a basic libompenmv bot?

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

Contents

Prerequisites

The following prerequisites are required in order for you to create your first bot by following this tutorial.

Real World

  • This tutorial assumes that you were able to successfully build LibOpenMetaverse! If you have not compiled libomv, please follow the instructions on the Getting Started wikipage.
  • Also, if you have no experience with the C# (c-sharp) programming language, I highly recommend that you stop now and please follow the list of links to csharp tutorials located here.
  • A copy of the libomv source code; you can get this by exporting the trunk from SVN or downloading the binarys from the latest release build
  • The following tutorial uses Visual C# Express for Windows XP/Vista.. You can download this IDE for free at Microsoft's website. Feel free to incorporate this tutorial for other development environments in this wiki if you have spare time.

Second Life

  • An active, alternate account for use as a bot on the Second Life grid. You can register a new account at [1].. and yes, registration fees (if they exist) do apply.
  • The grid has to be up and logins must be enabled for residents when you run your bots.

Getting Started

Step One

Step One


'Open the OpenMetaverse solution

Start up Visual Studio. For VS 2010, go to "File > Open Project..." For VS 2008, go to "File > Open > Project/Solution..." Find and open the OpenMetaverse solution.

Step Two

Step Two


Right-click the Solution icon

Right-click the Solution icon in the "Solution Explorer" tab. This opens up a context menu. Click on "Add > New Project..."


Step Three

Step Three


'Preparing your new project

At this dialog, create a "Console Application," and call it "MyFirstBot". Then press OK.

At this point, if you're using VS 2010, then in the Solution Explorer, under "MyFirstBot," double-click "Properties." Under the "Application" tab, set "Target Framework" to ".NET Framework 3.5." (If you're using VS 2008, you can skip this step.)

Step Four

Step Four


Gotta add your references!

In the Solution Explorer, right click the "References" button in your MyFirstBot project and click "Add Reference..."


Step Five

Step Five


'Selecting the references

At this dialog, navigate to the "Projects" tab and then select "OpenMetaverse" and "OpenMetaverse.Types" from the listing. Then press OK.

Finished

Finished


Congrats! If you got this far, you were able to successfully prepare Visual Studio for our first libomv bot.

If you had problems, try starting over, or talk to us in IRC or on the mailing list for help.

The Code

  • I like to present the code as a whole and step through each line when I am teaching.. so here is the code for a libsl bot that logs into the SL Grid and says "Hello World" before it finally logs out
  • Deatos has ported this over to vb.net the code for vb.net is shown below the c# code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenMetaverse;
 
namespace MyFirstBot
{
    class MyFirstBot
    {
        public static GridClient Client = new GridClient();
 
        private static string first_name = "First";
        private static string last_name = "Last";
        private static string password = "password";
 
        static void Main(string[] args)
        {
            Client.Network.LoginProgress += new EventHandler<LoginProgressEventArgs>(Network_LoginProgress);
            if (Client.Network.Login(first_name, last_name, password, "My First Bot", "Your name"))
            {
                Console.WriteLine("I logged into Second Life!");
            }
            else
            {
                Console.WriteLine("I couldn't log in, here is why: " + Client.Network.LoginMessage);
                Console.WriteLine("press enter to close...");
                Console.ReadLine();
            }
        }
 
        static void Network_LoginProgress(object sender, LoginProgressEventArgs e)
        {
            if (e.Status == LoginStatus.Success)
            {
                Console.WriteLine("I'm connected to the simulator, going to greet everyone around me");
                Client.Self.Chat("Hello World!", 0, ChatType.Normal);
                Console.WriteLine("Now I am going to logout of SL.. Goodbye!");
                Client.Network.Logout();
                Console.WriteLine("I am Loged out please press enter to close...");
                Console.ReadLine();
            }
        }
    }
}

The references

using OpenMetaverse;

This tells the compiler to include the contents of the OpenMetaverse librarys when compiling this file, put this at the top of all files using any of the OpenMetaverse classes

The GridClient Class

public static GridClient Client = new GridClient();

This line is probably the most important line in any OpenMetaverse application. It contains all of the functions that are required for your bot to communicate with the Second Life grid. Usually this variable is named "Client," but you can call it whatever you want.

Defining some variables

 
private static string first_name = "First";
private static string last_name = "Last";
private static string password = "password";
 

This step is optional, but I included it for clarity later on. I basically seperated the bot's first and last name into two variables and also put the password in a variable as well. Please note that this method isn't secure at all, and you should never include the details of a SL account in your code if you want to distribute your application. It is fine if you are the only one that will use the application

The Main Event

This block of code is run when the program starts, this is basic C#.. if you didn't know this, please read up on C# before you continue.

  • Please note that libsecondlife loves threads, so the program actually doesn't terminate until you log your bot out of Second Life.. more on that later.

The LoginProgressEvent

Client.Network.LoginProgress += new EventHandler<LoginProgressEventArgs>(Network_LoginProgress);

The LoginProgress Event (found under the Network class in your GridClient variable) is defined here.. Basically, during the login process it will call our Network_LoginProgress method (which I defined below the Main block). When that method is called with e.Status set to LoginStatus.Success we are successfully logged in.

Logging In

if (Client.Network.Login(first_name, last_name, password, "My First Bot", "Your name"))

We are all set up by now.. we defined the SecondLife variable ('client') and we defined what the bot should do when it logs in (OnConnected .) Now it is time to log the bot into the grid. This is easy, just use the Login method found under the Network class.

  • The Login() method returns a boolean: true resembles a success and false resembles an error.
  • Replace "Your Name" with either your Real Life name, your SL Name, or your email address. This is sent to LL as an extra on login, but to my knowledge, they do not record this data as of May 2007.
  • Replace "My First Bot" with the name of your application.. This is sent to LL as an extra on login.

Error?

Console.WriteLine("I couldn't log in, here is why: " + Client.Network.LoginMessage);

A useful property found in the Network class is "LoginMessage." On an error, this string is filled with the error details. If there were no errors at login, the LoginMessage will contain the Message of the Day.

Lets Chat!

Client.Self.Chat("Hello World!", 0, ChatType.Normal);

In the Network_OnConnected method, this line of code is executed. Here are the parameters:

  • the message (string)
  • the channel (integer) [use 0 for public chat]
  • the chat type (Enum).. The most commonly used chat types are:
    • ChatType.Normal = Regular Chat, 20meter range
    • ChatType.Whisper = Whisper Chat, 10meter range
    • ChatType.Shout = SHOUTING, 100meter range
  • NOTE: In earlier versions of libsl ChatType was defined under MainAvatar.ChatType

Logging out

Client.Network.Logout();

Now that we are done, lets log out.. Fairly simple, eh? More information on logging out (and problems logging out) can be found here: Logging Out

Running your bot

Now that you spent all that time writing your first bot code, it is time to compile and run the code for the first time.

Step One

Step One


'Setting project as the startup project

Right click your project in the solution explorer and click "Set as Startup Project"

Step Two

Step Two


Debug Run

Go to the Debug menu at the top of Visual Studio and click "Start Debugging" - If everything went according to plan, you should see a windows console pop up and text flying across that screen.. Also, you should see your bot log in and say "Hello World" in Second Life.

Need More Help?

It's OK! I pretty much guarantee everyone will have a problem when it comes to creating a LibOmv bot sometime in the future. Send an email to the mailing list or see if anyone is around in IRC to get some more help.