RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Writing Commerce Scripts in Second Life : Page 2

Millions of dollars change hands in Second Life. Find out how to create scripts to sell items in your own Second Life store.

Build a Vendor Script
You can think of a vendor script as being similar to a kiosk. The script displays a set of pictures of items for sale. Most scripts let avatars browse through the items using Forward and Back buttons. Figure 2 shows how such kiosks typically look.

Figure 2. Using a Vendor Script: The vendor script lets avatars scroll through images of items offered for sale in a kiosk-like manner, which saves space compared to building new stores or renting space in a mall to display the actual items.
A configuration notecard controls the vendor script. In Second Life notecards are similar to text files, and they're often used to configure scripts. For example, here's the content of the configuration notecard for the vendor script shown in Figure 2:

   Encog's Red Car
   Super Car
   Encog's Boat
The lines in the configuration notecard occur in groups of three. The first group of three is:

   Encog's Red Car
The first line specifies the name of the texture (the image of the item) to display when that item becomes current. The second line specifies the name of the item, and the third line specifies the price of that item, in Linden Dollars. Each object being sold must be in the vendor's object inventory.

Three scripts are required to run the vendor—the main script and two scripts to control each of the arrows.

Main Vendor Script
The main vendor script does most of the work. The Forward and Back buttons each contain a navigational script, but those simply tell the main script that it should navigate. Listing 1 shows the main vendor script in full; the rest of this section contains a detailed explanation of the code. You can download all the scripts for this article to experiment with them yourself.

The main vendor script begins by declaring some variables:

   integer index;
   string notecardName;
   key notecardQuery;
   integer notecardIndex;
   list notecardList;
   integer price;
   string itemName;
Table 1 shows the purpose of each variable:

Table 1: Main Script Variables: Here's a list of the global variables used in the main vendor script.
Variable NamePurpose
indexHolds the index of the item currently being viewed.
noteCardNameHolds the name of the notecard being read.
notecardQueryHolds the query used to read the configuration notecard.
priceHolds the price of the current item.
itemNameHolds the current item's name.

The displayItem function updates the vendor object to display a newly selected item. It begins by obtaining the texture from the list. The first three lines of the displayItem function read three items from the configuration notecard—the texture, name, and price:

       string textureName = llList2String(notecardList,index*3);
       itemName = llList2String(notecardList,(index*3)+1);
       string p = llList2String(notecardList,(index*3)+2);
Next, the script converts the price to an integer, stores it in the price variable, and then displays the price. It sends a message to all linked objects so the Buy button can display the text:

       price = (integer)p;
       string display = itemName + "\nL$" + p;
       llMessageLinked(LINK_ALL_OTHERS , 0, ":"+display, NULL_KEY);
Finally, it displays the texture, and sets the purchase price. Second Life textures are used for many purposes; however, here the texture is simply an image of the item being sold:

       llSetLinkPrimitiveParams(5,[PRIM_TEXTURE, 1, 
         textureName, <1,1,1>, <0,0,0>, 0 ]);
       llSetPayPrice(PAY_HIDE, [price, PAY_HIDE, PAY_HIDE, 
Second Life objects are state driven, and all begin in the default state. Second Life fires a state_entry event whenever the state changes. Whenever the default state fires, the script checks to see if any items are loaded. If not, the script enters the loading state, which you'll see in the next section:

           if( llGetListLength(notecardList)==0 )
               notecardName = "Config";
               state loading;
Otherwise, (if items are loaded) the script displays the first item:

               index = 0;
The navigation buttons send messages when an avatar pushes them. The link_message event handler processes these messages:

       link_message(integer sender_num, integer num, 
          string str, key id)
Just as you would expect, the Back button moves to the previous item, while the Forward button moves to the next item:

           if( str=="back" )
           if( str=="forward" )
The script displays the items in a circular fashion; if the last item has been passed, then it wraps around to the first item:

           if(index>=(llGetListLength(notecardList)/3) )
               index = 0;
Similarly, if a user backs up past the first item, the script wraps around to the last item:

           if(index<0 )
               index = (llGetListLength(notecardList)/3);
Finally, it displays the current item:

When a user buys an item the script calls the money event handler, and—if the user paid enough—gives the item to the purchaser:

       money(key id, integer amount)
           if( amount>=price )
               llSay(0,"Thanks for your purchase!");
Now you're ready to see how to load items.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date