Jump to content

BSL:Manual: Difference between revisions

407 bytes added ,  4 December 2015
explaining declaration and type spec; mentions of other sections are now links to them
(→‎Flow interrupt: moving "insider" statement to Introduction)
(explaining declaration and type spec; mentions of other sections are now links to them)
Line 27: Line 27:
  dmsg("Statement 2")
  dmsg("Statement 2")


The third example is in old-style syntax, which is discussed under "Old vs. new syntax" below.
The third example is in old-style syntax, which is discussed under [[#Old vs. new syntax|Old vs. new syntax]].


===Compound statements===
===Compound statements===
Line 41: Line 41:
  {dmsg("Statement 1"); dmsg("Statement 2"); dmsg("Statement 3");}
  {dmsg("Statement 1"); dmsg("Statement 2"); dmsg("Statement 3");}


The purpose of doing this is to group some statements under a header, which can be either a function declaration (see "Declaration" below) or an "if" statement (see "Conditional" below).
The purpose of doing this is to group some statements under a header, which can be either a function declaration (see [[#Declaration|Declaration) or an "if" statement (see [[#Conditional|Conditional]]).


===Comments===
===Comments===
Line 53: Line 53:
  if [block of code begins here]
  if [block of code begins here]


Do not using trailing comments when not ending statements with semicolons (see "Old vs. new syntax" below for explanation).
Do not using trailing comments when not ending statements with semicolons (see [[#Old vs. new syntax|Old vs. new syntax]] for explanation).


In documentation outside of source code or script files, such as this page, comments are sometimes used to tell the reader something in a way that doesn't break the actual code, if the user should type it in exactly as it appears, comments and all.
In documentation outside of source code or script files, such as this page, comments are sometimes used to tell the reader something in a way that doesn't break the actual code, if the user should type it in exactly as it appears, comments and all.
Line 108: Line 108:


===Declaration===
===Declaration===
Before using a function or a variable for the first time, you need to give some information on it.
====var====
====var====
See "Variables" below for details on using variables.
See [[#Variables/Variables]] for details on using variables.


====func====
====func====
See "Functions" below for details on using functions.
See [[#Functions|Functions]] for details on using functions.


===Type specification===
===Type specification===
:''See [[BSL:Types]] for details.''
When declaring a variable or defining a function, you need to state what type of data is contained in that variable or what kind the function works with.
void, bool, int, float, string


Besides "void", used in function definitions to indicate "no type", these are the types of data that can be assigned to variables, passed to functions, and returned by functions.  
====void, bool, int, float, string====
Besides "void" (used in function definitions to indicate "no type"), these are the types of data that can be assigned to variables, passed to functions, and returned by functions. See [[#Data types|Data types]].


===Conditional===
===Conditional===
Line 129: Line 131:
  }
  }


The condition in the parentheses needs to evaluate to "true" or "false". For examples of operators, see the "Operators" section.
The condition in the parentheses needs to evaluate to "true" or "false". For examples of operators, see [[#Operators|Operators]].


The condition can technically also evaluate to an int value, which then is converted to true/false:
The condition can technically also evaluate to an int value, which then is converted to true/false:
Line 137: Line 139:
  if (7 - 8) # evaluates to true because it is non-zero
  if (7 - 8) # evaluates to true because it is non-zero


Be aware that BSL does not properly respect scope for blocks of code under "if" statements, so some actions like setting variables and using "return" will occur even if the surrounding "if" condition is false (see "Flow interrupts" below for an example). A workaround for setting variables conditionally is to make the variable global, and under the "if" statement call a function that modifies that variable; the function call will not fire unless the "if" condition is true.
Be aware that BSL does not properly respect scope for blocks of code under "if" statements, so some actions like setting variables and using "return" will occur even if the surrounding "if" condition is false (see [[#Flow interrupt|Flow interrupt]] for an example). A workaround for setting variables conditionally is to make the variable global, and under the "if" statement call a function that modifies that variable; the function call will not fire unless the "if" condition is true.


The logical operators "and" and "or" can be used to string together multiple conditions:
The logical operators "and" and "or" can be used to string together multiple conditions:
Line 181: Line 183:
The statements under "else" will never run. The "return" will actually kick in and the function will exit, even though the surrounding control statement did not evaluate to true.
The statements under "else" will never run. The "return" will actually kick in and the function will exit, even though the surrounding control statement did not evaluate to true.


However, "return" is still useful for returning data to a calling function; see "Functions" below.
However, "return" is still useful for returning data to a calling function; see [[#Functions|Functions]].


====sleep====
====sleep====
Line 191: Line 193:


===Loop===
===Loop===
BSL has no "for" or "while" statement, but "schedule-repeat-every" (see "Multi-threading" below) can be used as a poor-man's loop (as long as you realize that there's no way to exit early).
BSL has no "for" or "while" statement, but "schedule-repeat-every" (see [[#Concurrency|Concurrency]]) can be used as a poor-man's loop (as long as you realize that there's no way to exit early).


===Multi-threading===
===Multi-threading===
====fork, schedule-at, schedule-repeat-every====
====fork, schedule-at, schedule-repeat-every====
See "Functions" below for the use of these keywords.
See [[#Concurrency|Concurrency]] for the use of these keywords.


===Obsolete===
===Obsolete===
Line 237: Line 239:
You'll note that there is no multiplication or division. Bungie West was only concerned with creating enough scripting power to drive Oni, and they did not need "higher math" for this.
You'll note that there is no multiplication or division. Bungie West was only concerned with creating enough scripting power to drive Oni, and they did not need "higher math" for this.


See "Data types" to learn the details of how operators work between different data types.
See [[#Data types|Data types]] to learn the details of how operators work between different data types.


===Relational===
===Relational===
Line 292: Line 294:


==Data types==
==Data types==
When declaring a variable or a function (more on this under the "Functions" and "Variables" sections below), you must specify the type of data it contains, accepts, or returns:
When declaring a variable or a function (more on this under the [[#Functions|Functions]] and [[#Variables|Variables]] sections), you must specify the type of data it contains, accepts, or returns:


  var int x;
  var int x;
Line 300: Line 302:


===void===
===void===
See "Functions" below. Variables cannot be type "void".
See [[#Functions|Functions]]. Variables cannot be type "void".


===bool===
===bool===
Line 471: Line 473:


===Returning===
===Returning===
As mentioned under "Flow interrupts" above, "return" exits the function at that point. Because of the bug documented in that section, you cannot exit early from a function under some condition. Since "return" can thus only be placed at the end of a function, there is no point in using it at all unless you are going to pass back a value by placing a variable name after "return":
As mentioned under [[#Flow interrupt|Flow interrupt]], "return" exits the function at that point. Because of the bug documented in that section, you cannot exit early from a function under some condition. Since "return" can thus only be placed at the end of a function, there is no point in using it at all unless you are going to pass back a value by placing a variable name after "return":


  func int add_ten(int input)
  func int add_ten(int input)