2009-06-24 33 views
3

私は各行に別の言葉でノートカードを持っています。私はその行からランダムに選択できるようにしたいと思います。どうやってやるの?LSLのノートカードからランダムな行を読み取るにはどうすればよいですか?

+0

これは、Script Academyグループの質問に対する回答です。後世のために保存するためにここに掲示されています。 – btubbs

+0

Second LifeのLinden Scripting Languageについての質問ですから、これを投票しています! :) –

答えて

2

まず、あなたが言及したように、ノートカードが必要です。私は、次の内容の1という名前の「色」を使用しています。この例では、:

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); 
     } 
    } 
} 
0

あなたは1を追加し、後であなたが上記の自分自身を与えるの答えでそれを再度substractingと、このような不必要な計算を行う理由は明らかではありません。 あなたは(数が偶数か奇数か確認せずに)行うことができllFrandのランダム性に関する既知の問題があるとして、あなたはより多くのランダムな番号を持っていることを確認したい場合:

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

あなたとの第二の問題上記のコードは、あなたがCHANGED_INVENTORYに対してチェックしていないということですこの2番目の問題をフォローアップするには、なぜランダムなノートカードの行番号を取得し、ある範囲内でランダムを返す答えを出すのかという質問をするのはなぜですか?ノートカードが変わったらどうしますか?コードとノートカードを変更しますか?これは私には重複しているようです。 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サービスに保存し、そこからテキストスニペット数とテキストスニペットを取得することもできます。

詳細はthe official Second Life wikiです。

関連する問題