Second Life has been my new hacking obsession ever since I bought a laptop fast enough to run it. I don’t spend a lot of time socialising in the gameworld, but I am fascinated by the possibilities for makers of new user interfaces, useful virtual objects and playful toys. With every object being scriptable, aware and active, it’s a proving ground for Everyware.
Version 1.10 was released last week, and hidden among the exciting new visual modeling possibilities of shiny rendering and flexible objects was Second Life’s own XMLHTTPRequest: llHTTPRequest. Using asynchronous callbacks, it gives the platform an important new capability: communication with the web on demand. A lot of what we are learning about AJAX makes sense here, in this world of Asynchronous Lindenscriptinglanguage And Some-sort-of-data (ALAS!)
I’ve spent a few hours hacking on some toy objects with this new capability, starting with the mashup de rigeur: Flickr integration. My home in SL now sports a simple picture frame. Touch it and it looks up your avatar name to see what your favourite Flickr tag is, picks a random picture with that tag from Flickr and displays it on its surface. If it hasn’t met you before, it asks you to tell it what tag to use.
Because Second Life is so wonderfully visual, here’s a little demo movie that I recorded with Tom Coates:
how it works
UPDATE: source code is available
LSL is a rather limited C-like scripting language with a couple of interesting features (like its native support for state-machines to respond to events). As far as I’ve been able to work out, it lacks the ability to parse XML or JSON, and has no native associative array type. Objects can store some state, but it is a little fragile in the face of resets. I decided not to try accessing the Flickr API directly, or using local storage. Instead, I created a stateful web companion for my new object with a bit of serverside Rails.
Strings and lists are the most useful datatypes available, and there’s a function similar to perl’s ‘split’ for turning strings into lists. This makes pipe-delimited a reasonable format for simple data. I created the following web resources:
/seen?user=SomeAvatar: Records that the object has sensed the presence of SomeAvatar
/touched?user=SomeAvatar: In response to a ‘touch’ event, consults the database for the user’s tag and asks the Flickr API for a random photo with that tag. Returns a string such as
/set_tag?user=SomeAvatar&tag=sausages: Records SomeAvatar’s favourite tag
The HTTP system is nicely responsive: using the web as my object’s outboard brain added only a tiny bit of latency to the mix. The asynchronous model allowed other processing to continue while waiting for a response. With these URLs ready to respond, I wired up the appropriate Second Life events using llSensorRepeat and sensor for presence, llListen and listen to respond to spoken commands, and touch_start for the physical interface. The llParcelMediaCommandList features are confusing (and only work on land you own, with movie streaming enabled in the client), but I found the source code for Freeview to be a useful reference.
If you’re interested in this code, or would like to see a demo, you can find me from time to time in SL as Matt Basiat.