How to create a basic libSL bot

From libsecondlife

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 libsecondlife! If you have not compiled libsl, 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 libsecondlife source code; you can get this by exporting the trunk from SVN or downloading a zipped copy from the 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

Step One


'Open the libsl solution

Start up VS2005 and go to File > Open Project...

Step Two
Step Two

Step Two


Right Click the solution icon

Right click the soltion icon in the "Solution Explorer" tab. This opens up a context menu, go to Add > New Project...


Step Three
Step Three

Step Three


'The New Project window

At this dialog, create an "Empty Project," and call it "MyFirstBot". Then press OK

Step Four
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

Step Five


'Selecting the libsecondlife reference

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

Step Six
Step Six

Step Six


Creating the main source file

Now we need to create a file to write our bot's code in, this can be done by right clicking the project's icon in the solution explorer and clicking on Add > New Item...


Step Seven
Step Seven

Step Seven


'Creating the main source file

At this dialog, select the "class" icon and name the file "MyFirstBot.cs" .. then press Add to create the file

Finished
Finished

Finished


Congrats! If you got this far, you were able to successfully prepare Visual Studio for our first libsl 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.Text;
using libsecondlife;
 
namespace MyFirstBot
{
    class MyFirstBot
    {
        public static SecondLife client = new SecondLife();
 
        private static string first_name = "First";
        private static string last_name = "Last";
        private static string password = "password";
 
        public static void Main()
        {
            client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
            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);
            }
        }
 
        static void Network_OnConnected(object sender)
        {
            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();
        }
    }
}
 

VB.NET Hello World example

 
' hello world example ported to vb.net by deatos
' Second life: Deatos Tigerpaw
' email: deatos [.at.] rock.com
' I did this to kickstart the development of sl bots for vb devs
' This code comes with no support and the author is at no liability for any part of what you do with it
' I assume you already have an understanding of .net and visual basic
' i have went through and commented this code to help make it easier to understand ~deatos
 
Imports libsecondlife, libsecondlife.NetworkManager, System, System.Collections.Generic, System.Text 'here we import the libs dont forget to add references
Module Module1
' slclient will be used to handle our secondlife calls
    Dim slclient As New SecondLife(), first_name, last_name, password As String 'setup the variables
 
    Sub Main()
        AddHandler slclient.Network.OnConnected, AddressOf Network_OnConnected ' add a handler/callback to handle onconnected events
        If slclient.Network.Login(first_name, last_name, password, "My First Bot", "Your name") Then ' login and check if login was successful
            Console.WriteLine("Logged in") 'then print logged in
        Else 'but if not successful
            Console.WriteLine("I couldn't log in, here is why: " + slclient.Network.LoginMessage) ' print out a reason, loginmessage returns a string
        End If
    End Sub
 
    Sub Network_OnConnected(ByVal sender As Object)   ' here is the handler for onconnected only called if connection is successful
        Console.WriteLine("I'm connected to the simulator, going to greet everyone around me") 'tell the user we are connected
        slclient.Self.Chat("Hello World!", 0, ChatType.Normal) '  heya lets say hello to the world
        Console.WriteLine("Now I am going to logout of SL.. Goodbye!") 'notify user we are logging out
        slclient.Network.Logout() ' here is how you logout
    End Sub
End Module
 
 

Please note that the csharp code is outline below i added the vb.net code as an example the comments explain it enough to where i do not feel a need to edit below this line. However if someone would like to explain it better feel free to do so. And one last thing do not forget to set the first_name last_name and password variables

 
first_name = "firstname"
last_name = "lastname"
password = "password"
 

The reference

using libsecondlife;

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

The SecondLife Class

public static SecondLife client = new SecondLife();

This line is probably the most important line in any libsecondlife 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 OnConnected Event

client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);

The OnConnected Event (found under the Network class in your SecondLife variable) is defined here.. Basically, whenever your bot connects to a simulator and marks its precense there, it will fire the Network_OnConnected method (which I defined below the Main block)

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

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

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 libsl 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.