Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

My Dialog Based Question System Script Does't not work

Jasline Ashdene
Registered User
Join date: 22 Oct 2008
Posts: 6
12-17-2008 04:46
Can anyone help me to debugg this script? I have problem with this script. It seems that it doesnt work. Did anything gone wrong? Do you all have better Dialog Based Question System scripts?

// Simple Dialog based question system with email support
// Primary reason would be as questionaire but could be adapted to a quiz by using the PRIZE and WIN Options
// Use the first answer as always being the right one, the answer order is randomised each time it is read.
// To Give a prize every time use PRIZE or PRIZE MONEY
// To give only if they win use WIN and WIN MONEY
//
// Uses a single Notecard to define all of the questions.
//
// Format of Note Card
//
// QUIZ NAME=My Silly little Quiz
// EMAIL=myemail@myserver
// +-START OF QUIZ-+
// QUESTION=What would you like to do?
// ANSWER=My Answer 1
// ANSWER=My Answer 2
// ANSWER=My Answer 3
// PRIZE=MyPrize
// PRIZE MONEY=5
// WIN=MyPrize
// WIN MONEY=5
// +-END OF QUESTION-+
// QUESTION=Where would you like to do it?
// ANSWER=My Answer 1
// ANSWER=My Answer 2
// ANSWER=My Answer 3
// +-END OF QUESTION-+
// PRIZE=MyPrize
// PRIZE MONEY=5
// +-END OF QUIZ-+
//
// PRIZE will give this gift wether the user answers the question correctly or not
// PRIZE MONEY will give the user the amount wether they are right or not
// WIN will only give the prize if they win
// WIN MONEY will like wise only give that amount if they win.
//
string notecard = "Questions";
integer lineCounter;
key dataRequestID;

string Email;
string QuizName;
string Question;
list Answers;
string Prize;
integer PrizeMoney = 0;
string WinPrize;
integer WinMoney = 0;

list Randomised;

integer listenchannel = 0;
integer Listening = 0;
integer i;

key User;
string UserName;
string results;

integer first;
integer pagesize = 10;

integer len;

//-------------------------------------------------------
ErrorMessage(string error)
{
llInstantMessage(llGetOwner(),llGetObjectName() + " : " + error);
}
//-------------------------------------------------------
UpdateListen(key id)
{
CancelListen();
Listening = llListen(listenchannel,"",id,"";);
llSetTimerEvent(30);
}
//-------------------------------------------------------
CancelListen()
{
if(Listening > 0) llListenRemove(Listening);
Listening = 0;
llSetTimerEvent(0);
}
//-------------------------------------------------------
ProcessData(string data)
{
list ldata = llParseString2List(data, ["="], [""]);
string command = llList2String(ldata,0);
string value = llList2String(ldata,1);
if("QUESTION" == command)
{
Question = value;
Answers = [];
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
}
else if("ANSWER" == command)
{
Answers = (Answers = []) + Answers + [ value ];
}
else if("QUIZ NAME" == command)
{
QuizName = value;
}
else if("EMAIL" == command)
{
Email = value;
}
else if("PRIZE" == command)
{
Prize = value;
}
else if("PRIZE" == command)
{
Prize = value;
}
else if("PRIZE MONEY" == command)
{
PrizeMoney = (integer)value;
}
else if("WIN" == command)
{
WinPrize = value;
}
else if("WIN MONEY" == command)
{
WinMoney = (integer)value;
}
}
//-------------------------------------------------------
AskTheQuestion()
{
list choices = ["Quit"];

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

for(i = 0;i < pagesize;i++)
{
string strname = llList2String(Randomised,(i+first));
if( llStringLength(strname) > 0)
{
choices += (strname);
}
}
// finally show the dialog
llDialog(User,Question,choices,listenchannel);
UpdateListen(User);

}
//-------------------------------------------------------
HandlePrizes(string gift, integer amount)
{
if(llStringLength(gift) > 0)
{
if(llGetInventoryType(gift) != INVENTORY_NONE)
llGiveInventory(User,gift);
}
if(amount > 0)
{
llGiveMoney(User, amount);
}
}
//-------------------------------------------------------
GivePrizes(integer right)
{
HandlePrizes(Prize, PrizeMoney);
if(right == 0) HandlePrizes(WinPrize, WinMoney);
}
//-------------------------------------------------------
default
{
state_entry()
{
lineCounter = 0;
Question = "";
Answers = [];
User = NULL_KEY;
UserName = "";
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
// we need this permission to give change / winnings
integer perms = llGetPermissions();
if((perms & PERMISSION_DEBIT) ==0)
llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);

llSetText(llGetScriptName() + " Initialisation in Progress\nPlease Wait",<1.0,0.0,0.0>, 1);

state ReadPrelim;
}

on_rez(integer number)
{
llResetScript();
}

changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
}
//-------------------------------------------------------
state StartQuiz
{
state_entry()
{
Question = "";
Answers = [];
User = NULL_KEY;
UserName = "";
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;

llSetText(QuizName + " Ready.\nPlease Touch me to answer the questions",<0.0,1.0,0.0>, 1);
lineCounter++;
}

on_rez(integer number)
{
llResetScript();
}

touch_start(integer num_detected)
{
User = llDetectedKey(0);
UserName = llDetectedName(0);
llSetText("In Use by " + UserName + ". Please Wait",<0.7,0.3,0.7>, 1);
lineCounter = 0;
state ReadQuestion;
}

changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}
}
//-------------------------------------------------------
state AnswerQuestion
{
state_entry()
{
listenchannel = (integer)llFrand(4000);
AskTheQuestion();
}

on_rez(integer number)
{
llResetScript();
}

changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}

