What's wrong with this script?
|
|
Cherry Hainsworth
Registered User
Join date: 31 Jul 2006
Posts: 125
|
01-13-2008 16:39
Please help! I've been plugging away at this all day, and I'm still getting errors. LSL Editor doesn't find any syntax errors, but it doesn't seem to work right in the debugger. The in-world editor seems to be alternating between "name previosuly defined" and "Name not defined"  I'm far from sure about all the state changes, but they did seem to help reduce my error count! All input gratefully received - diagnosis & improvements. Thanks! // Freebie Shack by Cherry Hainsworth // Lucky dip script: Ordinal Malaprop, January 2008 // Alterations & additions: Cherry Hainsworth, January 2008 // Please keep these credits intact! // Thank you.
// -------------------------------------------------------- // vars used in owner functions
integer owner; key owner_key; string owner_name; integer listenkey; string contents;
// -------------------------------------------------------- // lucky dip
string random_object_name() { integer n = llGetInventoryNumber(INVENTORY_OBJECT); integer random = llFloor(llFrand((float)n)); return llGetInventoryName(INVENTORY_OBJECT, random); }
// -------------------------------------------------------- // visitor function
visitor() { key visitor = llDetectedKey(0); string visitor_name = llKey2Name(visitor);
llWhisper(0, "Hello, " + visitor_name + ", you just got lucky! Free gift! Hope you like it.");
llGiveInventory(visitor, random_object_name()); llGiveInventory(visitor, llGetInventoryName(INVENTORY_LANDMARK, 0)); }
// -------------------------------------------------------- // owner functions
add_items() { llAllowInventoryDrop(TRUE); llWhisper(0, "Touched by my owner, " + owner_name + ". You can add items - please note, only OBJECTS will be delivered! \nIf you want to give out textures, animations & so on, put them in a box first. \n");
if (owner && (!llGetInventoryNumber(INVENTORY_LANDMARK)) ) {
llWhisper(0,"You can drop a landmark in, if you like. It will be given out with each freebie :)"); }
}
// --------------
list_inventory() { list result = []; integer n = llGetInventoryNumber(INVENTORY_ALL); integer i = 0;
while(i < n) { result += llGetInventoryName(INVENTORY_ALL, i); ++i; }
contents = llDumpList2String(result, "\n"); llWhisper(0, "Entire Contents:\n" + contents); }
// --------------------------------------------------------
default {
state_entry() { llSetStatus(STATUS_BLOCK_GRAB, TRUE); owner_key = llGetOwner(); owner_name = llKey2Name(owner_key); }
touch_start(integer touched) {
if (llDetectedKey(0) == owner_key ) { owner = TRUE; state owner; }
else { state visitor; } } changed(integer dropped) { if (dropped & (CHANGED_INVENTORY | CHANGED_ALLOWED_DROP)) { integer total = llGetInventoryNumber(INVENTORY_OBJECT); llWhisper(0, "Thanks, " + owner_name + ". Your Freebie Shack now contains " + (string)total + " gifts."); }
}
}
// --------------------------------------------------------
state owner { state_entry() {
// dialog ~ Create random channel within range [-1000000000,-2000000000] integer channel = (integer)(llFrand(-1000000000.0) - 1000000000.0); listenkey = llListen( channel, "", owner_key, "" ); llSetTimerEvent(60); llDialog(owner_key, "\nPlease choose an option:\n", ["Test", "List", "Add"], channel); }
timer() { llListenRemove(listenkey ); llSetTimerEvent( 0 ); } listen ( integer channel, string name, key id, string message ) { llListenRemove( listenkey ); if ( message == "Add" ) { add_items(); } if ( message == "List" ) { list_inventory(); } if ( message == "Test" ) { owner = FALSE; state visitor; } } state_exit() { llResetScript(); state default; } }
// --------------------------------------------------------
state visitor { state_entry() { visitor(); } state_exit() { llResetScript(); state default; } }
// --------------------------------------------------------
|
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
01-13-2008 17:06
You've got a global variable named 'owner' and a state named 'owner'. SL just crashed on me but that's a good place to start. edit: this is a good reason to come up with a naming convention and stick to it.. I use integer gGlobalVar; integer NEVER_CHANGES = 42;
SomeFunction() { integer localVar; }
edit edit: same thing with 'visitor' - you've got a state and a function named that.
|
|
Cherry Hainsworth
Registered User
Join date: 31 Jul 2006
Posts: 125
|
01-13-2008 18:17
Thank you, Sindy! I have a feeling this is going to be one of those, very embarrassing, incidents where staring at the same thing for hours stops you from actually seeing it ..... Script still isn't working, so I'm off to remove all those states. Feel free to point out any other idiocies you may spot  Cherry
|
|
Cherry Hainsworth
Registered User
Join date: 31 Jul 2006
Posts: 125
|
pass a variable from one state to the next??
01-13-2008 20:47
OK, I got both the owner functions working without removing my states. I just took out all the state_exit & reset stuff. I can't get the 'test' part to work, though. I need the script to fill the 'visitor' key with the value it got when I clicked on the prim, ie the current 'owner' key. I have tried every method I know (from PHP), to no avail. I'm assuming the variable is emptied when the script passes from one state to another? I looked up the documentation on this, but it's flimsy. Would this be a time to use jump? The Wiki's jump example uses @, which I haven't seen before and isn't in the docs .....  Oh, please, somebody, tell me what I should be doing!!!! ** In case anybody's interested, I'm posting the current script (which works) below. It works because I've repeated the commands - marked FUDGE - for the visitor function, whilst what I really wanted was to move the script to visitor state with the existing variable. I hate repeating code, but can't figure out how to re-use it. // Freebie Shack by Cherry Hainsworth // Lucky dip script: Ordinal Malaprop, January 2008 // Alterations & additions: Cherry Hainsworth, January 2008 // Please keep these credits intact! // Thank you.
// -------------------------------------------------------- // vars used in owner functions
integer is_owner; key owner_key; string owner_name; integer is_visitor; key visitor_key; integer listenkey; string contents;
// -------------------------------------------------------- // lucky dip
string random_object_name() { integer n = llGetInventoryNumber(INVENTORY_OBJECT); integer random = llFloor(llFrand((float)n)); return llGetInventoryName(INVENTORY_OBJECT, random); }
// -------------------------------------------------------- // visitor function
visitor() { visitor_key = llDetectedKey(0); string visitor_name = llKey2Name(visitor_key);
llWhisper(0, "Hello, " + visitor_name + ", you just got lucky! Free gift! Hope you like it.");
llGiveInventory(visitor_key, random_object_name()); llGiveInventory(visitor_key, llGetInventoryName(INVENTORY_LANDMARK, 0)); }
// -------------------------------------------------------- // owner functions
add_items() { llAllowInventoryDrop(TRUE); llWhisper(0, "Touched by my owner, " + owner_name + ". You can add items - please note, only OBJECTS will be delivered! \nIf you want to give out textures, animations & so on, put them in a box first. \n");
if (is_owner && (!llGetInventoryNumber(INVENTORY_LANDMARK)) ) {
llWhisper(0,"You can drop a landmark in, if you like. It will be given out with each freebie :)"); }
}
// --------------
list_inventory() { list result = []; integer n = llGetInventoryNumber(INVENTORY_ALL); integer i = 0;
while(i < n) { result += llGetInventoryName(INVENTORY_ALL, i); ++i; }
contents = llDumpList2String(result, "\n"); llWhisper(0, "Entire Contents:\n" + contents); }
// --------------------------------------------------------
default {
state_entry() { llSetStatus(STATUS_BLOCK_GRAB, TRUE); owner_key = llGetOwner(); owner_name = llKey2Name(owner_key); }
touch_start(integer touched) {
if (llDetectedKey(0) == owner_key ) { is_owner = TRUE; state owner_options; }
else { state giveaway; } } changed(integer dropped) { if (dropped & (CHANGED_INVENTORY | CHANGED_ALLOWED_DROP)) { integer total = llGetInventoryNumber(INVENTORY_OBJECT); llWhisper(0, "Thanks, " + owner_name + ". Your Freebie Shack now contains " + (string)total + " gifts."); }
}
}
// --------------------------------------------------------
state owner_options { state_entry() {
// dialog ~ Create random channel within range [-1000000000,-2000000000] integer channel = (integer)(llFrand(-1000000000.0) - 1000000000.0); listenkey = llListen( channel, "", owner_key, "" ); llSetTimerEvent(60); llDialog(owner_key, "\nPlease choose an option:\n", ["Test", "List", "Add"], channel); }
timer() { llListenRemove(listenkey ); llSetTimerEvent( 0 ); } listen ( integer channel, string name, key id, string message ) { llListenRemove( listenkey ); if ( message == "Add" ) { add_items(); } if ( message == "List" ) { list_inventory(); } if ( message == "Test" ) { is_owner = FALSE; // FUDGE visitor_key = owner_key; string visitor_name = llKey2Name(visitor_key);
llWhisper(0, "Hello, " + visitor_name + ", you just got lucky! Free gift! Hope you like it.");
llGiveInventory(visitor_key, random_object_name()); llGiveInventory(visitor_key, llGetInventoryName(INVENTORY_LANDMARK, 0)); } state default; } }
// --------------------------------------------------------
state giveaway { state_entry() { visitor(); state default; } }
// --------------------------------------------------------
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
01-13-2008 23:25
I see you're calling function visitor from state entry in the state giveaway...
lldetected functions (like in the visitor function) aren't going to work from state entry, because all the detected data was back in the touch event of your previous state... you need to store the detected info before leaving the state that's doing the detection... I'd just move the function call back into the touch event instead of moving it into a separate state
_____________________
| | . "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... | - 
|
|
Cherry Hainsworth
Registered User
Join date: 31 Jul 2006
Posts: 125
|
01-14-2008 12:29
Thank you, Void! (What would I do without you?) I reckon I'm just going to wait until "They" introduce classes to lsl: my script works perfectly - with everything laid out in linear order. Now all I have to do is add delete functionality, and work out how to allow a group to be an owner .... Cheers again, Cherry 
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
01-15-2008 01:41
I believe NCI still does classes... maybe even some of the scripters might... I'd offer but I'm a horrible teacher =) besides my idea of even tutoring is to walk someone through a script part by part... but it's not really possible to cover much that way, and you cover alot of the same stuff over and over.
_____________________
| | . "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... | - 
|
|
Cherry Hainsworth
Registered User
Join date: 31 Jul 2006
Posts: 125
|
01-15-2008 04:34
actually I meant classes as in modular code ..... but you're right, I could use some lessons 
|
|
Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
|
01-15-2008 05:27
From: Cherry Hainsworth actually I meant classes as in modular code ..... but you're right, I could use some lessons  Do you mean modular code, or object orientation? Both would be useful but for different things 
|