各行に異なる単語が書かれたノートカードを持っていますが、その行からランダムに選択できるようにしたいと思います。どうやってやるの?
2 に答える
まず、あなたが言及したように、メモカードが必要です。この例では、次の内容の「colors」という名前のものを使用しています。
red
blue
green
yellow
orange
purple
そのメモカードが存在する場合、次のスクリプトは、プリムがタッチされるたびにカードからランダムな行を読み取り、チャットします。
//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);
}
}
}
上記の回答で、1を追加してから後で再度減算するという不必要な計算を行う理由は明らかではありません。ランダム性に関する既知の問題があるため、より多くの乱数があることを確認したい場合は、llFrand
(数値が偶数か奇数かをチェックせずに):
integer max;
integer random = llFrand((integer)(max/2)) + llFrand((integer)(max/2));
上記のコードの 2 番目の問題は、チェックを行っCHANGED_INVENTORY
ていないことです。なぜチェックしないのかよくわかりません。この 2 番目の問題をフォローアップすると、ランダムなノートカードの行番号を取得する質問をして、範囲内でランダムな回答を返すのはなぜですか? そして、ノートカードが変わったらどうしますか? コードとメモカードを変更しますか? これは私には冗長に思えます。
NOTECARD という名前colors
またはスクリプトで設定したもの:
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);
}
}
}
さらに詳しい情報:
読んでいるノートカードは必ずしも同じプリムにある必要はありません。ノートカードの名前がわかっている場合はUUID
、譲渡可能 (かつ削除されていない) 限り、そのメモカードから読み取ることができます。ノートカードの内容を変更して保存すると、新しい内容が別 UUID
の に保存されることに注意してください。しかし、それほど熟練している場合は、テキストを Web サービスに保存して、そこからテキスト スニペット カウントとテキスト スニペットを取得することもできます。