VBScript (XSISDK)

VBScript has been a long-time favourite with XSI users and many of the examples in the Object Model reference have been written with VBScript.

VBScript is installed on every Windows machine and is available on Linux in the context of XSI.

VBScript can be learned easily. It is recommended that all SDK users spend a few hours mastering its syntax, even if it won’t be the primary scripting language that you use. This will make is easier to read and understand any VBScript examples.

One way to learn VBScript is to read the help file Script56.chm that is part of the Script Debugger download http://msdn.Microsoft.com/scripting. Other VBScript information is freely available on-line.

Table of contents

VBScript Advantages

  • Widely used, with lots of example XSI code
  • Stable language and available in all XSI installations.
  • Some people consider this an ideal language for novice programmers. Even users who don’t consider themselves programmers by any stretch of the imagination can cut and paste their way to a simple script.
  • VBScript, Visual Basic, VBA and other variants of the language are widely used as a scripting language on the Windows Platform. Visual Basic is one of the most successful languages of all time (although "professional" developers don't like to admit it). People familiar with scripting on the Microsoft Office platform will be comfortable with VBScript on XSI.

VBScript Disadvantages

  • Some of the syntax is very different from C++ and will probably frustrate experienced programmers.
    • “=” is used for both assignment and comparison
    • the “()” characters are used less often then in C++
    • "NOT" does both boolean and bitwise NOT (so NOT true is false, but NOT -1 is -2!)
  • VBScript is not as widely used in web technology as JScript/Javascript, because it is only supported on Windows. XSI is able to support VBScript on the Linux platform, but few developers from a purely linux/unix background are familiar with VBScript.
  • As a basic (sic) language the error handling is primitive (“on error resume next”, “option explicit”)
  • Without good support for creating objects it is harder to write very large scripts with VBScript than JScript and Python.
  • "The Flangy Guide to Hating VBScript" (http://adamv.com/dev/articles/hatevbs/vbscript) gives a good overview of some of the strange syntax and missing aspects of vbscript.

Tips and “Gotchas”

Dim and Redim

Extremely common confusion for anyone coming from the C, C++ or JScript world is the fact that vbscript deals with array size in terms of the upper bound index. So an array with upper bound 10 has 11 elements - 0,1,2,3,4,5,6,7,8,9 and 10.

redim x( 4 ) 

'Says 4 which means there are five elements in
'the array
logmessage ubound( x, 1 ) 

'Initialize all the values
for i = 0 to ubound( x, 1 )
	x(i) = i
next


Using JScript Math functions

JScript has a math (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsobjmath.asp) object that includes methods like acos that are not available in VBScript.

However, as Bradley Gabe suggested on the XSI mailing list, it is possible to use the JScript math object within VBScript.

Basically, create a self-installed command (set to non-logging) that returns the JScript global Math object.

function GetMathObj_Execute(){ 
       return(Math);
}

Once this command is part of XSI, you can switch back to VBScript and access all of the goodies JScript's Math object has to offer, including the acos method:

set m = GetMathObj
LogMessage(m.acos(0.5))

Because there is a certain cost to calling a custom command, be sure to remember the math object within your VBScript code, rather than calling GetMathObj many times.



This page was last modified 18:40, 30 Aug 2007.
This page has been accessed 24331 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy