2009-06-24 41 views
3

我在每行都有一個不同單詞的記事卡,我希望能夠隨機選擇行。我怎樣才能做到這一點?如何從LSL中的記事卡讀取隨機行?

+0

這是對Script Academy組中提出的問題的回答。張貼在這裏保存它的後代。 – btubbs 2009-06-24 19:49:12

回答

2

首先,如你所說,你需要一張記事卡。在這個例子中,我使用的是一個名爲「色」,其內容如下:

red 
blue 
green 
yellow 
orange 
purple 

隨着目前認爲notecard,下面的腳本將讀取並從卡聊天隨機線各古板被觸摸的時間。

//this script will grab and chat a random line from the "colors" notecard each time the prim is touched. 

string card = "colors"; 
key linecountid; 
key lineid; 
integer linemax; 

integer random_integer(integer min, integer max) 
{ 
    return min + (integer)(llFrand(max - min + 1)); 
} 


default 
{ 
    state_entry() 
    { 
     //get the number of notecard lines 
     linecountid = llGetNumberOfNotecardLines(card); 
    } 

    touch_start(integer total_number) 
    { 
     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); 
     } 
    } 
} 
0

你爲什麼做這樣的不必要的數學附加1,然後在你給自己上面的答案以後再次從其減去它目前尚不清楚。 如果你想確保你有因爲有一些已知的llFrand隨機性,你可以做的問題更隨機數(不檢查的數量是偶數或奇數):

integer max; 
integer random = llFrand((integer)(max/2)) + llFrand((integer)(max/2)); 

第二個問題你上面的代碼是,你不檢查CHANGED_INVENTORY,我不太確定你爲什麼不這樣做。在第二個問題之後,爲什麼要問一個問題來獲得一個隨機的記錄卡行號並給出一個在範圍內隨機提供的答案?如果記錄卡發生變化,你會怎麼做?更改代碼和記事卡?這對我來說似乎是多餘的。

命名colors或任何你在腳本中設置NOTECARD:在同一個古板

blue 
red 
green 
yellow 
black 

SCRIPT:

// this script reads from a notecard which is named whatever you set in init 
// in this example from a notecard named "colors" 

string ncName; 
key ncNumOfLinesReqId; 
key ncReqId; 
integer numOfLines; 

init() 
{ 
// Put the name of your notecard as in the prim's inventory here. 
    ncName = "colors"; 
} 

default 
{ 
    changed(integer change) 
    { 
//  reset script to make sure you have the current number of lines 
//  CHANGED_OWNER has the integer value 0x80 (128) 
//  CHANGED_INVENTORY has the integer value 0x01 (1) 
     if (change & (CHANGED_OWNER | CHANGED_INVENTORY)) 
     { 
      llResetScript(); 
     } 
    } 

    state_entry() 
    { 
     init(); 

//  get the number of notecard lines 
     ncNumOfLinesReqId = llGetNumberOfNotecardLines(ncName); 
    } 

    touch_start(integer num_detected) 
    { 
//  if the number of lines is 0 
     if (!numOfLines) 
     { 
//   PUBLIC_CHANNEL has the integer value 0 
      llSay(PUBLIC_CHANNEL, "~!~ Unconfigured, check notecard ~!~"); 
     } 
     else // if number of lines not 0 
     { 
      ncReqId = llGetNotecardLine(ncName, (integer)llFrand(numOfLines)); 
     } 
    } 

    dataserver(key reqId, string data) 
    { 
     if (reqId == ncNumOfLinesReqId) 
     { 
//   make sure you typecast! 
      numOfLines = (integer)data; 
     } 
     else if (reqId == ncReqId) 
     { 
//   PUBLIC_CHANNEL has the integer value 0 
      llSay(PUBLIC_CHANNEL, data); 
     } 
    } 
} 

更多信息:

的notecard你是 閱讀並不一定要在相同的原始。如果您知道記事卡的UUID,只要它可以轉讓(而不是刪除),您就可以閱讀它。要警告的是,更改記錄卡的內容並保存,將新內容存儲在不同的UUID之下。但是,如果你是熟練的,你可以將文本存儲在Web服務上,並從那裏獲取文本片段數和文本片段。

更多關於the official Second Life wiki