Jump to content

Read Notecard line after timer event issue


Parasite Palen
 Share

You are about to reply to a thread that has been inactive for 4647 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hey there people.

 

I'm trying to write a simple script that counts a random timer then outputs a random line from an included notecard.

 

The problem I'm having (I think) is that the notecard doesn't get read quickly enough for the script to output anything other than the first line.

 

Ok I know you're going to say "You should write it so the script reads and stores the full notecard before starting."

Thats beyond my capabilities. I've read through many tutorials and examples and the wiki put cannot figure out how to adapt any of those scripts to my needs, so I took what I thought was the simple route.

 

I'm hoping that all this needs is a bit of a reorder. Here goes.

 

integer channel = 746;

list options = ["Yes", "No"];

float min = 10.0;

float max = 20.0;

key avatar = "insert key here";

integer listen_handle;

string card = "the notecard";

key linecountid;

key lineid;

integer linemax;

integer random_integer( integer min, integer max )

return min + (integer)( llFrand( max - min + 1 ) );

}


default

{   

on_rez(integer start_param)

    {       

llResetScript(); 

      llSetText("", <0.0, 0.0, 0.0>, 0.0);

            } 

           touch_start(integer total_number)


    { 

      listen_handle = llListen(channel, "", NULL_KEY, ""); 

      llDialog(llDetectedKey(0), "Are you ready?", options, channel); 

  } 

     

listen(integer channel, string name, key id, string message)

{

   if (message == "Yes")               

{           

state active;

        } 

          else if (message == "No") 

          { 

          llResetScript();

        }

    }

}

state active
{   

state_entry()

         { 

      llSay(0, "Timer Active.Touch again to stop.");

        llSetText("Active", <0.0, 1.0, 0.0>, 1.0);

        llInstantMessage(avatar, "Timer Active. Be ready :P"); 

      llSetTimerEvent( llFrand( max - min ) + min );

        llListenRemove(listen_handle); 

           } 

 

               touch_start(integer total_number) 

     { 

         llSay(0, "Stopped"); 

         llSetText("", <0.0, 0.0, 0.0>, 0.0);

           llResetScript(); 

     } 

 

           timer()

    {

        llInstantMessage(avatar, "Time's Up"); 

      llSetTimerEvent( llFrand( max - min ) + min ); 

      lineid = llGetNotecardLine(card, random_integer(0, linemax));

    } 

     dataserver(key id, string data)

    { 

      if (id == linecountid)

        { 

          linemax = (integer)data - 1; 

      } 

      else if (id == lineid) 

     { 

         llSay(0, data); 

     }

    }

    }

 

 

My apologies for the formatting, I have no idea how to paste the script onto the forum, had to manullay tab and space it all out lol.

 

Anyway, many thanks in advance if you can help me rearrange it to make ot work, or if you feel really generous you could adapt it for me to read the notecard from the start :)

 

Link to comment
Share on other sites

there are a few strange things:

  1. you always just read the fist line of your notecard since linemax never gets set explicitly - so it will stay 0
  2. linecountid never gets set
  3. avatar ever gets set and thus will lead to errors in the instant messages
  4. you should restart the timer in the dataserver event or the script will time out no matter what

The notecard reading is not very fast - but you give the script 10 - 20 secs - this should in most cases be more than enough to read a line.

Link to comment
Share on other sites

When you're reading a notecard I would always advise reading it all once and storing the results in the script's memory.   Less work for you and less work for the sim. 

And when people ask me to do "random" stuff I find they don't always mean "truly random" -- rather, they mean "in an unpredictable order".   So I just randomise the list, work down it, and randomise it again when I get to the end -- this avoids the possibility of having the item say the same thing/give out the same item several times in succession.    But if you do want a random choice each time, just call llRandomizeList each time, and read the first line.

Anyway, this is my basic mechanism if you want to play with it:

(edited to reset the counter before I re-read the notecard if it changes)

 

string notecard;key query;integer counter;integer max;list my_list;read_notecard(){	notecard = llGetInventoryName(INVENTORY_NOTECARD,0);	if(llGetInventoryType(notecard)==INVENTORY_NOTECARD){		counter=0;		llOwnerSay("reading the notcard");		my_list=[];		query=llGetNotecardLine(notecard,counter);	}	else{		llOwnerSay("Please put a notecard in me");	}}default{	state_entry()	{		read_notecard();	}	changed(integer change)	{		if(change & CHANGED_INVENTORY){			read_notecard();		}	}	dataserver(key requested, string data)	{		if(requested==query){			if(data!=EOF){				data=llStringTrim(data,STRING_TRIM);//chop off leading and trailing spaces				if(data!=""&&llGetSubString(data,0,1)!="//"){//not an empty line and not a comment					my_list+=[data];//add it to the list				}				counter++; //advance the counter				query=llGetNotecardLine(notecard,counter);//fetch the next line			}			else{//finished reading the card				llOwnerSay("finished reading the notecard");				state running;			}		}	}}state running{	state_entry()	{		max=llGetListLength(my_list);		counter=0;		my_list=llListRandomize(my_list,1);	}	touch_end(integer total_number)	{		llOwnerSay(llList2String(my_list,counter);		counter++;//advance the counter		if(counter==max){//reached the end of the list			counter=0; //reset the counter			my_list=llListRandomize(my_list,1);//randomise the list again		}	}	changed(integer change)	{		if(change & CHANGED_INVENTORY){			state default;		}	}}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4647 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...