我在每行都有一個不同單詞的記事卡,我希望能夠隨機選擇行。我怎樣才能做到這一點?如何從LSL中的記事卡讀取隨機行?
3
A
回答
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服務上,並從那裏獲取文本片段數和文本片段。
相關問題
- 1. 如何從java中的文件中讀取隨機行
- 2. 如何從python中的一個文件中讀取隨機行?
- 3. 如何從文本文件中讀取隨機行?
- 4. Java - 隨機行讀取
- 5. 從文本文件中隨機讀取
- 6. 從Vector中讀取隨機對象java
- 7. 從Python中的文件中讀取許多隨機行
- 8. 從php中隨機記錄取自mysql
- 9. 從txt文件中讀取隨機行的C#(sharp)
- 10. 從文本文件中隨機讀取一行的函數
- 11. getline函數讀取文件中沒有的隨機字符;隨同記事本關鍵字中的數據一起讀取兩個隨機字符。
- 12. ASP.NET MVC - 如何從模型中獲取隨機記錄?
- 13. 如何從SQLite數據庫中獲取隨機記錄?
- 14. 如何從oracle數據庫中隨機獲取記錄?
- 15. 如何隨機從數組中獲取
- 16. c#從txt文件中讀取「n」個隨機行數
- 17. Clojure懶洋洋地從文件中讀取隨機行
- 18. 如何讀取從Android發送到Mac OSx的隨機數據?
- 19. 針對隨機讀取進行優化
- 20. Android/Java - 隨機讀取行不變
- 21. 從/ dev/random讀取隨機數(c)
- 22. 如何從一組隨機數中進行隨機選擇?
- 23. 如何從MySQL數據庫中獲取隨機數的行?
- 24. 隨機記錄從MongoDB的
- 25. 隨機列取中卡桑德拉
- 26. LINQ2SQL獲取隨機記錄
- 27. Python - 隨機卡
- 28. 如何從C中的大量單詞中讀取隨機字符串?
- 29. 如何通過挑選隨機變量來在Python中執行隨機事件?
- 30. Javascript:如何在按下按鈕時從本地txt文件讀取隨機行?
這是對Script Academy組中提出的問題的回答。張貼在這裏保存它的後代。 – btubbs 2009-06-24 19:49:12