Jump to content

BSL:Manual: Difference between revisions

203 bytes added ,  11 December 2023
m
wording improvements
m (→‎Variables: didn't realize that we broke up the flow of the "… …" series with that interpolated remark on initialization)
m (wording improvements)
Line 123: Line 123:
When declaring a variable, the statement must begin with "var" and the type of the variable:
When declaring a variable, the statement must begin with "var" and the type of the variable:


  var int i = 0; # declare a variable named "i" and initialize it to the value 0
  var int i = 0; # declare a variable named "i" and initialize it to the value zero


You don't have to initialize variables (set them to a value when declaring them), but it's usually a good idea. If you don't initialize, the engine will use a default value: "false" for bools, "0" for ints and floats, and "" (an empty string) for strings.
You don't have to initialize variables (set them to a value when declaring them), but it's usually a good idea. If you don't initialize, the engine will use a default value: "false" for bools, "0" for ints and floats, and "" (an empty string) for strings.
Line 135: Line 135:


==Data types==
==Data types==
When declaring a variable or a function (more on this under {{SectionLink||Functions}} and {{SectionLink||Variables}}), you must specify the type of data it contains, accepts, or returns. You can choose from "bool" (can be "true" or "false", but see warning under the "bool" section), "int" (can be any whole number), "float" (a value with a decimal point), or "string" (some text).
When declaring a variable (more on this under {{SectionLink||Variables}}), you must specify the type of data it contains. When declaring a function (more on this under {{SectionLink||Functions}}), you can optionally specify the type of data it accepts and returns. You can choose from "bool" (can be "true" or "false", but see warning under {{SectionLink||bool}}), "int" (can be any whole number), "float" (a value with a decimal point), or "string" (some text).


  var '''int''' x;
  var '''int''' x;
Line 147: Line 147:


===void===
===void===
See {{SectionLink||Functions}}. Variables cannot be type "void".
See {{SectionLink||Functions}} for the meaning of "void". Variables cannot be of type "void".


===bool===
===bool===
'''(Note: See warning at bottom of this section; the bool type is not recommended for variables.)''' A [[wp:Boolean data type|Boolean]] number has one of two possible states. Thus, a bool can be assigned a value with the keywords "true" and "false".
'''(Note: See warning at bottom of this section; the bool type is not recommended for variables.)''' A [[wp:Boolean data type|Boolean]] number has one of two possible states. Thus, a bool can be assigned a value with the keywords "true" and "false".


  var bool example = true;
  var '''bool''' example = true;
  func bool are_we_there_yet(void)
  func '''bool''' are_we_there_yet(void)
  {
  {
     ...
     ...
Line 163: Line 163:
  if (example eq true)
  if (example eq true)


However, Windows Oni will fail to evaluate bare references to bool variables, and testing indicates the possibility of other bugs existing in Windows Oni's handling of bools. Additionally, Bungie West uses virtually no bool variables in Oni's scripts (though they do use built-in functions that return bool values). Since ints are handled much more reliably, it's recommended to use int for variables instead of bool; the values 1 and 0 can represent "true" and "false".
However, Windows Oni will fail to evaluate bare references to bool variables, and testing indicates the possibility of other bugs existing in Windows Oni's handling of bools. Additionally, Bungie West uses virtually no bool variables in Oni's scripts (though they do use built-in functions that return bool values). Since ints are handled much more reliably, it's recommended to use int for variables instead of bool; the values 1 and 0 can be used to represent "true" and "false".


===int===
===int===
A 32-bit signed [[wp:Integer|whole number]]; that is, it can be positive, negative or zero, with a maximum value of 2,147,483,647 and a minimum value of -2,147,483,648.
A 32-bit signed [[wp:Integer|whole number]]; it can be positive, negative or zero, with a maximum value of 2,147,483,647 and a minimum value of -2,147,483,648.


  var int example = -1000;
  var '''int''' example = -1000;
  func int get_enemy_count(void)
  func '''int''' get_enemy_count(void)
  {
  {
     ...
     ...
  }
  }


Note that the number wraps around, which means that once it passes its maximum or minimum value, it starts over from the other end. For instance, the function...
Note that the value wraps around, which means that once it passes its maximum or minimum value, it starts over from the other end. For instance, the function...


  func void overflow_test(void)
  func void overflow_test(void)
Line 182: Line 182:
  }
  }


...will print "int32: 2147483647" to screen when it runs.
...will print the positive number "int32: 2147483647" to screen when it runs.


Adding a float or a bool to an int yields odd and very high results, even if the float or bool could in theory be seamlessly converted to an int. Assigning a float to an int correctly assigns the truncated integer to the int. Assigning a bool to an int works correctly, the bool being treated as either 0 or 1.
Adding a float or a bool to an int yields odd and very high results, even if the float or bool could in theory be seamlessly converted to an int. Assigning a float to an int correctly assigns the truncated integer to the int. Assigning a bool to an int works correctly, the bool being treated as either 0 or 1.
Line 189: Line 189:
A [[wp:Floating-point arithmetic|floating-point]] number. Bungie West actually never used floats in their scripts for Oni, which explains why there are certain rough aspects to them. For instance, floats are limited to six decimal places of precision:
A [[wp:Floating-point arithmetic|floating-point]] number. Bungie West actually never used floats in their scripts for Oni, which explains why there are certain rough aspects to them. For instance, floats are limited to six decimal places of precision:


  var float good_example = 10.5; # returns as "10.500000"
  var '''float''' good_example = 10.5; # returns as "10.500000"
  var float too_precise = 3.141592653589793; # returns as "3.141593"
  var '''float''' too_precise = 3.141592653589793; # returns as "3.141593"


Thus, adding a value below the sixth decimal place to a float will have no effect, e.g. 3.000000 + 0.0000001 = 3.000000. Additionally, adding an int (or a bool) to a float has no effect:
Thus, adding a value below the sixth decimal place to a float will have no effect, e.g. 3.000000 + 0.0000001 = 3.000000. Additionally, adding an int value (or a bool value) to a float has no effect:


  func float test_float_addition(void)
  func float test_float_addition(void)
Line 211: Line 211:


===string===
===string===
A sequence of textual characters. BSL does not provide any explicit means for manipulating strings (concatenation, trimming, etc.), though see below for a simple trimming hack. It also does not provide a reliable method of string comparison, though perhaps due to a bug rather than an oversight (see [[BSL:String comparison|HERE]] for more information).
A sequence of textual characters. BSL does not have any built-in way of performing the string manipulations that you would expect from a scripting language, such as concatenation and trimming, though see below for a simple trimming hack. It also does not provide a reliable method of string comparison (see [[BSL:String comparison|HERE]] for a detailed examination).


  var string example = "hello";
  var '''string''' example = "hello";


Trying to give a string a non-textual value (bool/int/float) gives the error "illegal type convertion" (sic). Trying to add two strings together simply crashes the game, rather than concatenating them as it would in some other languages. Adding a float to a string crashes too. Bools have no effect whatsoever.
Trying to give a string a non-textual value (bool/int/float) gives the error "illegal type convertion" (sic). Trying to add two strings with a '+' operator crashes the game, rather than concatenating them as it would in some other languages. Adding a float to a string crashes too. Bools have no effect whatsoever.


Trying to add an int value to a string creates some cool and unexpected results. For instance, adding a number between 5 and 11 removes the first character from the string. A number greater than or equal to 12 removes the first two, and so on. Subtraction does not yield the reverse effect, even if you try it with the maximum int value (in which case, Oni crashes). Subtracting numbers can, however, result in printing random strings from Oni's memory, such as BSL function names!
Trying to add an int value to a string creates some cool and unexpected results. For instance, adding a number between 5 and 11 removes the first character from the string. A number greater than or equal to 12 removes the first two, and so on. Subtraction does not yield the reverse effect, even if you try it with the maximum int value (in which case Oni crashes). Subtracting numbers can, however, result in printing random strings from Oni's memory, such as BSL function names!


"(null)" is the value that strings assume when they have not been given a value yet; it cannot be assigned to a string manually.
"(null)" is the value that strings assume when they have not been given a value yet; it cannot be assigned to a string manually.