Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Notecard support

Malik Bode
Registered User
Join date: 27 Dec 2006
Posts: 23
07-16-2007 08:14
Hi,

I'd like to make my scripts a little more fool proof. Ideally id have the script refer to a notecard for any variables. This would allow the owner to make changes without touching the script.

Any help appreciated.
Imajica Hand
Registered User
Join date: 3 Feb 2006
Posts: 66
07-16-2007 08:35
:confused: This question looks too easy...
Something like that?
http://lslwiki.net/lslwiki/wakka.php?wakka=LibrarySettingsNotecard
Milambus Oh
Registered User
Join date: 6 Apr 2007
Posts: 224
07-16-2007 08:47
Reading from a Notecard is moderately easy, once you understand event driven programming. Writing to a Notecard however is impossible.

Here are the steps you need to go through to read a notecard:
1. Read the initial line with llGetNotecardLine. Store the key this function returns in a global variable.

2. Reading the line with fire the dataserver() event. Within the event check to make sure this is the correct data request and that you have not hit the end of the notecard.

3. Then process the data in some way.

4. Request the next line.

5. Steps #2-4 repeat until the end of the Notecard.

Here is the bare bones needed to read a Notecard, adjust it however you need:

CODE

integer currentLine = 0;
key request;
string notecardName = "Config";

init() {
currentLine = 0;
request = llGetNotecardLine(notecardName, currentLine);
}

default
{
state_entry()
{
// Read the notecard
init();
}

changed(integer change)
{
// Reread the notecard when the objects inventory has been changed.
// Editting the notecard evokes this call.
if (change & CHANGED_INVENTORY) {
init();
}
}

dataserver(key requested, string data)
{
if (request == requested) {
// Make sure we have not hit the end of the notecard (EOF or "End Of File")
if (data != EOF) {
// Process this line in some way here (Parse out variables, print out contents, whatever)

// After processing, read the next line.
currentLine++;
request = llGetNotecardLine(notecardName, currentLine);
}
}
}
}
Shadow Subagja
Registered User
Join date: 29 Apr 2007
Posts: 354
07-16-2007 10:49
Yeah the event stuff is what made this hardest for me as well. You have to realize that your note card reading function will call llGetNoteCardLine then continue without having ever read a line. The dataserver event will get called by the server once your note card line is ready, and you continue from there (including calling llGetNoteCardLine again, from within the event to read more lines).

Once you get a handle on that, its really easy.

Also I prefer to get my parameters different from the way the example script posted does. I will define a note card structure as follows:

CODE

//note card line structure
//NAME:VALUE1:VALUE2

integer PARAM_NAME = 0;
integer PARAM_VALUE1 = 1;
integer PARAM_VALUE2 = 2;

list nameList;
list value1List;
list value2List;

state whatever
{
dataserver( key kQuery, string sData )
{
if( sData != EOF )
{
//parse out parameters on this line
list lSettings = llParseString2List("sData",[":"],[]);

//add them to their lists
nameList+=lSettings[PARAM_NAME];
value1List+=lSettings[PARAM_VALUE1];
value2List+=lSettings[PARAM_VALUE2];
kCurrentDataRequest = llGetNotecardLine( sSettingsNotecard, ++iNoteCardLine );
}
}


From that point on you can lookup a name in your nameList, and get the various values you associate with it with the same index in the other lists.

That may not be the most memory efficient way to go, but I find that unless I have a memory constraint, the readability and flexibility (and this code is very generic so I can plop it down in any of my scripts and use it, just changing around the names of things a bit) really helps me just get along and not worry about the note cards.