Joystiq has you covered with all things Metal Gear Solid 4!

Under The Grid - Building a simple bot in ironpython

Under the GridWelcome to the fourth installment of "Under The Grid", an irregular look at the mechanics underneath Second Life. This time we'll look at the basics of a bot. An automaton in avatar form. Building a bot is hard, right? The sole purview of C++ or C# programmers, you think? Not so. If you're familiar with python, and are willing to take some time to learn the structure of libsecondlife, you can put together simple task oriented bots fairly quickly.

As with any programming task, the more complex the task the more research, writing and testing you will need to do. If you're going to start, start small. In many cases a simple task-oriented bot can automate an annoying task for you, while placing significantly lower demands on Second Life than logging in and doing it yourself. An important aspect of any Second Life automaton is to remember that Second Life is a shared space and set of resources and never to impose more load on the system than an ordinary avatar might. Less, if possible - and it is frequently possible.You'll need several things for this. You'll need to know your python. You'll need to download and build libsecondlife. You'll need ironpython. You'll need to know how to find necessary programming information on libsecondlife on your own. That said, if you can manage those things, read on. If not, you've got a bit more learning to do.

Ironpython is a fast python implementation running on .NET/MONO. It's particular useful features here are that it's python, and thus simple, fast and powerful, and that it is quite capable of calling .NET code and using .NET classes directly. That allows us to connect it directly to libsecondlife.

The code below is a simple example, based on John Hurliman's C# example code. The key to loading libsecondlife is the clr module which is able to load and import libsecondlife functions and classes as a python namespace.

#---CUT--HERE---

import clr
import sys
import time
import string

clr.AddReferenceToFile("libsecondlife.dll")
import libsecondlife

# Replace credentials with the firstname, lastname and password of an SL account
credentials=['BotFirstName','BotLastName','BotPassword']
currentname=""

class EventTracker:
def __init__(self):
self.data=False
def Clear(self):
self.data=False
def Set(self):
self.data=True
def Wait(self):
while not self.data:
time.sleep(0.1)

# Show a UUID in a familiar, friendly format
def format_uuid(id):
return "%s-%s-%s-%s-%s" %(id[0:8],id[8:12],id[12:16],id[16:20],id[20:])

# Handle the results of the people search
def DirQueryHandler(queryid, results):
global currentname
global event
print currentname,format_uuid(results[0].AgentID.ToString())
event.Set()

event=EventTracker()
client=libsecondlife.SecondLife()
client.Directory.OnDirPeopleReply += libsecondlife.DirectoryManager.DirPeopleReplyCallback(DirQueryHandler)

qlist=['Tateru Nino','Torley Linden']

if not client.Network.Login(credentials[0], credentials[1], credentials[2], "example", "yourname@here.com"):
print ("ERROR: " + client.Network.LoginMessage)
sys.exit(0)

for i in qlist:
i=string.strip(i)
currentname=i
(fname,lname)=string.split(i," ")
event.Clear()
queryID = client.Directory.StartPeopleSearch(libsecondlife.DirectoryManager.DirFindFlags.People, fname+" "+lname, 0)
event.Wait()

client.Network.Logout()
#---CUT--HERE---
(The formatting of this code is bound to get messed up. Download the code here)

Replace the three elements of the credentials list with the first name, last name and password of a valid Second Life account. That's your bot. This example logs into Second Life, requests the key/UUID for me, and for Torley, prints them out in a relatively simple formatted fashion, and logs out again once the task is complete.

A lot of what the Second Life protocol does is asynchronous, so you'll want to start with simple examples and rely on simple tools like the EventTracker class above to track completion of tasks and response to requests.

Now, while nobody, least of all me, is offering you any tech support on this, you can learn to do this. It'll take you less effort than learning Greek history or the rise of the merchant classes - but you have to be patient and actually spend the time. Like Second Life, this sort of scripting repays you in proportion to what you put in. Don't put in the work, and you get out what you put in. Nothing.

This is just a start. It shows you how to mesh python runtime with libsecondlife and get an automaton into Second Life, ready to perform tasks. From there you will need to expand your knowledge of the libsecondlife structure and the Second Life network protocols, but all that will cost you is time.
by Tateru Nino

Reader Comments

(Page 1)
General
Arts and Culture (70)
Gridbugs (207)
Live Performance (17)
Machinima (72)
MMO Watch (33)
Op/Ed (53)
Podcasts (21)
SL Blogs (9)
Teaching (57)
Teen Grid (13)
Updates (158)
Events (347)
How-To (52)
News (771)
SL Insider Business (27)
Stories (264)
Comics (18)
Mixed Reality (434)
Linden Lab (356)
Odds and Ends (916)
Just Askin' (96)
Objects
Building (96)
Clothing (38)
Gadgets (71)
Graphic Design (27)
LSL (24)
Economics
Accounts (80)
Business (446)
Linden Dollars (316)
Making Money (79)
Residents
Resident Snapshot (58)
Interviews (125)
Newbies (45)
Places
Great Builds (90)
Educational (115)
Entertainment (110)
Exploration (110)
Shopping (113)

RSS NEWSFEEDS

RESOURCES

Powered by Blogsmith

Other Weblogs Inc. Network blogs you might be interested in: