User Tools

Site Tools


scripting:basic_syntax

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
scripting:basic_syntax [2012/03/26 00:18] mitscripting:basic_syntax [2025/05/28 16:34] (current) – external edit 127.0.0.1
Line 31: Line 31:
   * ''<=''  ('' < = '' )     * ''<=''  ('' < = '' )  
   * ''>=''   * ''>=''
 +
 +===== Loops =====
 +''while'' loops are supported, for example...
 +<codedoc>
 +$loop = 0
 +while ( $loop < 10 )
 +{
 +   DoMyFunction( $loop )
 +   $loop += 1
 +}
 +</codedoc>
  
 ===== Comments ===== ===== Comments =====
Line 48: Line 59:
  
 <codedoc> <codedoc>
-     $var = $var + 1+     $var = 1
      $newvar = $var * 100      $newvar = $var * 100
 +     $var += 1
 </codedoc> </codedoc>
  
-Currently only the 4 basic operators ( '+', '-''/''*' ) are supported.+Currently the 4 basic operators ( ''+, -, /, *'' ) are supported, along with ( ''+=, -=, /=, *='' ) for operating on the LHS variable (e.g. ''$var += 1'' is equivalent to ''$var = $var + 1''.
  
-===== Local & Global Variables =====+===== Local, Module & Global Variables =====
  
 Local variables can be used at any point without advance declaration. They remain in scope throughout the event or function as you'd expect.  Local variables can be used at any point without advance declaration. They remain in scope throughout the event or function as you'd expect. 
-Global variables (those that are instantiated outside of an event or function) remain in scope for all events and functions until the script is reloaded or the server is restarted.+Module variables (those that are instantiated outside of an event or function) remain in scope for all events and functions within the file until the script is reloaded or the server is restarted
 +Global variables are declared using the keyword 'global' and are available to all functions and events in any file.
  
 Example Local variable use : Example Local variable use :
Line 70: Line 83:
 } }
 </codedoc> </codedoc>
-Example global variable use :+Example module variable use :
 <codedoc> <codedoc>
 $mNumberOfBongoPlays = 0 $mNumberOfBongoPlays = 0
Line 79: Line 92:
 } }
 </codedoc> </codedoc>
 +Example global variable use :
 +<codedoc>
 +global $kNumberOfBongoPlays = 0
 +Event( "UseItem", "Bongos" )
 +{
 +   $kNumberOfBongoPlays = $kNumberOfBongoPlays + 1
 +   *say $kNumberOfBongoPlays people have played the bongos since the script was last restarted
 +}
 +</codedoc>
 +
 +===== Arrays =====
 +
 +Local, module and global arrays can be created. Arrays can contain values and text. Array indexing starts from 1, which is a bit annoying if you've got any sense at all but hey ho.
 +
 +Arrays are declared and referenced as shown :
 +
 +<codedoc>
 +$maTestArray[] = 
 +{
 +    100,   "Item 1",
 +    200,   "Item 2", 
 +}
 +
 +Event( "&command", "TestArray" )
 +{
 +   *say The first value in the array is $maTestArray[1]
 +   *say The second text item in the array is $maTestArray[4] 
 +}
 +</codedoc>
 +
 +.. which would print 'The first value in the array is 100' followed by 'The second text item in the array is Item 2'.
 +
 +
 +>> * [[Scripting:Other Language Features|Other Important Language Features]]
 +
 +
 +
  
scripting/basic_syntax.1332721121.txt.gz · Last modified: (external edit)