Player APIs (BrightScript & Ja...
...
BrightScript
Language Reference

Built-In Functions

11min
brightscript features a set of built in, module scope, intrinsic functions a number of file i/o, string, mathematics, and system functions are also available via the roglobal object type() type(a as variable) as string this function returns the type of the passed variable and/or object getglobalaa() getglobalaa() as object this function fetches the global associative array for the current script rnd() rnd(range as integer) as integer rnd(0) as float if passed a positive, non zero integer, this function returns a pseudo random integer between 1 and the argument value the range includes the argument value for example, calling rnd(55) will return a pseudo random integer greater than 0 and less than 56 if the argument is 0, this function returns a pseudo random float value between 0 and 1 note the rnd() functions utilize a pseudo random seed number that is generated internally and not accessible to the user box() box(type as dynamic) as object this function returns an object version of the specified intrinsic type objects will be passed through example b = box("string") b = box(b) ' b does not change run() run(file name as string, \[optional arg as dynamic, …]) as dynamic run(file names as roarray, \[optional arg as dynamic, …]) as dynamic this function runs one or more scripts from the current script you may append optional arguments, which will be passed to the main() function of the script(s) the called script may also return arguments to the caller script if a string file name is passed, the function will compile and run the corresponding file if an array of files is passed, the function will compile each file, link them together, and run them example sub main() run("test brs") breakifrunerror(line num) print run("test2 brs", "arg 1", "arg 2") if run(\["file1 brs","file2 brs"])<>4 then stop breakifrunerror(line num) stop end sub sub breakifrunerror(ln) el=getlastruncompileerror() if el=invalid then el=getlastrunruntimeerror() if el=\&hfc or el=\&he2 then return 'fc==err normal end, e2=err value return print "runtime error (line ";ln;") ";el stop else print "compile error (line ";ln;")" for each e in el for each i in e print i;" ";e\[i] end for end for stop end if end sub eval() eval(code snippet as string) as dynamic this function runs the passed code snippet in the context of the current function the function compiles the snippet, then executes the byte code if the code compiles and runs successfully, it will return zero if the code compiles successfully, but encounters a runtime error, it will return an integer indicating the error code (using the same codes as the getlastrunruntimeerror() function) if compilation fails, it will return an rolist object; the rolist structure is identical to that of the getlastruncompileerror() function the eval() function can be useful in two cases when you need to dynamically generate code at runtime when you need to execute a statement that could result in a runtime error, but you don’t want code execution to stop example print eval("1/0") 'returns a divide by zero error getlastruncompileerror() getlastruncompileerror() as rolist this function returns an rolist object containing compile errors (or invalid if no errors occurred) each rolist entry is an roassociativearray object containing the following keys errstr the compile error type (as string) filespec the file uri of the script containing the error (as string) errno the error number (as integer) lineno the line number where the error occurs (as integer) the following are possible errno values error code description expanded description \&hbf 191 err nw endwhile statement occurs without statement \&hbe 190 err missing endwhile while statement occurs without endwhile statement \&hbc 188 err missing endif end of script reached without finding an endif statement \&hbb 187 err noln no line number found \&hba 186 err lnseq line number sequence error \&hb9 185 err loadfile error loading file \&hb8 184 err nomatch match statement does not match \&hb7 183 err unexpected eof unexpected end of string encountered during string compilation \&hb6 182 err for next mismatch variable on next does not match for \&hb5 181 err no block end \&hb4 180 err labeltwice label defined more than once \&hb3 179 err untermed string literal string does not have end quote \&hb2 178 err fun not expected \&hb1 177 err too many const \&hb0 176 err too many var \&haf 175 err exit while not in while \&hae 174 err internal limit exceded \&had 173 err sub defined twice \&hac 172 err nomain \&hab 171 err foreach index tm \&haa 170 err ret cannot have value \&ha9 169 err ret must have value \&ha8 168 err fun must have ret type \&ha7 167 err invalid type \&ha6 166 err nolonger feature no longer supported \&ha5 165 err exit for not in for \&ha4 164 err missing initilizer \&ha3 163 err if too large \&ha2 162 err ro not found \&ha1 161 err too many labels \&ha0 160 err var cannot be subname \&h9f 159 err invalid const name \&h9e 158 err const folding getlastrunruntimeerror() getlastrunruntimeerror() as integer this function returns the error code that resulted from the last run() function these codes indicate a normal result error code description expanded description \&hff 255 err okay \&hfc 252 err normal end execution ended normally, but with termination (e g end, shell "exit", window closed) \&he2 226 err value return return executed with value returned on the stack \&he0 224 err no value return return executed without value returned on the stack the following codes indicate runtime errors error code description expanded description \&hfe 254 err internal unexpected condition occurred \&hfd 253 err undefined opcd opcode could not be handled \&hfb 251 err undefined op expression operator could not be handled \&hfa 250 err missing parn \&hf9 249 err stack under no value to pop off the stack \&hf8 248 err break scriptbreak() function called \&hf7 247 err stop stop statement executed \&hf6 246 err ro0 bscnewcomponent failed because object class not found \&hf5 245 err r01 brightscript member function call does not have right number of parameters \&hf4 244 err ro2 brightscript member function not found in object or interface \&hf3 243 err ro3 brightscript interface not a member of the object \&hf2 242 err too many param too many function parameters to handle \&hf1 241 err wrong num param number of function parameters incorrect \&hf0 240 err rvig function returns a value, but is ignored \&hef 239 err notprintable value not printable \&hee 238 err notwaitable wait statement cannot be applied to object because object does not have an romessageport interface \&hed 237 err must be static interface calls from rotinterface type must be static \&hec 236 err ro4 " " dot operator used on object that does not contain legal object or interface reference \&heb 235 err notypeop operation attempted on two type less operands \&he9 233 err use of uninit var uninitialized variable used illegally \&he8 232 err tm2 non numeric index applied to array \&he7 231 err arraynotdimmed \&he6 230 err use of uninit brsubref reference to uninitialized sub \&he5 229 err must have return \&he4 228 err invalid lvalue left side of the expression is invalid \&he3 227 err invalid num array idx number of array indexes is invalid \&he1 225 err unicode not supported \&he0 224 err notfunopable \&hdf 223 err stack overflow \&h20 32 err cn continue ( cont or c ) not allowed \&h1c 28 err stringtolong \&h1a 26 err os string space has run out \&h18 24 err tm a type mismatch (string /number operation mismatch) has occurred \&h14 20 err div zero \&h12 18 err dd attempted to re dimension array \&h10 16 err bs array subscript out of bounds \&h0e 14 err missing ln \&h0c 12 err outofmem \&h08 8 err fc invalid parameter passed to function/array (e g a negative matrix dim or square root) \&h06 6 err od out of data (read) \&h04 4 err rg return without gosub \&h02 2 err syntax \&h00 0 err nf next without for