Difference between revisions of "State/ja"

From Second Life Wiki
Jump to navigation Jump to search
(moved State/ja to 状態: changing japanses page names..)
 
m
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
#REDIRECT [[状態]]
{{Multi-lang}}
{{LSL Header/ja|ml=*}}
{{LSLC/ja|}}
{{RightToc}}
<div id="box">
<h2>説明</h2>
<div style="padding: 0.5em">
LSLでは、ほとんどのスクリプトは何らかの入力を受け取るか、環境で何らかの変化を検出するまで、アイドル状態にあります。どの瞬間も、スクリプトは何らかのstateにあり、プログラマーによって定義された多くのスキームと一致したイベント、あるいはインプットに反応するでしょう。しかしながら、スクリプトは二つ以上の異なるstateを包含することができ、状態によってイベントやインプットに対して異なる反応を定義できます。
 
中心的なstateはdefault stateです。スクリプトはコンパイルされた時、リセットもしくはリロードされた時、まずdefaultのstateが実行されます。default stateの定義の後に続けて、スクリプトで処理されるイベントがどのような入力、変更によって発生するのかを定義する、新たなstateを追加することができます。
</div>
</div>
{{#if:
{{#vardefine:p_jump_state_desc|実行するstate名です。}}
{{#vardefine:p_label_state_desc|state名です。}}
{{#vardefine:p_events_desc|一つ以上の{{LSLGC|Events/ja|イベント}}です。}}
 
{{#vardefine:header_title|default { {{LSL Param|events}} } }}
{{#vardefine:header_text|{{{!}}
{{LSL DefineRow|event|events|{{#var:p_events_desc}}}}
{{!}}}
default state の定義です。
}}
{{#vardefine:constants_nb|
<div id="box">
<h2>state {{LSL Param|label_state|target}} { {{LSL Param|events}} }</h2>
<div style="padding: 0.5em">
{{{!}}
{{LSL DefineRow|label|target|{{#var:p_label_state_desc}}}}
{{LSL DefineRow|event|events|{{#var:p_events_desc}}}}
{{!}}}
{{LSLP|target}} state の定義です。
</div>
</div>
<div id="box">
<h2>state {{LSL Param|jump_state|target}};</h2>
<div style="padding: 0.5em">
{{{!}}
{{LSL DefineRow|label|target|{{#var:p_jump_state_desc}}}}
{{!}}}<br/>
''state target;''が実行時に発生し、現在 のstate と target state が異なる場合、以下の順に動作します。
#現在のstate内に{{LSLG/ja|state_exit}}が'''存在する場合は実行され'''、イベントキューは消去されます。
#targetにstateが変わると、'''全ての'''{{LSLG/ja|listen}}は解除されます。
#target stateに{{LSLG/ja|state_entry}}が'''存在する場合は実行されます。'''
もしtarget state が 現在 の state と同じ場合、'''state は変わらず、またどんな副作用もありません。'''
</div>
</div>
}}
 
{{#vardefine:examples|
<source lang="lsl2">default {
 
  touch_end(integer a)
  {
      state hello;
  }
 
}
 
state hello {
 
  state_entry()
  {
      llOwnerSay("Hello");
      state default;
  }
  state_exit()
  {
      llOwnerSay("Goodbye");
  }
 
}
</source>
 
<source lang="lsl2">
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'");
    }
}
</source>
* '''[http://www.youtube.com/watch?v=49vj9HEI2OY/Basic Tutorial on States, Events, and Functions]
}}
 
{{#vardefine:notes|{{#var:notes}}
*これまで溜っていたイベントを破棄するのを防ぐために、{{LSLG/ja|state}} target コマンドを {{LSLG/ja|timer}} イベントに移動して、{{LSLG/ja|llSetTimerEvent}}( 0.01 ) で呼び出します。それから新しい {{LSLG/ja|state}} の {{LSLG/ja|state_entry}} でタイマーを止めます。これが心配な場合に {{LSLG/ja|state_exit}} を使用しないでください。
}}
 
{{#vardefine:caveats|*stateを変える上で
**全ての[[llListen/ja|listen]]はリリースされます。
**イベントキューは消去されます。
**[[llSensorRepeat|Repeating sensors]] はリリースされます。
**[[llSetTimerEvent|timer event clock]] はクリアされません。
**つまり、次の state に timer イベントがあり、前の state でタイマーが設定されていると、以前の state で設定された遅延時間で新しい state の timer イベントが発生することになります。
*default stateはほかのstateに行く前に定義すべきです。
*state範囲内にユーザ関数、あるいはユーザ変数を設定することはできず、 イベントだけがstate範囲内で定義できます。
*ユーザ定義(グローバル)関数は state を変えることができません。コンパイラは 'ERROR: Global functions can't change state' というエラーを出すでしょう。注意: 以前はグローバル関数でも 'if' 文の中なら state を変えることができましたが、この"裏技"はもう使えません。
}}
 
{{#vardefine:helpers|}}
 
{{#vardefine:also_header|<h3>Keywords</h3>
{{{!}}
{{LSL DefineRow||{{LSLG/ja|jump}}|}}
{{LSL DefineRow||{{LSLG/ja|return}}|}}
{{!}}}
}}
 
{{#vardefine:also_footer|}}
 
{{#vardefine:also_functions|}}
 
{{#vardefine:also_events|
{{LSL DefineRow||{{LSLG/ja|state_entry}}|}}
{{LSL DefineRow||{{LSLG/ja|state_exit}}|}}
}}
 
{{#vardefine:also_articles|}}
 
{{#vardefine:also_tests|}}
 
{{#vardefine:location|}}
 
}}{{LSL Generic}}{{LSLC/ja|Flow Control|3={{#var:lang}}}}{{LSLC/ja|Script|3={{#var:lang}}}}{{LSLC/ja|State|3={{#var:lang}}}}

Latest revision as of 09:17, 26 September 2023

説明

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