Saturday, January 28, 2012

Lightbulb Moment!


And I mean that very literally, I really do.

This is a little toy I've been making.  It sits up there above your head, normally dark and invisible.  But, when you indicate that you've had a thought, or an idea, by typing a message in chat that contains those words, it turns visible and lights up to show everyone nearby just how brilliant you are.

The lightbulb itself wasn't all that tough.  Mostly it's cylinders nailed together (bayonet fitting, so much easier than a screw thread), with a filament made of a twisted tube, following the directions in Ayumi Cassini's now-famous guide. The bulb is a sculpty, but it honestly is a piece of piddle making something like that in Rokuro.  Texures?  A glass one from Builders' Brewery, a couple of metal ones from the many, many texture packs I've bought from the USC Texture Tomb, a blank grey for the filament.

The fun part, of course, comes in the scripting.

First off, this thing has to be invisible when it's rezzed, so I defined a "shutdown" function and called it in the on_rez event, along with setting up a listener.  To clean up, I put some code in the attach event to turn off that listener when the thing's detached.  (We know, of course, that there is no "detach" event, but the attach event handler tells you the UUID of the person your object's attached to, and if that is a null key then it isn't attached to anybody, and that's how you find out if an object's just been detached.  Or at least I knew that, and now you do too if you didn't before.)

Anyway.  Once the listener is running, it checks each of your messages (using the llGetSubString function) for occurrences of the strings "thought" or "idea".  This function returns a -1 if it doesn't find the thing it's looking for... so, we can figure that, if we call this once for "idea" and once for "thought", and add the two results together, if they come to anything over -2 then you have said "thought" or "idea" at least once, so we start firing off the stuff to make the thing appear.

This means 1) using llSetLinkAlpha to set the visibility to 1.0 (fully visible) on all components, 2) using llSetLinkAlpha again just on the glass part to set its visibility to 0.5 so you can see through the darn thing, 3) using llLinkParticleSystem on the filament to make it shoot out sparklies, and 4) using llSetLinkPrimitiveParams with PRIM_POINT_LIGHT on the filament in order to make it shed light.  (Identifying the glass bit and the filament inside the linkset is left as an exercise for the alert student.)

You can see, I hope, that doing the "shutdown" function I mentioned earlier pretty much means doing the same things backwards (except we are turning the whole thing invisible, so we don't need to faff around with any special settings on the glass bit there).

The listen event also calls llSetTimerEvent to give a five-second timer.  At the end of those five seconds, the timer event fires... doing what?  Why, first calling our "shutdown" function to turn the lights off and the lightbulb invisible, and secondly calling llSetTimerEvent again to set it to 0.0, or in other words turn it off.

So there we have it.  Stick this on a head-related attachment point (I chose my nose), and you have a lightbulb that pops up and glows whenever you have a brilliant thought or idea, or at least whenever you say you have.  It's a bit of fun, at any rate.

No comments:

Post a Comment