Jump to content

BSL:Variables: Difference between revisions

updated general notions
m (added introduction)
(updated general notions)
Line 5: Line 5:
The usual thing to do in this situation is to store the needed data in a '''variable''': an elementary data container.
The usual thing to do in this situation is to store the needed data in a '''variable''': an elementary data container.


==Type and value==
A variable has two important attributes: the ''type'' and the ''value''. The value or the variable is just the contents it currently stores. It can be read and written.


Also, a given variable can store only a specific type of data (floating-point number or integer or text string or boolean), and that is what we call the variable's type.
==General notions==
===Type and value===
A variable (data container) has three important attributes: the ''name'', the ''type'' and the ''value''.


An easy way to get the type and value of an existing BSL variable is to enter its name at the console in [[Developer Mode]]: you typically get console output like "bool: 1" or "int32: 42" or "float: 3.14" or "string: hello"
The ''value'' or the variable is just the contents it currently stores. It can be read and written.
==Declaration==
 
Put usage of '''var''' here. Link to [[OSL:data|OSL data types]]
Also, a given variable can store only a specific ''type'' of data (floating-point number or integer or text string or boolean), and that is what we call the variable's type.
==Assignment==
 
Put usage of '''=''' here.
Finally, the ''name'' of a variable is a unique text string that is used to identify the data container within the script. Like [[BSL:Functions|function]] names, variable names are case-sensitive (NOTE: [[BSL:Preset|preset]] variables and functions are all lower-case). Valid characters are: ''please fill me in''
 
An easy way to get the ''type'' and ''value'' of an existing BSL variable is to enter its ''name'' at the console in [[Developer Mode]]: you typically get console output like "bool: 1" or "int32: 42" or "float: 3.14" or "string: hello"
 
 
----
===Declaration and assignment===
If you want to use a new variable (data container), you must set it up (''declare'' it) first, with the '''var''' keyword. A declaration statement can look a bit like this:
var int my_superduper_integer;
var bool my_totally_awesome_boolean;
var string my_incredible_text_string;
var float my_cool_floating_point_number;
The first word is the keyword "var", next comes the type (keyword), then the name of the variable. The final ";" is required. The name can be any valid name (i.e., not a keyword and not containing invalid characters)
 
A declaration is usually seen at "global scope" (i.e., outside all functions). It is recommended to group declarations at the beginning of a BSL file (for readability), but it's not necessary. It is also not necessary (but strongly recommended) to declare a variable ''before'' the context where it is used for the first time.
 
On some occasions you may want to declare a ''local'' variable (i.e., to embed a declaration statement into a [[BSL:Functions|function]]). This feature will be detailed elsewhere.
 
 
----
===Assignment===
You can assign a new value to an existing variable with the assignment operator "=". The name of the variable you want to modify is on the left-hand-side (LHS) of the "=", and on the right-hand-side (RHS) you can have any valid [[BSL:Expressions|expression]] that evaluates/converts into the variable's type.
 
An assignment can be combined with a declaration (see above). That means the variable will have a definite value right after it is created. If you don't specify/define such a starting value, you should not, generally speaking, assume anything about the value.
 
However, apparently there ''are'' "default" values assigned to every variable even if it hasn't been defined yet. Those values are 0 (i.e., false) for bool, 0 for int and float, and "" (empty string) for string.
 
 
----
===More===
Later.
 
 
----
----
==Preset variables==
==Preset variables==
;All the preset OSL variables are listed here, in alphabetical order.
;All the preset OSL variables are listed here, in alphabetical order.
Line 70: Line 104:
|-
|-
|bool||[[ai2_showactivationpaths]]||0
|bool||[[ai2_showactivationpaths]]||0
|shows the initial pathfinding (upon actiavation) of all AI||OK
|shows the initial pathfinding (upon activation) of all AI||OK
|-
|-
|bool||[[ai2_showastar]]||0
|bool||[[ai2_showastar]]||0