LSL: A notecard giver
You can set your land to play an Internet radio station that others can listen to, but you can't stream Spotify to your visitors. I needed something to hide the gap behind my Botanica Nevermore fireplace, which I had rotated to fit into a corner. I thought an antique radio would work well on the mantelpiece and, ideally I would like it to play tracks from the Spotify Wednesday Soundtrack.
I bought a Copy/Modify radio from Velvet Whip and wrote a very simple Linden Scripting Language (LSL) script that would allow the radio to give a notecard on touch.
Every script in LSL needs at least one state: default, although others are possible. The code that is run in default is sandwiched between { and }.
To get something to happen when you touch the object you use:
touch_start(integer num_detected)
Note the touch_start is indented as it is part of default. num_detected is what's known as a variable. You can call this variable anything you like, if you read other scripts you might see n used or total_number. Basically this is an integer that tells the object how many avatars have clicked the object. The value is usually 1, but in busy areas it could be higher. In many scripts this variable is not used and we won't here either.
After touch_start(integer num_detected), there is another pair of { } containing the code we want to run when the object is touched.
The next line uses the llSay function. The 0 means that the message goes out on the Public Channel, so that all objects and avatars within 20m can hear it. The message that you want the object to broadcast goes between " and ". The message channel and the message itself are wrapped in ( and ) and then the whole line finishes with ;
The code that actually gives the notecard is in the next line. The llGiveInventory function needs two parameters: the destination prim or avatar and the inventory item to be given.
The call to llDetectedKey(0) returns the unique identifier within Second Life of whoever first touched the object. Note that if you wanted the second one to be given something as well you'd call llDetectedKey(1) etc. I'm not expecting my house to become a high traffic area so it's safe to leave it as llDetectedKey(0) and if someone is unlucky enough to click it simultaneously with another avatar, they can just touch again a second or so later.
To tell llGiveInventory which inventory item to give is simple in this case. As you can see from the Contents tab of the Build window, there is this script, Notecard Giver Script, and the notecard itself called OWWVW Radio Spotify Links. Things would be more complicated if we wanted to offer the user a choice. Here though we just call the llGetInventoryName function - we have to do this in much the same way that we need to use llDetectedKey. Each time a notecard is saved it actually has a new unique identifier. It might look the same to us reading the human-readable name that is displayed to us but that's not the name used by LSL. The llGetInventoryName has two parameters itself: the inventory type - in this case INVENTORY_NOTECARD - and which inventory item you mean. The inventory items are sorted alphabetically and assigned numbers starting with 0 for the first one. There is only one notecard here do it must have a value of 0.
If you want to learn more about LSL I recommend investigating the LSL Portal, where you can find plenty of tutorials and sample scripts.
♒ Aqua ♒
Comments
Post a Comment