BSL:Introduction: Difference between revisions

added comments and syntax sections; added some bold text to emphasize certain examples; a lot of this material is about to get rearranged
m (→‎Variables: typesetting)
(added comments and syntax sections; added some bold text to emphasize certain examples; a lot of this material is about to get rearranged)
Line 1: Line 1:
{{TOCfloat}}
{{TOCfloat}}
Like any typical scripting or programming language, BSL scripts consist of [[#Files|plain-text files]] which use [[#Logic|branching logic]] and [[#Operators|various operators]] to process [[#Types|data]] with [[#Functions|functions]] that act upon [[#Variables|variables]]. Click one of those words to jump to the brief explanations of those terms below, but if you are new to programming, you'll also want to read the "Details" pages that are linked to.
Like any typical scripting or programming language, BSL scripts consist of [[#Comments|commented]] [[#Files|plain-text files]] which use [[#Logic|branching logic]] and [[#Operators|various operators]] according to a [[#Syntax|strict syntax]] to process [[#Types|data]] with [[#Functions|functions]] that act upon [[#Variables|variables]]. Click one of those words to jump to the brief explanations of those terms below, but if you are new to programming, you'll also want to read the "Details" pages that are linked to.


For those familiar with C, the syntax is similar and you will feel at home in BSL, but BSL's feature set is stripped down to a degree that is simpler than even most scripting languages. Fortunately, this makes BSL fast to learn.
For those familiar with C, the syntax is similar and you will feel at home in BSL, but BSL's feature set is stripped down to a degree that is simpler than even most scripting languages. Fortunately, this makes BSL fast to learn.
{{clearall}}
{{clearall}}
==Comments==
Comments are notes from the programmer to explain some code. They are supposed to be placed above or after a line of code:
'''# The following block of code calculates the meaning of life'''
if [block of code begins here]
var int a = 0; '''# this global is also modified in my_cutscenes.bsl'''
In documentation outside of source code or script files, such as this page, they are still 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.
==Files==
==Files==
When Oni loads a level, it also loads and parses all .bsl files in the folder in [[IGMD]] which contains that level's scripts (the name of the folder being specified by the level's [[ONLV]] resource). Like C, the code automatically begins running with the function called "main", regardless of which .bsl file it is found in.
When Oni loads a level, it also loads and parses all .bsl files in the folder in [[IGMD]] which contains that level's scripts (the name of the folder being specified by the level's [[ONLV]] resource). Like C, the code automatically begins running with the function called "main", regardless of which .bsl file it is found in.
Line 40: Line 51:
We're all familiar with the four basic arithmetical operations: addition, subtraction, multiplication and division. Well, BSL only has the first two :-) They're the "+" and "-" symbols, as you would expect. Example:
We're all familiar with the four basic arithmetical operations: addition, subtraction, multiplication and division. Well, BSL only has the first two :-) They're the "+" and "-" symbols, as you would expect. Example:


  counter = counter + 1 # increases counter's value by one
  counter = counter + 1; # increases counter's value by one


To compare two values, you use relational operators, like this:
To compare two values, you use relational operators, like this:


  if (counter < 3)
  if (counter < 3)
==Syntax==
:''See [[BSL:Syntax]] for details.''
BSL allows two kinds of syntax to be used somewhat interchangeably, which can be called "shell-style" and "C-style". This can be confusing; it's recommended to stick to C-style consistently in your scripting, because it's more explicit and therefore safer.
Braces define blocks of code, whether the block is a function or a conditional:
func void some_function(void)
'''{'''
    # some code
'''}'''
if (condition)
'''{'''
    # some code
'''}'''
Semicolons end a statement and parentheses enclose function parameters, but only in C-style BSL:
func void some_function(void)
{
    var int a;
    a = 4;
    sleep(60);
    if (something)
    {
      # some code
    }
}
The same code would look like this in shell-style BSL:
func some_function
{
    var int a
    a = 4
    sleep 60
    if (something)
    {
      # some code
    }
}
Notice that parentheses are always needed with "if" statements, and also note that the type (see "Types" below) of the function and its parameters can be left out in shell-style BSL (this meaning "void" implicitly), but it doesn't have to be.


==Types==
==Types==
Line 82: Line 138:
You've already seen some example of variable declaration. One more thing to be aware of is that variables can be initialized upon declaration:
You've already seen some example of variable declaration. One more thing to be aware of is that variables can be initialized upon declaration:


  var int y = 9;
  var int y '''= 9''';


or not:
or not: