19,344
edits
m (Iritscen moved page BSL:Syntax to BSL:Introduction without leaving a redirect: Syntax is a specific aspect of BSL; this page is a general introduction.) |
(wording; shortening a sample; improving a sample) |
||
Line 1: | Line 1: | ||
{{TOCfloat}} | {{TOCfloat}} | ||
Like any typical scripting or programming language, BSL scripts consist of [[#Comments|commented]] [[#Files|plain-text files]] which | Like any typical scripting or programming language, BSL scripts consist of [[#Comments|commented]] [[#Files|plain-text files]] which employ [[#Logic|branching logic]] and [[#Operators|various operators]] according to a [[#Syntax|strict syntax]] to process [[#Types|data]] using [[#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 from each section. | ||
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. | ||
Line 16: | Line 16: | ||
==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 (Oni's scripts always place it in a file called *_main.bsl). Only other functions that are called by main() (and the functions it calls) will run on their own, though you can also manually call any of those functions from the [[Developer Mode|developer console]] -- however, you cannot actually define variables or functions through the console. | ||
Note that the optional [[IGMD/global|global]] folder is also loaded for all levels, but this code cannot run on its own unless you edit another function that does run automatically so that it calls a function in a global BSL file. | |||
Note that the optional [[global]] folder is also loaded for all levels, but this code cannot run on its own unless you edit another function that does run automatically so that it calls a function in a global BSL file. | |||
==Logic== | ==Logic== | ||
:''See [[BSL:Statements]] for details.'' | :''See [[BSL:Statements]] for details.'' | ||
In order to react to | In order for game events to react to the player's actions, you need branching logic, which basically boils down to an if statement in this form: | ||
if (counter eq 3) # "if counter's value is equal to three, do this..." | if (counter eq 3) # "if counter's value is equal to three, do this..." | ||
Line 45: | Line 29: | ||
} | } | ||
...where counter is a variable declared beforehand (see "Variables" below). | ...where "counter" is a variable declared beforehand (see "Variables" below). By reading the BSL:Statements page, you can also learn how to test for multiple conditions at once in an "if" statement, use an "else" condition, etc. | ||
==Operators== | ==Operators== | ||
Line 60: | Line 44: | ||
:''See [[BSL:Syntax]] for details.'' | :''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 | BSL allows two kinds of syntax to be used somewhat interchangeably, which can be called "shell-style" and "C-style". This can make it more complicated to talk about the language, and you can also generate errors with mixed syntax. Using the C style requires a bit more typing, but it creates safer and more readable code. | ||
Braces define blocks of code, whether the block is a function or a conditional: | Braces define blocks of code, whether the block is a function or a conditional: | ||
Line 74: | Line 58: | ||
'''}''' | '''}''' | ||
In C-style BSL, semicolons end a statement, and parentheses enclose function parameters: | |||
func void some_function(void) | func void some_function(void) | ||
Line 81: | Line 65: | ||
a = 4; | a = 4; | ||
sleep(60); | sleep(60); | ||
if ( | if (a eq 4) | ||
{ | { | ||
dprint("Hello"); | |||
} | } | ||
} | } | ||
Line 94: | Line 78: | ||
a = 4 | a = 4 | ||
sleep 60 | sleep 60 | ||
if ( | if (a eq 4) | ||
{ | { | ||
# | dprint "Hello" # the quotes could be left out too | ||
} | } | ||
} | } | ||
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) | 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). | ||
==Types== | ==Types== |