Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

reading the NC correctly

Jonesey Spitteler
Builder / Shop Owner
Join date: 19 May 2007
Posts: 60
11-05-2007 05:12
Hi there...
Got a script for a chair that will make the "sitter" choose different poses as she sits down.
The script works well exept for 1 small thing... It doesnt reas the NC correct, and leaves out the first line... i think...
Here it goes:

MY NOTECARD:
------------------------------------------
Female,female
Male1,sitting chair m04
Male2,sitting chair m07
------------------------------------------

MY SCRIPT:
------------------------------------------
// Animation Player v1.0 - Newgate Ludd
// ------------------------------------
// Written in responce to a posting on the Forums by Artemis Winthorpe
//
string notecardName = "Contents List"; // Notecard containing list of animations
integer lineCounter; // Line number within the notecard
key dataRequestID; // Data request ID
integer CHANNEL = 42; // Channel on which Dialog listens
list MENU; // Menu text for each Anim
list ANIMS; // Actual Animation name
integer maxAnims; // Number of Animations laoded
integer first; // First entry to be displayed on Dialog
integer pagesize = 8; // Number of Items per Dialog
list choices; // Current Dialog Choices
integer Listening = 0; // Listen handle
string animation_name; // Name of current animation
string new_animation_name; // Name of next animation.
key avatar; // Key of the avatar who's sitting on me
vector placement = <0.0,0.0,0.2>;

integer i; // General Purpose

// --------------------------
UpdateListen(key id)
{
CancelListen();
Listening = llListen(CHANNEL,"",id,"";);
llSetTimerEvent(20);
}
// --------------------------
CancelListen()
{
if(Listening > 0) llListenRemove(Listening);
Listening = 0;
llSetTimerEvent(0);
}
// --------------------------
ShowMenu()
{
choices = [];
if(avatar == llGetOwner())
choices += ["*RELOAD*"];

// Now add the items
//len = llGetListLength( MENU );
if(first > 0)
choices += "Prev";
if(maxAnims > (first + pagesize))
{
choices += "Next";
}

for(i = 0;i < pagesize;i++)
{
string strname = llList2String(MENU,(i+first));
if( llStringLength(strname) > 0)
{
choices += strname;
}
}
// finally show the dialog
llDialog(avatar, "Which anim would you like to play?", choices, CHANNEL);
UpdateListen(avatar);
}
// --------------------------
// Set the next animation available as the active one
PlayAnimation(integer number)
{
// Get the name of the animation
new_animation_name = llList2String(ANIMS,number);

// Get the key of the avatar on the stand (or if none is present)
avatar = llAvatarOnSitTarget();

if (avatar != NULL_KEY) // Is avatar is still posing, if so then..
{
llStopAnimation(animation_name); // Stop current animation
llStartAnimation(new_animation_name); // Start next animation
}
// Set the new animation name as the current
animation_name = new_animation_name;
}
// --------------------------

