A Step Up In Second Life Scripting: Getting Flexible With llGetInventory
When I started scripting in Second Life, it was pretty iffy. If (string==this) do this. If (string==that) do that. If (string==somethingelse) do something else. If, if, if. It wasn’t bad for simple stuff, but when I wanted a chat-activated script to play a bunch of sounds, give out landmarks, or give out objects, it really bogged me down. First of all, I had to do an “if” statement for every single item I wanted to use.
Second, if I made a single change in the inventory of the item I wanted to use, I had to dig around and match up the script to every script, notecard, sound, animation, or landmark or object inside the item. And last of all, I had to keep the script no modify when I sold it, so people couldn’t change it around. If they wanted a new version I’d have to do custom work and sell them another one! Talk about inconvenient.
I was looking around my favorite SL wiki, RPGStats, and I found some help in an unlikely area: lLGetInventoryType.
What does this seemingly academic LSL command do?
LlGetInventoryType (usually used like llGetInventoryType(“item name”) or llGetInventoryType(stringvariable) will return a number wherever you use it. For example, if you have something llsay(0,llGetInventoryType(“fruitbat”)), GetInventory will tell you two things:
1. If the item is in the scripted object’s inventory (anything besides -1 means that it is), and
2. What type of inventory the named object is:
Number result / Inventory Type
-1 Item does not exist
0 texture
1 sound
3 landmark
5 clothing
6 object
7 notecard
10 script
13 body part
20 animation
21 gesture
Again, this may look a little silly. Say I get a GetInventoryType result saying that “breakdance” is an animation inside my scripted object (20). What can I do with this result?
Here’s where GetInventoryType shines — when you combine it with an If statement.
Let’s say you’ve got a listen state set up with a million if statements inside your scripted object, and it’s lag city.
But there’s still hope. Whatever is said is put into a string called m:
default
{
state_entry()
{
llListen(0,””,NULL_KEY,”hello”);
llListen(0,””,NULL_KEY,”lol”);
llListen(0,””,NULL_KEY,”woo”);
llListen(0,””,NULL_KEY,”yikes”);
llListen(0,””,NULL_KEY,”breakdance”);
llListen(0,””,NULL_KEY,”crazydance”);
llListen(0,””,NULL_KEY,”electric slide”);
}
listen(integer channel, string name, key id, string m)
{if…
if…
if…
if…
if…
if…
and so on}
}
So let’s say you’re goofing around in SL and you want this object to play a bunch of crazy animations when you say the name of one of them.
Instead of having to script your poor fingers to the bone, llGetInventoryType works with string m perfectly. All you need to know is the inventory number for the items you want to use. In this case, animations that exist inside the object cause llGetInventoryType to return a value of 20.
So if you replace all your if statements above with GetInventory, you get this convenient solution:
listen(integer channel, string name, key id, string m)
{if (llGetInventoryType(m)==20)
{ llStartAnimation(m);
}
It’s simple! Whatever comes in as chat message m, the script looks for an item in the object’s inventory whose name matches m. If that item’s inventory type number is 20 (for an animation that is there in the object), then the script plays an animation in the object called m.
So if you have an animation called Crazydance and you don’t feel like fiddling with your attachments, fix up the above script like this:
default
{
state_entry()
{
llListen(0,””,NULL_KEY,””); (since you’re listening for everything you don’t have to use multiple listens!)
}
listen(integer channel, string name, key id, string m)
{if (llGetInventoryType(m)==20)
{ llStartAnimation(m);}
}
}
That’s all there is to it: if your input (chat, notecard, list item, link message) equals a matching object inventory item, do something with the corresponding inventory in the {} of the if statement. Turn a script on or off; play a sound, gesture, or animation; pass another link message; read the associated notecard (I’ll have more on this in a future post); put on or take off some clothing or change a texture. Or even go to a complex script state that does several things for you in one big batch.
Hope you were able to make some use of this scripting shortcut! If you have any comments or questions about this lesson, just leave a comment or email me at [email protected]. Thank you and good luck!
And remember… friends don’t let friends listen on channel 0!
