BSL:Introduction: Difference between revisions

From OniGalore
Jump to navigation Jump to search
(okay, I was going to say a lot more, but that will have to wait until later)
m (wording and section link improvements; don't link to the unfinished BSL listing project)
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Like any scripting or programming language, BSL uses branching logic and logical operators to build functions which process data that is stored in variables.
This page gives a brief look at [[BSL]], Oni's scripting language. For those with scripting or programming experience, BSL's syntax and concepts will be very familiar, but BSL's feature set is simpler than most scripting languages. For details on any aspect of the language, as well as documentation of potentially-serious quirks in certain features of the language, read the full [[BSL:Manual|BSL Manual]].
{{TOClimit|2}}
==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 this folder being specified by the level's [[ONLV]] resource). Like C, the code automatically begins running with the function called "main" at the time of level-load, regardless of which .bsl file it is found in (the convention in Oni's scripts is to place it in a file called *_main.bsl).


==Syntax overview==
Unlike C, there are other "entry points" in the code. For instance, characters can call script functions upon death and trigger volumes can call functions when they are entered or exited. See [[CHAR]], [[OBD:BINA/OBJC/CONS|BINA CONS]], [[OBD:BINA/OBJC/DOOR|BINA DOOR]], [[NEUT]], and [[TRGV]] for all known places in the game data that can trigger BSL functions.
{| border="1" cellpadding="5" cellspacing="0" style="margin-left:auto; margin-right:auto;" align="center"
|+'''BSL syntax overview table'''
|-
| style="border-bottom:3px solid grey;"|
{| border="0"
|+''BSL separators''
|-
| width="100px" | [[BSL:Statements|<tt>;</tt>]]
| width="100px" | [[BSL:Functions|<tt>,</tt>]]
| width="100px" | [[BSL:Comments|<tt>#</tt>]]
| width="100px" | [[BSL:Types|<tt>"</tt>]]
|-
| width="100px" | [[BSL:Statements|<tt>{</tt>]]
| width="100px" | [[BSL:Statements|<tt>}</tt>]]
| width="100px" | [[BSL:Functions|<tt>(</tt>]]
| width="100px" | [[BSL:Functions|<tt>)</tt>]]
|-
| colspan="4" style="border-top:1px solid red; border-right:1px solid red; border-bottom:2px solid red; border-left:1px solid red;" |
Click on a separator for details
|}
|-
| style="border-bottom:3px solid grey;"|
{| border="0"
|+''BSL operators''
|-
| width="100px" | [[BSL:Operators|<tt>+</tt>]]
| width="100px" | [[BSL:Operators|<tt>-</tt>]]
| width="100px" | [[BSL:Variables|<tt>=</tt>]]
| width="100px" | [[BSL:Operators|<tt>!</tt>]]
|-
| width="100px" | [[BSL:Operators|<tt>and</tt>]]
| width="100px" | [[BSL:Operators|<tt>or</tt>]]
| width="100px" | [[BSL:Operators|<tt>eq</tt>]]
| width="100px" | [[BSL:Operators|<tt>ne</tt>]]
|-
| width="100px" | [[BSL:Operators|<tt><</tt>]]
| width="100px" | [[BSL:Operators|<tt>></tt>]]
| width="100px" | [[BSL:Operators|<tt><=</tt>]]
| width="100px" | [[BSL:Operators|<tt>>=</tt>]]
|-
| colspan="4" style="border-top:1px solid red; border-right:1px solid red; border-bottom:2px solid red; border-left:1px solid red;" |
Click on an operator for details
|}
|-
| style="border-bottom:3px solid grey;"|
{| border="0"
|+''BSL keywords''
|-
| width="75px" | [[BSL:Keywords|<tt>at</tt>]]
| width="75px" | [[BSL:Types|<tt>float</tt>]]
| width="75px" | [[BSL:Statements|<tt>if</tt>]]
| width="75px" | [[BSL:Keywords|<tt>repeat</tt>]]
| width="75px" | [[BSL:Types|<tt>string</tt>]]
|-
| width="75px" | [[BSL:Types|<tt>bool</tt>]]
| width="75px" | [[BSL:Keywords|<tt>for</tt>]]
| width="75px" | [[BSL:Types|<tt>int</tt>]]
| width="75px" | [[BSL:Functions|<tt>return</tt>]]
| width="75px" | [[BSL:Keywords|<tt>using</tt>]]
|-
| width="75px" | [[BSL:Statements|<tt>else</tt>]]
| width="75px" | [[BSL:Functions|<tt>fork</tt>]]
| width="75px" | [[BSL:Keywords|<tt>iterate</tt>]]
| width="75px" | [[BSL:Keywords|<tt>schedule</tt>]]
| width="75px" | [[BSL:Variables|<tt>var</tt>]]
|-
| width="75px" | [[BSL:Keywords|<tt>every</tt>]]
| width="75px" | [[BSL:Functions|<tt>func</tt>]]
| width="75px" | [[BSL:Keywords|<tt>over</tt>]]
| width="75px" | [[BSL:Keywords|<tt>sleep</tt>]]
| width="75px" | [[BSL:Types|<tt>void</tt>]]
|-
| colspan="5" style="border-top:1px solid red; border-right:1px solid red; border-bottom:2px solid red; border-left:1px solid red;" |
Click on a keyword for details
|}
|}


[[Category:BSL syntax]]
The optional [[IGMD/global|global]] folder is also loaded for all levels, but any function found in a .bsl file in global/ will be stranded, only accessible by [[Developer Mode|developer console]], unless it is called by a function in a level script or by one of the above types of game data in that level.
 
==Syntax==
===Statements===
BSL offers the choice of a strong and weak syntax in some contexts. Here's a typical statement…
 
dmsg("Hello");
 
…but you can omit the semicolon, parentheses, and also the quotes if your string doesn't have any spaces in it:
 
dmsg Hello
 
Similarly, here's the formal syntax for a snippet of code:
 
func void some_function(void)
{
    var int a;
    a = 4;
    sleep(60);
    if (a eq 4)
    {
      dprint("Hello");
    }
}
 
The same code would look like this in the weak syntax:
 
func void some_function(void)
{
    var int a;
    a = 4
    sleep 60
    if (a eq 4)
    {
      dprint "Hello"
    }
}
 
Notice that parentheses are always needed with "if" statements and semicolons are always needed on variable declarations. You will probably find the strong syntax to be more consistent and safe to use than the weak syntax. Be aware that that mixing strong and weak syntax in one file can cause errors (see {{SectionLink|BSL:Manual|Old vs. new syntax}} for details).
 
===Comments===
Comments are marked with the pound sign:
 
var int a = 0; # this global is also modified in my_cutscenes.bsl
 
==Reserved words==
===Declaration===
You always need to use "var" when declaring a variable and "func" when defining a function:
 
var int a = 0;
func int get_enemy_count(void)
 
Functions do not need to be defined or declared above the place in the code where they are called, unlike in C.
 
===Type specification===
BSL does not support weak typing. You need to specify the type when making declarations, as seen in the above examples. You can choose from "bool" (buggy, so not recommended), "int", "float" (rarely useful because of BSL's limited math operations), and "string". As with C, you use "void" to mean "no type" when defining functions.
 
===Conditional===
BSL supports standard if/else statements, and you can use "and" and "or" to test compound conditions:
 
if ((a < b) and (c > d))
{
    ...
}
else if (a < 0)
{
    ...
}
else
{
    ...
}
 
Beware of a bug in BSL where variable assignments under an "if" statement will fire even when the "if" condition is false. See {{SectionLink|BSL:Manual|Conditional}} for details.
 
===Flow interrupt===
There is no "goto" statement in BSL, nor any loop controls like "continue" or "break" (since there are no proper loops!). There's a "return" keyword in BSL, but a "return" inside of an "if" statement will fire regardless of whether the statement evaluates to true or false. Thus you can only use "return" to return data at the end of the function, not to exit early (see {{SectionLink||Functions}}).
 
There's also a "sleep" command that pauses BSL execution; you pass it a number in ticks:
 
sleep(60); # wait for one second
 
===Loop===
There's no loop keyword like "for" or "while" in BSL, but you can create a loop using one of two methods. First, you can call a function recursively, but BSL has a short stack so don't expect to get more than four levels deep. You can avoid recursion and the related stack limitation by "sleep"ing for a tick or more, and then "fork"ing the call that would otherwise be recursive. See [[BSL:Snippets]] for an example that also makes up for the missing multiplication operator in BSL.
 
Second, you could use schedule-repeat-every:
 
schedule some_function() repeat 50 every 20;
 
...or schedule-at:
 
schedule some_function() at 15;
schedule some_function() at 30;
schedule some_function() at 45;
...
 
Just be aware that the BSL will continue executing without waiting for this "loop" to finish.  Also, there is no way to exit the "loop" early, but some_function() could simply refuse to perform its task if a global variable has changed to false, simulating a loop exit.
 
===Multithreading===
BSL doesn't have a robust solution, but you can perform a certain amount of multithreading using "fork" or "schedule". Please see {{SectionLink|BSL:Manual|Concurrency}} for examples.
 
==Operators==
Like some other languages, BSL differentiates between ''checking for'' equivalency ("eq") and ''setting'' equivalency ("="):
 
if (counter eq 3) ...
i = 4;
 
BSL's operators are pretty standard stuff:
+ -
eq ne < > <= >=
and or !
 
But you'll note that there is no operator for multiplying or dividing! You'll need to create your own loop with addition and subtraction to perform that kind of math.
 
==Data types==
You can choose from "void", "bool", "int", "float", and "string" (but "void" is only allowed when defining a function). The "bool" type is buggy in Windows Oni, so use of "int" is recommended instead.
 
var int a = 1;
# see the "Functions" section below on how to use a data type keyword when defining a function
 
The "int" type is signed 32-bit. See {{SectionLink|BSL:Manual|Data types}} to learn about various limitations on math between different data types in BSL.
 
==Functions==
As mentioned above, functions do not need to be declared or defined before they are called. Defining and calling a function looks exactly like C except for the "func" keyword in the definition:
 
a = get_enemy_count(0);<br />
func int get_enemy_count(int count_dead)
{
    ...
    return(count);
}
 
Once again, please see {{SectionLink|BSL:Manual|Functions}} section to learn about concurrent and recursive calling.
 
==Variables==
As usual, variables can be explicitly initialized:
 
var int y = 9;
 
or not:
 
var int y;
 
In the second case, "y" is automatically initialized to zero.
 
Variables will have global scope if declared outside of a function.
 
==Built-in functions and variables==
Like any game, Oni provides access to a number of hardcoded functions and global variables that can be used to alter the game environment. They are listed on [[BSL:Functions]] and [[BSL:Variables]].
 
[[Category:BSL docs]]

Latest revision as of 00:10, 4 February 2024

This page gives a brief look at BSL, Oni's scripting language. For those with scripting or programming experience, BSL's syntax and concepts will be very familiar, but BSL's feature set is simpler than most scripting languages. For details on any aspect of the language, as well as documentation of potentially-serious quirks in certain features of the language, read the full BSL Manual.

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 this folder being specified by the level's ONLV resource). Like C, the code automatically begins running with the function called "main" at the time of level-load, regardless of which .bsl file it is found in (the convention in Oni's scripts is to place it in a file called *_main.bsl).

Unlike C, there are other "entry points" in the code. For instance, characters can call script functions upon death and trigger volumes can call functions when they are entered or exited. See CHAR, BINA CONS, BINA DOOR, NEUT, and TRGV for all known places in the game data that can trigger BSL functions.

The optional global folder is also loaded for all levels, but any function found in a .bsl file in global/ will be stranded, only accessible by developer console, unless it is called by a function in a level script or by one of the above types of game data in that level.

Syntax

Statements

BSL offers the choice of a strong and weak syntax in some contexts. Here's a typical statement…

dmsg("Hello");

…but you can omit the semicolon, parentheses, and also the quotes if your string doesn't have any spaces in it:

dmsg Hello

Similarly, here's the formal syntax for a snippet of code:

func void some_function(void)
{
   var int a;
   a = 4;
   sleep(60);
   if (a eq 4)
   {
      dprint("Hello");
   }
}

The same code would look like this in the weak syntax:

func void some_function(void)
{
   var int a;
   a = 4
   sleep 60
   if (a eq 4)
   {
      dprint "Hello"
   }
}

Notice that parentheses are always needed with "if" statements and semicolons are always needed on variable declarations. You will probably find the strong syntax to be more consistent and safe to use than the weak syntax. Be aware that that mixing strong and weak syntax in one file can cause errors (see BSL:Manual § Old vs. new syntax for details).

Comments

Comments are marked with the pound sign:

var int a = 0; # this global is also modified in my_cutscenes.bsl

Reserved words

Declaration

You always need to use "var" when declaring a variable and "func" when defining a function:

var int a = 0;
func int get_enemy_count(void)

Functions do not need to be defined or declared above the place in the code where they are called, unlike in C.

Type specification

BSL does not support weak typing. You need to specify the type when making declarations, as seen in the above examples. You can choose from "bool" (buggy, so not recommended), "int", "float" (rarely useful because of BSL's limited math operations), and "string". As with C, you use "void" to mean "no type" when defining functions.

Conditional

BSL supports standard if/else statements, and you can use "and" and "or" to test compound conditions:

if ((a < b) and (c > d))
{
   ...
}
else if (a < 0)
{
   ...
}
else
{
   ...
}

Beware of a bug in BSL where variable assignments under an "if" statement will fire even when the "if" condition is false. See BSL:Manual § Conditional for details.

Flow interrupt

There is no "goto" statement in BSL, nor any loop controls like "continue" or "break" (since there are no proper loops!). There's a "return" keyword in BSL, but a "return" inside of an "if" statement will fire regardless of whether the statement evaluates to true or false. Thus you can only use "return" to return data at the end of the function, not to exit early (see § Functions).

There's also a "sleep" command that pauses BSL execution; you pass it a number in ticks:

sleep(60); # wait for one second

Loop

There's no loop keyword like "for" or "while" in BSL, but you can create a loop using one of two methods. First, you can call a function recursively, but BSL has a short stack so don't expect to get more than four levels deep. You can avoid recursion and the related stack limitation by "sleep"ing for a tick or more, and then "fork"ing the call that would otherwise be recursive. See BSL:Snippets for an example that also makes up for the missing multiplication operator in BSL.

Second, you could use schedule-repeat-every:

schedule some_function() repeat 50 every 20;

...or schedule-at:

schedule some_function() at 15;
schedule some_function() at 30;
schedule some_function() at 45;
...

Just be aware that the BSL will continue executing without waiting for this "loop" to finish. Also, there is no way to exit the "loop" early, but some_function() could simply refuse to perform its task if a global variable has changed to false, simulating a loop exit.

Multithreading

BSL doesn't have a robust solution, but you can perform a certain amount of multithreading using "fork" or "schedule". Please see BSL:Manual § Concurrency for examples.

Operators

Like some other languages, BSL differentiates between checking for equivalency ("eq") and setting equivalency ("="):

if (counter eq 3) ...

i = 4;

BSL's operators are pretty standard stuff:

+ -
eq ne < > <= >=
and or !

But you'll note that there is no operator for multiplying or dividing! You'll need to create your own loop with addition and subtraction to perform that kind of math.

Data types

You can choose from "void", "bool", "int", "float", and "string" (but "void" is only allowed when defining a function). The "bool" type is buggy in Windows Oni, so use of "int" is recommended instead.

var int a = 1;
# see the "Functions" section below on how to use a data type keyword when defining a function

The "int" type is signed 32-bit. See BSL:Manual § Data types to learn about various limitations on math between different data types in BSL.

Functions

As mentioned above, functions do not need to be declared or defined before they are called. Defining and calling a function looks exactly like C except for the "func" keyword in the definition:

a = get_enemy_count(0);
func int get_enemy_count(int count_dead) { ... return(count); }

Once again, please see BSL:Manual § Functions section to learn about concurrent and recursive calling.

Variables

As usual, variables can be explicitly initialized:

var int y = 9;

or not:

var int y;

In the second case, "y" is automatically initialized to zero.

Variables will have global scope if declared outside of a function.

Built-in functions and variables

Like any game, Oni provides access to a number of hardcoded functions and global variables that can be used to alter the game environment. They are listed on BSL:Functions and BSL:Variables.