Utility Functions

The library defines a number of utility functions and intrinsic class extensions for both internal and game author use. Some of these will be of more use to game authors than others, since a number of them are primarily designed to carry out specialized functions in the library. They are nevertheless all available for game authors to use, and are described below. Note that most of them were simply taken over from the Mercury library, but a few have been added in adv3Lite.

Functions

The following functions (some quite general, some special-purpose) are defined in misc.t or english.t:

The tryInt(val) and tryNumber(val) functions can be useful when you want to parse user input to see whether something is a valid number and, if so, do something with the numerical value it returns. The following table further illustrate how these two functions work (with the output from the intrinsic functions toInteger() and toNumber() also supplied for comparison):

valtryInt(val)tryNum(val)toInteger()toNumber()
11111
'1'1111
1.111.111.1
'1.1'nil1.111.1
00000
'foobar'nilnil00
truenilnil11
'5a5'nilnil55
'15b'nilnil1515
'b15'nilnil00
'3e'nil333
'3e3'nil300033000
'1.2E2'nil1201120
'+4'4444
'-3'-3-3-3-3
'-3.2e4'nil-32000-3-32000

You can see from this that tryInt(val) and tryNumber(val) are both stricter than toInteger(val) and toNumber(val) (which, for example, would have returned numeric values when val was true, '15b' or '5a5'), and that tryInt(val) is 'stricter' than tryNum(val); tryInt(val) only returns a non-nil value either if it's passed a number or if it's passed a string containing only digits optionally preceded by + or -, whereas tryNum(val) will also accept strings with decimal point and exponent notation (such as '1.2E2').


There are also a number of list-related utility functions which are described in the chapter on Lists and Listers.

Intrinsic Class Extensions

In addition, the misc.t module (which must be included in every adv3Lite game) defines a number of additional methods on the intrinsic classes String, Vector, List and Object:

String

The String class gains the following additional methods, each of which returns a new string:


Vector

The Vector class gains the following additional methods:


List

The List class gains the following additional methods:


Object

Some methods have been added to the base Object class to make it somewhat interchangeable with lists and vectors. Certain operations that are normally specific to the collection types have obvious degenerations for the singleton case. In particular, a singleton can be thought of as a collection consisting of one value, so operations that iterate over a collection degenerate to one iteration on a singleton.

The createLiveIterator() method thus allows us to write code like this:

  local a = 'Hello World! ';
  foreach(local cur in a)
     "<<cur>>\n"; 
 

Executing this will then indeed result in a display of the string 'Hello World!'. This will principally be useful when we want to iterate over some variable (or property) that may contain either a collection (List or Vector) or a singleton value.