timer()
{
CancelListen();
results = (results = "";) + results + Question + " = TIMED OUT. (Should have been " + llList2String(Answers,0) + ";)\n";
llSay(0, UserName + ", Oh dear you took too long. Next Question.";);
lineCounter++;
state ReadQuestion;
}

listen(integer number, string name, key id, string message)
{
// Cancel the listen timer
CancelListen();

if("Quit" == message)
{
state GoodBye;
}
else if("Prev" == message)
{
first -= pagesize;
AskTheQuestion();
}
else if("Next" == message)
{
len = llGetListLength( Answers );
if((first + pagesize) < len)
first += pagesize;
AskTheQuestion();
}
else
{
// Check for a valid answer
integer selection = llListFindList(Answers, [ message ]);
if(selection >= 0)
{
results = (results = "";) + results + Question + " = " + message;
if(selection == 0)
{
results += " (CORRECT)\n";
}
else
{
results += " (Should have been " + llList2String(Answers,0) + ";)\n";
}

// Handle Prizes
GivePrizes(selection);

lineCounter++;
state ReadQuestion;
}
}
}
}
//--------------------------------------------------------------------------------
state EndOfQuiz
{
state_entry()
{
GivePrizes(1);
llSay(0, UserName + ", Thank you for taking part in our quiz.";);
llSetText("Processing Answers. Please Wait",<1.0,0.0,0.0>,1);
// Email The quiz details to the specified Email Address.
llEmail(Email, QuizName, UserName + "\n" + results );
lineCounter = 0;
state ReadPrelim;
}

on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state GoodBye
{
state_entry()
{
Prize = "";
PrizeMoney = 0;
WinPrize = "";
WinMoney = 0;
state EndOfQuiz;
}

on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ErrorCondition
{
state_entry()
{
llSetText("Sorry I'm broken.\nPlease Try again later.",<1.0,0.0,0.0>,1);
}

changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llResetScript();
}
}

on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ReadPrelim
{
state_entry()
{
lineCounter = 0;
integer itemtype = llGetInventoryType(notecard);
if(INVENTORY_NOTECARD == itemtype)
{
llSetTimerEvent(10);
dataRequestID = llGetNotecardLine( notecard, lineCounter ); // start reading the notecard
}
else
{
ErrorMessage("Unable to read Quiz";);
state ErrorCondition;
}
}

dataserver( key query_id, string data ) // read from the notecard
{
if(dataRequestID == query_id)
{
if (data != EOF)
{
if(llGetSubString(data, 0,0) != ";";)
{
if("+-START OF QUIZ-+" == data)
{
llSetTimerEvent(0);
if(llStringLength(Email) == 0)
{
ErrorMessage("Email Address Not Specified.";);
state ErrorCondition;
}
else
state StartQuiz;
}
else
{
ProcessData(data);
}
}
lineCounter++;
dataRequestID = llGetNotecardLine( notecard, lineCounter );
}
else
{
llSetTimerEvent(0);
state AnswerQuestion;
}
}
}

timer()
{
llSetTimerEvent(0);
ErrorMessage("Timeout reading Quiz";);
state ErrorCondition;
}


on_rez(integer number)
{
llResetScript();
}
}
//--------------------------------------------------------------------------------
state ReadQuestion
{
state_entry()
{
llSetTimerEvent(10);
dataRequestID = llGetNotecardLine( notecard, lineCounter ); // start reading the notecard
}

dataserver( key query_id, string data ) // read from the notecard
{
if(dataRequestID == query_id)
{
if (data != EOF)
{
if(llGetSubString(data, 0,0) != ";";)
{
if("+-END OF QUIZ-+" == data)
{
llSetTimerEvent(0);
state EndOfQuiz;
}
else if("+-END OF QUESTION-+" == data)
{
llSetTimerEvent(0);
Randomised = llListRandomize(Answers, 1);
state AnswerQuestion;
}
else
{
ProcessData(data);
}
}
lineCounter++;
dataRequestID = llGetNotecardLine( notecard, lineCounter );
}
else
{
llSetTimerEvent(0);
state AnswerQuestion;
}
}
}

timer()
{
llSetTimerEvent(0);
ErrorMessage("Timeout reading Quiz";);
state ErrorCondition;
}


on_rez(integer number)
{
llResetScript();
}
}
Klug Kuhn
Registered User
Join date: 7 Sep 2007
Posts: 126
12-17-2008 22:35
Just had a run in-world for the script, it's completely compiling ok and no syntax error. :)
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
12-17-2008 22:44
It might be a good idea to enclose your code in CODE tags or PHP tags. For those of us who use the highly recommended BBCode In Text Greasemonkey script described in the stickies at the top of the Resident Answers forum, this makes the code appear properly indented.
_____________________
-

So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.

I can be found on the web by searching for "SuezanneC Baskerville", or go to

http://www.google.com/profiles/suezanne

-

http://lindenlab.tribe.net/ created on 11/19/03.

Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard,
Robin, and Ryan

-
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-18-2008 00:25
And when you say, ". It seems that it doesnt work. Did anything gone wrong?" could you be a bit more specific? In what way doesn't it work, and does it give you any error messages?

If it's compiling OK but just failing silently and not doing anything, the first thing I would check is that there's some valid data in the notecard for it to read. That's the mistake I usually make, anyway -- depending on how the script's written, if there's a notecard in the inventory, you won't get an error message, but if the script can't understand the notecard (or it's blank) it probably wont do or say anything.
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
12-18-2008 03:26
Why do I strongly suspect that the OP has no idea how the script works, and did not write it...?

/esc
_____________________
http://slurl.com/secondlife/Together
Jasline Ashdene
Registered User
Join date: 22 Oct 2008
Posts: 6
12-22-2008 06:45
What went wrong actually?