State/ja

From Second Life Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

説明

LSLでは、ほとんどのスクリプトは何らかの入力を受け取るか、環境で何らかの変化を検出するまで、アイドル状態にあります。どの瞬間も、スクリプトは何らかのstateにあり、プログラマーによって定義された多くのスキームと一致したイベント、あるいはインプットに反応するでしょう。しかしながら、スクリプトは二つ以上の異なるstateを包含することができ、状態によってイベントやインプットに対して異なる反応を定義できます。

中心的なstateはdefault stateです。スクリプトはコンパイルされた時、リセットもしくはリロードされた時、まずdefaultのstateが実行されます。default stateの定義の後に続けて、スクリプトで処理されるイベントがどのような入力、変更によって発生するのかを定義する、新たなstateを追加することができます。

default { events }

default { events }
• event events 一つ以上のイベントです。

default state の定義です。

state target { events }

• label target state名です。
• event events 一つ以上のイベントです。

target state の定義です。

state target;

• label target 実行するstate名です。

state target;が実行時に発生し、現在 のstate と target state が異なる場合、以下の順に動作します。

  1. 現在のstate内にstate_exit存在する場合は実行され、イベントキューは消去されます。
  2. targetにstateが変わると、全てのlistenは解除されます。
  3. target stateにstate_entry存在する場合は実行されます。

もしtarget state が 現在 の state と同じ場合、state は変わらず、またどんな副作用もありません。

Caveats

  • stateを変える上で
    • 全てのlistenはリリースされます。
    • イベントキューは消去されます。
    • Repeating sensors はリリースされます。
    • timer event clock はクリアされません。
    • つまり、次の state に timer イベントがあり、前の state でタイマーが設定されていると、以前の state で設定された遅延時間で新しい state の timer イベントが発生することになります。
  • default stateはほかのstateに行く前に定義すべきです。
  • state範囲内にユーザ関数、あるいはユーザ変数を設定することはできず、 イベントだけがstate範囲内で定義できます。
  • ユーザ定義(グローバル)関数は state を変えることができません。コンパイラは 'ERROR: Global functions can't change state' というエラーを出すでしょう。注意: 以前はグローバル関数でも 'if' 文の中なら state を変えることができましたが、この"裏技"はもう使えません。

Examples

default {

   touch_end(integer a)
   {
       state hello;
   }

}

state hello {

   state_entry()
   {
       llOwnerSay("Hello");
       state default;
   }
   state_exit()
   {
       llOwnerSay("Goodbye");
   }

}
default
{
    state_entry()
    {
        llSay(0,
            "You either just saved the script after editing it"
            + "\nand/or the script (re)entered the default state.");

        // white and opaque text
        llSetText("Click to change states", <1.0, 1.0, 1.0>, (float)TRUE);
    }

    touch_end(integer num_detected)
    {
        // Note: NEVER do a state change from within a touch_start event -
        // - that can lead to the next touch_start on return to this state to be missed.
        // Here we do the state change safely, from within touch_end
        state two;
    }

    state_exit()
    {
        llSay(0, "The script leaves the default state.");
    }
}

state two
{
    state_entry()
    {
        llSay(0, "The script entered state 'two'");
        state default;
    }

    state_exit()
    {
        llSay(0, "The script leaves state 'two'");
    }
}

Notes

  • これまで溜っていたイベントを破棄するのを防ぐために、state target コマンドを timer イベントに移動して、llSetTimerEvent( 0.01 ) で呼び出します。それから新しい statestate_entry でタイマーを止めます。これが心配な場合に state_exit を使用しないでください。

See Also

Keywords

•  jump
•  return

Events

•  state_entry
•  state_exit