Monday 14 May 2007

Sit script

This is my Second Life sit-script and is the core for many functions starting with sitting on a seat, to teleports and pose-stands.

When an avatar sits they get linked to the object they are sitting on and your movement becomes linked to that object. This is why you move when flying a car, sailing a boat or riding a bicycle rather than being left behind.

The sit script works by:-
  1. Wait for a changed event
  2. Check if it was something being linked with the CHANGED_LINK mask
  3. Check if it was an avatar being linked with llAvatarOnSitTarget
  4. Check for the permission to trigger an animation. This triggers a run_time_permissions event
  5. Stop the previous animation with llStopAnimation
  6. Start my animation with llStartAnimation

When the avatar stands up:-
  1. Wait for a changed event
  2. Check if the avatar is being unlinked; llAvatarOnSitTarget will return a NULL_KEY
  3. Unsit the sitter with llUnsit(sitter). The 'sitter' key will only be valid (not a NULL_KEY) if I already had permission to animate the avatar.
When objects are linked a change event is triggered, this includes when you sit on the object. The change event also triggers on other changes such as an object changing colour or size. I first check that this is a link event using the mask CHANGED_LINK, then check whether it was an avatar being linked with llAvatarOnSitTarget. This will return an avatar key if an avatar was linked. If I get an avatar key then I check for animate permissions. This triggers a run_time_permissions event and I check if I have permission to animate. If I have, then stop the old animation "sit" and start my animation "anim".

llAvatarOnSitTarget will only work if llSitTarget has been set for the object that you are sitting on. You can have several sit scripts running with one in each sit position and this is used where you have more than one seat in an object, such as a car or settee. The sit position that is triggered is the lowest link number that is not being sat upon.

If we attempt to animate an avatar without permission we will get a script error. It looks as if there are two places that llUnist will create an error as it is used where we may not have animate permission. The argument 'sitter' will be NULL_KEY if we do not have animate permission so this will not be a problem.

Setting the script up. You will need to change the sit position to match your animation. Change the first number to move the avatar backwards and forwards and the last one to adjust the height.

// Sit script
// by Asp Grelling 23APR07

vector sitPosition = <0.0, 0.0, 0.76> ; // Sit offset
vector sitRotation = <0,0,0> ; // Sit rotation

// ---- DO NOT MODIFY BELOW THIS LINE -----

key avatar ;
key sitter = NULL_KEY ;
string anim = "sit" ;

default
{
state_entry()
{
llSetSitText("Sit here");
rotation rot = llEuler2Rot(sitRotation);
llSitTarget(sitPosition, rot);
sitter = NULL_KEY ;
}

on_rez(integer param)
{
llResetScript();
}

changed(integer change)
{
if(change & CHANGED_LINK) {
avatar = llAvatarOnSitTarget();
if ( avatar ) {
// Request permision to sit
llRequestPermissions(avatar,PERMISSION_TRIGGER_ANIMATION);
} else {
if ( sitter ) {
llUnSit(sitter);
sitter = NULL_KEY ;
} else {
// We can get here for many reasons
// Do nothing
}
}
}
}

run_time_permissions(integer perm)
{
if(perm & PERMISSION_TRIGGER_ANIMATION)
{
// Sit action
sitter = avatar ;
llStopAnimation("sit");
llStartAnimation(anim);
} else {
// We do not have permissions, so unist
llUnSit(sitter);
sitter = NULL_KEY ;
}
}
}

1 comment:

Mike McKay said...

Hi Anne. Works well in OpenLife so far. Wondered if I could make it available to the community. Thanks.