8,315
edits
Paradox-01 (talk | contribs) (global vars) |
Paradox-01 (talk | contribs) (variables) |
||
Line 11: | Line 11: | ||
==== | ====Variables==== | ||
=====Declaration===== | |||
In VBS, any new variable is of type "variant". | |||
dim MyVar | |||
A variant's subtype (boolean, string, integer, ...) gets declared automatically when the variable is used the first time. | |||
MyVar = 100 | |||
Variants can be declared in bulk, separated by comma. | |||
dim MyVar, MyVar2, MyVar3 | |||
Variables can be used without declaring them. That's convenient for short scripts. | |||
MyVar4 = 100 | |||
MyVar5 = "text" | |||
As longer a script becomes as more likely typos can appear including in variables. | |||
'''''option explicit''''' will force you to declare every variable but that will also make sure no misspelled variable can appear. You would see the error immediately. | |||
option explicit | |||
dim MyVar6 | |||
MyVar6 = true | |||
=====Global variables===== | |||
Any variable that gets defined outside a function or subroutine is a global variable. | |||
Variables inside a function or sub are local. | |||
A local variable can only be used of the sub or function where it was declared. | |||
Global variables can be used by any sub or function in the file. | |||
' global var | |||
MyVar7 = 3 + 0.3 | |||
sub test | |||
' local var | |||
MyVar8 = 3.3 | |||
end sub | |||
function test2 | |||
' local var | |||
MyVar9 = 3 | |||
MyVar10 = 4 + MyVar9 | |||
end function | |||
In Softimage '''''Public''''' variable declaration doesn't work. It means variables can be global inside a script but not between multiple scripts. | |||
At same time Softimage's substitute for these missing "public" variables are two commands: SetGlobal and GetGlobal. | |||
SetGlobal "MyGloVar", "variable_value" | |||
logmessage GetGlobal ("MyGloVar") | |||
Further information are found over [http://download.autodesk.com/global/docs/softimage2014/en_us/sdkguide/si_cmds/SetGlobal.html HERE.] | |||
Theoretically it should be possible to place all code on one script file but that screws the overview. | Theoretically it should be possible to place all code on one script file but that screws the overview. | ||
Other possibilities to pass values from one script to another: | |||
: a) a command (containing a function with at least one argument), needs many lines just for the setup | |||
: b) user data blob, preferably attached to the scene root, needs annoying checks to see if they already exist, however an advantage is that UDB can be saved inside *.xsi files | |||
: c) a Softimage environment item, the information can only be stored as a string | |||
' set value | |||
XSIUtils.Environment.Setitem "MyVar", "true" | |||
' caution: it can't contain boolean values no matter if true and false were set in quotes or not | |||
' get value | |||
logmessage XSIUtils.Environment("MyVar") | |||
As the information is a string you need to convert it back to what it was meant originally e.g. with cBool and cInt. For more conversion see [http://www.w3schools.com/vbscript/vbscript_ref_functions.asp HERE] | |||
=====Arrays===== | |||
An array is a variable that can contain multiple values, also named elements. VBS arrays are 0-based. For example MyArr(1) has 2 elements (one at index 0 and one at index 1). | |||
' static array | |||
Dim MyArr(1) | |||
MyArr (0) = true | |||
MyArr (1) = false | |||
To create dynamic arrays (where the amount of elements can by changed) use '''''redim''''' at all times or '''''Array''''' declaration at the beginning. ReDim clears an array. Use '''''preserve''''' to keep the old values. | |||
MyArr = Array(true,false) | |||
MyArr2 = Array("A","B") | |||
ReDim Preserve MyArr (2) | |||
ReDim Preserve MyArr2(2) | |||
MyArr(2) = true | |||
MyArr(2) = "C" | |||
[...] | |||
====Directories==== | ====Directories==== |
edits