Monday, July 2, 2012

More Second Life


Following my last post about programming in Second Life, today I will create a Note Card Giver, a Post Box System and an Object which interacts with a web page.

Note Card Giver

I chose to build a note card giver which gives note cards to avatars who pass by the object. Another option for this is to give a note card to who touches the note card giver. First I created an object to be able to give note cards. I did not create an elaborate object as this was not the purpose of the task. Then I created a note card in the inventory which will be given by the note card giver. The note card was also added to the contents of the note card giver. I added a script to the note card giver to start handing note cards. In the first part of the script I created a number of variables


string notecard = "Note1";
integer freq = 1;

integer maxList = 100;

list given;


The string holds the name of the note card which will be given to the passing avatar. freq is the number which the sensor uses as a frequency. It is the number of seconds which the sensor waits until he checks again for an avatar. The max list is the number of avatars which can be stored in the list of already contacted avatars. Then, I modified the code of the object of the state_entry().


state_entry()
    {

        llSensorRepeat("", "",AGENT, 5, PI, freq);

        llSetText("", <1.0, 1.0, 1.0>, 1.0);
    }


The code above is to create a sensor to check for avatar movement in the area. The number 5 is the radius in metres to check around. PI is used to make the sensor work in 360 degrees.




       sensor(integer num_detected)
    {

        integer i;

        key detected;
      
        for(i=0;i<num_detected;i++)
        {
            detected = llDetectedKey(i);
          
            if( llListFindList(given, [detected]) < 0 )
            {
                given += llDetectedKey(i);
              
                llGiveInventory(detected, notecard);
                if (llGetListLength(given) >= maxList)
                {
                    given = llDeleteSubList(given,0,10);
                }                               
            }
        }               
    }


Figure 1: Note Card given to me by Object

The code above is the part which sends the note cards. When the sensor detects an avatar, the function sensor(integer num_detected) is called. This function contains a for loop to go through all the detections. When an avatar is detected, the list is checked to see if the avatar is in the list already. An avatar might have already passed near this note card giver. If the avatar is not in the list, his name will be added to the list and a note card will be given to him. llGiveInventory() is the function used by Second Life to give the note card to the avatar. llGetListLength() checks the length of the list and if the list reaches the maximum, the function llDeleteSubList(given,0,10) deletes the first 10 avatar names of the list.


Figure 2: The Contents of the Note card

Post Box System

This was quite not difficult to create, but to test it, I had some problems. I created a very simple object to test the post box system. I did not create an elaborate post box because it was not the scope of the task. The code for this post box system is shown below:


default
{

    state_entry() {

        llAllowInventoryDrop(TRUE);
    }
    touch_start(integer total_number) {
        llSay(0,"Create a notecard and drop it into the mailbox");
    }
  
    changed(integer mask) {
        if (mask & (CHANGED_INVENTORY | CHANGED_ALLOWED_DROP)){



            integer i;

            for (i=0; i<llGetInventoryNumber(INVENTORY_ALL); i++) {
                string name = llGetInventoryName(INVENTORY_ALL, i);
                integer type = llGetInventoryType(name);
                if (type != INVENTORY_SCRIPT) {
                    integer perms = llGetInventoryPermMask(name, MASK_OWNER);
                    if ((perms & PERM_TRANSFER) == PERM_TRANSFER &&
                        (perms & PERM_COPY) == 0) {
          
                              llSay(0, "Thanks for the "+name);
                              llGiveInventory(llGetOwner(), name);
                        llRemoveInventory(name);
                    } else {
                              llSay(0, "Sorry, "+name+" isn't nocopy, trans");
                              llRemoveInventory(name);
                    }
                }
            }
        }
    }



Figure 3: Another avatar put a test note card in my post box system

The state entry part allows people to drop items into the post box object. The touch_start part displays a message when the object is touched. It displays a message to Create a notecard and drop it into the mailbox. The first part of the changed function checks for changes in the inventory and the changed allowed drop state. Then the function loops inside the inventory items and checks if an object is not a script in the inventory mask of the owner of the object with respect to the item. If the object dragged is not a copy, it is moved from the post box to the inventory. If it is a copy, it is not copied to the inventory and it is then removed from the post box. The avatar did not want to give me full permission because he was afraid of something. But he still was kind enough to test the system for me.


Figure 4: The note card given by the post box system

Object Interacting with a Website

For this part, I wanted to show how easy it is to display the content of a website in Second Life, without knowledge of the LSL scripting language. I created a kind of screen using a large object of about 10m by 10m. Then I used a texture to load the website. I selected one face of the screen and clicked on the plus sign at the bottom of the object window. In the URL, I added a URL of a youtube video. I removed the Auto Scale and entered the size of the video, 640x360. Then, I played a bit with the texture offset to center the video across the object. Below you can see the avatar in front of the BIG Screen. However the flash player is not installed within second life and the video cannot be seen. Another screen was thus created to show the content of another website. www.google.com is shown on the other screen.


Figure 5: Both screens showing two web pages in Second Life

After all, it was not that difficult to program objects in Second Life, you just need some time and patience :D

No comments:

Post a Comment