Don't click here unless you want to be banned.

LSL Wiki : LSL101Chapter6

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl803.us.archive.org
<Int - Fo - Ch1 - Ch2 - Ch3 - Ch4 - Ch5 - Ch 6 - App1 - App2>

Chapter 6: The Simple Poseball


Poseballs? Posecubes?



Now that all that is out of the way, we can write a real script that might be useful: a poseball.

Poseballs have been around as long as custom animations in Second Life. Simple in design, these little items require four parts:

1) A sit target
2) A way to show/hide the poseball
3) An actual animation
4) A way to animate the connected avatar

This simple tutorial will help you create a poseball using a default animation.



Starting Once Again
Create a new script. We should be back to this again:

default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}



First, we can slice into the state_entry event. Since we need a sit target, let's add one. Let's also add some text for good measure:

default
{
    state_entry()
    {
        llSitTarget(<0,0,1>,<0,0,0,1>); // Set the target one meter above the center of the prim
        llSetSitText("Pose!");
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}



Next, we need a way to make the poseball appear and disappear. We can do this easily. Using llSetLinkAlpha, we can make the entire object appear or disappear with a simple touch! Let's try a boolean variable this time.

integer hidden = FALSE; // Stores whether the object is visible

default
{
    state_entry()
    {
        llSitTarget(<0,0,1>,<0,0,0,1>); // Set the target one meter above the center of the prim
        llSetSitText("Pose!");
    }

    touch_start(integer total_number)
    {
        if(hidden)
        {
           hidden = FALSE;
           llSetLinkAlpha(LINK_SET,1,ALL_SIDES);
        }
        else
        {
           hidden = TRUE;
           llSetLinkAlpha(LINK_SET,0,ALL_SIDES);
        } 
    }
}



Good! Next we need an event that will be called when an avatar "sits" on the object. We can use the changed event, along with the constant CHANGED_LINK. We can also use llAvatarOnSitTarget to figure out who we want to animate.

integer hidden = FALSE; // Stores whether the object is visible

default
{
    state_entry()
    {
        llSitTarget(<0,0,1>,<0,0,0,1>); // Set the target one meter above the ground
        llSetSitText("Pose!");
    }

    changed(integer change)
    {
        if(change & CHANGED_LINK) // If someone has sat on, or "linked," to this prim...
        {
            key agent = llAvatarOnSitTarget();
            if(agent)
            {
                llStopAnimation("sit");
                llStartAnimation("stand");
            }
            else
            {
                llStopAnimation("stand");
            }
        }
    }

    touch_start(integer total_number)
    {
        if(hidden)
        {
           hidden = FALSE;
           llSetLinkAlpha(LINK_SET,1,ALL_SIDES);
        }
        else
        {
           hidden = TRUE;
           llSetLinkAlpha(LINK_SET,0,ALL_SIDES);
        } 
    }
}



Great! Now compile that code and sit ("Pose!") on it. You should see a blue bar warning telling you that permissions are not set.

Permissions? What are those? Well, certain actions require that a person consent to them first. Check out here for some examples.

Let's use run_time_permissions and llRequestPermissions to finish our poseball.

integer hidden = FALSE; // Stores whether the object is visible

default
{
    state_entry()
    {
        llSitTarget(<0,0,1>,<0,0,0,1>); // Set the target one meter above the ground
        llSetSitText("Pose!");
    }

    changed(integer change)
    {
        if(change & CHANGED_LINK) // If someone has sat on, or "linked," to this prim...
        {
            key avataronsittarget = llAvatarOnSitTarget();
            if( avataronsittarget != NULL_KEY )    //Someone is sitting on the object
            {
                // Comment out this code:
                //llStopAnimation("sit");
                //llStartAnimation("dance1");
                // Before animating, first check if we have permission to do so:
                if ((llGetPermissions() & PERMISSION_TRIGGER_ANIMATION) && llGetPermissionsKey() == avataronsittarget) {
                    // If we do, we can animate:
                    llStopAnimation("sit");
                    llStartAnimation("dance1");
                } else {
                    // If we dont, ask for them:
                    llRequestPermissions(avataronsittarget, PERMISSION_TRIGGER_ANIMATION);
                    // We'll animate in the run_time_permissions event, which is triggered
                    // When the user accepts or declines the permissions request.
                }
            }
        }
    }

    run_time_permissions(integer perm)
    {
        if(perm)
        {
            // Place the code here!
            llStopAnimation("sit");
            llStartAnimation("stand");
        }
    }

    touch_start(integer total_number)
    {
        if(hidden)
        {
           hidden = FALSE;
           llSetLinkAlpha(LINK_SET,1,ALL_SIDES);
        }
        else
        {
           hidden = TRUE;
           llSetLinkAlpha(LINK_SET,0,ALL_SIDES);
        } 
    }
}




Congratulations! You've written your first script and taken your first steps into the world of LSL scripting. Feel free to reference these pages should you ever get lost or need to find your way.

<Int - Fo - Ch1 - Ch2 - Ch3 - Ch4 - Ch5 - Ch 6 - App1 - App2>

Homepage | LSL101 | Tutorial | CrashCourse | Glossary
There are 2 comments on this page. [Display comments/form]