Unofficial LSL Reference

[[language:state]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

language:state [2018-10-11 10:20 SLT]
sei typo
language:state [2020-06-15 12:58 SLT] (current)
sei more wording
Line 33: Line 33:
 where `<​state_name`>​ can be $kw[default] or the name of any existing state. This statement causes a state switch, interrupting execution of the current function or event. where `<​state_name`>​ can be $kw[default] or the name of any existing state. This statement causes a state switch, interrupting execution of the current function or event.
  
-When a state switch statement is found, a $kw[return] statement is automatically inserted at that point in the code, and when the currently executed ​event finalizes, if the state is NOT the current state, the state switch happens. This triggers two events: first, the $lev[state_exit] event for this state; then, the $lev[state_entry] event for the state changed to.+When a state switch statement is found, a $kw[return] statement is automatically inserted at that point in the code, and when the current ​event finalizes, if the state is NOT the current state, the state switch happens. This triggers two events: first, the $lev[state_exit] event for this state; then, the $lev[state_entry] event for the state changed to.
  
 A state switch statement that specifies the same state where it is executed, causes the interruption of the current event or function, but does not cause any other effects: neither the $ev[state_exit] nor the $ev[state_entry] events of the current state are executed. A state switch statement that specifies the same state where it is executed, causes the interruption of the current event or function, but does not cause any other effects: neither the $ev[state_exit] nor the $ev[state_entry] events of the current state are executed.
Line 89: Line 89:
 ===== Hacks ===== ===== Hacks =====
  
-State switches are designed to be used only within event declarations. They work by flagging ​the state change and adding a $lkw[return] statement at the point where the switch statement is. The flag is checked when the currently executed ​event terminates. That creates the illusion that the state switch acts as an immediate jump to another state, stopping execution of the current code.+State switches are designed to be used only within event declarations. They work by signalling ​the state change and adding a $lkw[return] statement at the point where the state switch statement is. The signal ​is checked when the current ​event terminates. That creates the illusion that the state switch acts as an immediate jump to another state, stopping execution of the current code.
  
-The compiler ​checks ​that there is no state switch statement within a user function; however, due to a bug, the check fails when the statement is somewhere within the block associated to:+This $kw[return] trick, however, does not work in user-defined functions, because it would return to the caller, rather that completely interrupting the currently running event. Since it doesn'​t work in these circumstances,​ it can't be properly supported in UDFs easily, and rather than adding the necessary complexity for adding proper support for it, LL chose to not support it in UDFs at all, and implemented a check in the compiler ​to ensure ​that there is no state switch statement within a user function
 + 
 +However, due to a bug, the check fails when the statement is somewhere within the block associated to:
  
   * an $lkw[if] statement that has no $lkw[else] part,   * an $lkw[if] statement that has no $lkw[else] part,
Line 98: Line 100:
   * a $lkw[do] statement.   * a $lkw[do] statement.
  
-allowing us to use state statements even within user functions+thus allowing us to exploit that bug and use state statements even within user functions.
  
 For example: For example:
Line 115: Line 117:
 </​code>​ </​code>​
  
-Note however ​that, against normal expectations,​ this will //not// jump to the specified state directly. Keep in mind that it works by flagging the state switch and inserting a return statement; this means that it will return to the caller of the function, which may be another function or an event. Only when the event within which these functions are executed terminates, will the actual state switch happen.+But note that, against normal expectations,​ this will //not// jump to the specified state directly. Keep in mind that it works by flagging the state switch and inserting a $kw[returnstatement; this means that it will return to the caller of the function, which may be another function or an event. Only when the event within which these functions are executed terminates, will the $ev[state_exit] event execute, if it exists, and then the actual state switch ​will happen.
  
 To illustrate this, consider the following example: To illustrate this, consider the following example:
Line 147: Line 149:
 </​code>​ </​code>​
  
-The output of that script will be "Oops, this is visible!"​ followed by "Yep, it changed state alright"​. The line that displays "You can't see me" isn't executed, because the state statement causes a return before reaching it. The line that displays "Oops, this is visible!",​ however, is executed when the function returns, therefore it will still be executed //after// the switch statement. When the $ev[state_entry] event finishes, the system will notice that there'​s a state switch pending, and change to ''​state another''​.+The output of that script will be "Oops, this is visible!"​ followed by "Yep, it changed state alright"​. The line that displays "You can't see me" isn't executed, because the state statement causes a return before reaching it. The line that displays "Oops, this is visible!",​ however, is executed when the function returns, therefore it will still be executed //after// the state switch statement. When the $ev[state_entry] event finishes, the system will notice that there'​s a state switch pending, and change to ''​state another''​.
  
 When using this hack, it's best to use it within a function that does not return any values. If the function returns a value, the outcome depends on the virtual machine used and on the type returned. When using this hack, it's best to use it within a function that does not return any values. If the function returns a value, the outcome depends on the virtual machine used and on the type returned.