default
{
state_entry() { state ReadNoteCard; }
on_rez(integer num) { llResetScript(); }
}
// --------------------------
state Running
{
state_entry()
{
llSitTarget(placement,<0,0,0,90>;);
animation_name = "sit";
maxAnims = llGetListLength(MENU);
llSay(0,llGetScriptName() + " Loaded - " + (string)maxAnims + " Avaliable.";);
}

on_rez(integer num) { llResetScript(); }

touch_start(integer total_number)
{
key id = llDetectedKey(0);
// We only want the Avatar currently sat to be able to change pose
if(avatar == id) ShowMenu();
}

listen(integer channel, string name, key id, string message)
{
CancelListen();
// verify dialog choice
// present main menu on request to go back

if("*RELOAD*" == message)
{
llResetScript();
}
else if("Prev" == message)
{
first -= pagesize;
ShowMenu();
}
else if("Next" == message)
{
first += pagesize;
ShowMenu();
}
else
{
integer index = llListFindList(MENU, [message]);
if(index != -1)
{
PlayAnimation(index);
}
}
}

timer()
{
CancelListen();
}

changed(integer change)
{
// Test for a changed inventory
if (change & CHANGED_INVENTORY)
{
llResetScript();
}

// The object's sit target has been triggered
if (change & CHANGED_LINK) // Test for a changed link
{
avatar = llAvatarOnSitTarget();
if(avatar != NULL_KEY) // Is that changed link an avatar?
{
llRequestPermissions(avatar,PERMISSION_TRIGGER_ANIMATION);
}
else
{
if (llGetPermissionsKey() != NULL_KEY)
{
llStopAnimation(animation_name);
}
}
}
}

run_time_permissions(integer perm)
{
if (perm & PERMISSION_TRIGGER_ANIMATION)
{
llStopAnimation("sit";);
llStartAnimation(animation_name);
first = 0;
ShowMenu();
}
}

}
// --------------------------
state ReadNoteCard
{
on_rez(integer num) { llResetScript(); }

state_entry()
{
MENU = [];
ANIMS = [];
lineCounter = 0;

integer itemtype = llGetInventoryType(notecardName);
if(INVENTORY_NOTECARD == itemtype)
{
dataRequestID = llGetNotecardLine(notecardName, lineCounter);
llSetTimerEvent(10);
}
else
{
llOwnerSay("Error - Contents list notecard missing!";);
state Running;
}

}
dataserver(key queryid, string data)
{
//Check to make sure this is the request we are making.
//Remember that when data comes back from the dataserver,
//it goes to *all* scripts in your prim.
//So you have to make sure this is the data you want, and
//not data coming from some other script.
if (dataRequestID)
{
llSetTimerEvent(0);
//If we haven't reached the end of the file
if (data != EOF)
{
dataRequestID = llGetNotecardLine(notecardName, lineCounter);
llSetTimerEvent(10);

lineCounter += 1;
// Each line is of the form
// Name,Animation
if(llGetSubString(data, 0,0) != ";";)
{
list ldata = llParseString2List(data, [","], [""]);
string name = llList2String(ldata, 0);
string anim = llList2String(ldata, 1);
integer itemtype = llGetInventoryType(anim);
if(INVENTORY_ANIMATION == itemtype)
{
MENU = (MENU = []) + MENU + name;
ANIMS = (ANIMS = []) + ANIMS + anim;
}
else
{
llOwnerSay("Error - " + name + " (" + anim + ";) Not an Animation.";);
}
}
}
else
{
llSetTimerEvent(0);
state Running;
}
}
}

timer()
{
// The notecard read failed so abort
llSetTimerEvent(0);
llOwnerSay("Error reading Data.Aborting.";);
state Running;
}
}
------------------------------------------

Can someone help, please :-D

/Jonesey
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
11-05-2007 06:07
Sorry, but until BBCode is re-enabled in the forum, I'm not touching any code that long.
_____________________
Pale Spectre
Registered User
Join date: 2 Sep 2005
Posts: 586
11-05-2007 08:57
Firstly the dataserver Event Handler will ignore lines begining with ";"

...as conditioned by: if(llGetSubString(data, 0,0) != ";";)


Also there's an error trap:

if(INVENTORY_ANIMATION == itemtype)

...so that if the script can't find the animation in its inventory it will tell you:

llOwnerSay("Error - " + name + " (" + anim + ";) Not an Animation.";);


Finally, and I accept that you say it's missing the first line out, but it looks to me as if it will read the first line twice as it appears to increment the lineCounter after making the llGetNotecardLine request:

dataRequestID = llGetNotecardLine(notecardName, lineCounter);
llSetTimerEvent(10);

lineCounter += 1;

...whereas I would expect:

lineCounter += 1;
dataRequestID = llGetNotecardLine(notecardName, lineCounter);
llSetTimerEvent(10);


Given the original author of this script I am a bit surprised at this bug, maybe everyone else always started their notecard with ;a comment.

And I agree, reading this stuff without BBCode is a PITA. :(
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
11-06-2007 00:29
From: Pale Spectre
And I agree, reading this stuff without BBCode is a PITA. :(


which is why I make the suggestions found in my sig.... but they don't help if people don't continue to add PHP tags =/

everthing Pale said is spot on, it looks to be reading the first line twice
(and I have no idea why they didn't use (llSubStringIndex( data, ";" )) instead of the more clunky version shown)
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -