我有一张记事卡,每行都有不同的单词,我希望能够从这些行中随机选择。我怎样才能做到这一点?
问问题
4053 次
2 回答
2
首先,正如您提到的,您需要一张记事卡。对于此示例,我使用了一个名为“colors”的内容,其内容如下:
red
blue
green
yellow
orange
purple
在存在该记事卡的情况下,每次触摸 prim 时,以下脚本将读取并聊天卡中的随机行。
//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);
}
}
}
于 2009-06-24T19:46:23.040 回答
0
目前尚不清楚为什么您在上面给自己的答案中添加一个然后再次减去它来进行这种不必要的数学运算。如果你想确保你有一个更随机的数字,因为llFrand
你可以做的随机性存在已知问题(不检查数字是偶数还是奇数):
integer max;
integer random = llFrand((integer)(max/2)) + llFrand((integer)(max/2));
上面代码的第二个问题是您没有进行检查CHANGED_INVENTORY
,我不太确定您为什么不这样做。跟进第二个问题,你为什么要问一个问题来获得一个随机的记事卡行号并给出一个提供范围内随机数的答案?如果记事卡发生变化,你会怎么做?更改代码和记事卡?这对我来说似乎是多余的。
NOTECARD 命名colors
或您在脚本中设置的任何内容:
blue
red
green
yellow
black
同一个 prim 中的脚本:
// 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 服务上,并从那里获取文本片段计数和文本片段。
更多关于官方第二人生维基。
于 2013-06-03T07:55:44.570 回答