Expressions

From ADRIFT 5 Manual Wiki
Revision as of 03:20, 7 December 2011 by Saabie (Talk | contribs) (Added workaround for mixing different classes of functions)

Jump to: navigation, search

Expressions are used to calculate integer values or produce strings of text that can then be stored in Variables or Properties using Actions, or used in comparison operations in Restrictions.

IconWarning.pngIn the current version of Adrift (5.50.21) it is usually not possible to mix the % Functions with math/string functions in the same expression. If you do need a mixed expression then create extra Variables to hold temporary values and separate the different function types into separate actions. The final action then uses these temporary values to calculate the result. The same temporary variables can then be reused in the next equation.

Integer Expressions

In an action a value can be calculated by an integer expression and the result stored in a 'number' variable, an element of an array variable, or an integer property of a character, object or location. The basic mathematical functions +,-,*,/ can be used to calculate a value, using integer constants, properties, variables, and the reference %number%. An integer expression can also be compared with an integer variable or property in a restriction, to determine if they are equal or one is greater than or less than the other value. x mod y Returns the modulus of x and y, i.e. the remainder when x is divided by y. i.e. 11 mod 4 = 3

  • NOTE: The divide operator '/' performs a round-to-nearest-integer integer divide. If you need the truncated integer part of the division then first subtract 0.5 times the number being divided by, eg. (%IntVar%-50)/100
  • Parentheses can be used to control the order of evaluation, eg. (17+4)*(9-6)
  • Variable names must be delimited with '%' symbols, eg. %Score%
  • Array variables use square brackets to enclose the index value, eg. %ArrayVar1[3]%
  • Integer properties are accessed using the PropertyValue function. This function is delimited with '%'s and currently requires the key of the character/object/location as its first parameter and the key of the property as its second. eg. %PropertyValue[Object16,Property2]%

A number of mathematical functions can also be used, including:

  • min(x,y) Returns the minimum of value x and y
  • max(x,y) Returns the maximum of value x and y
  • either(x,y) Randomly returns either x or y
  • rand(x,y) Selects a random value between x and y
  • abs(x) Returns the absolute value of x( the magnitude regardless of sign), i.e. abs(-4) = 4
  • If(test,x,y) If "test" evaluates true, returns x, else returns y Where "test" is a=b, a==b, a<b, a<=b, a>b, a>=b, a<>b, a!=b. Conditions can be ANDed using "and", "&" or "&&" or ORed using "or", "|", "||", eg. IF(%variable1%=1,%variable2%+1,RAND(5,7))

String Expressions

In an action a line of text can be calculated using a string expression and the result stored in a 'text' variable, an element of an array variable, or a text property of a character, object or location. A string expression can also be compared with a text variable or property in a restriction, to determine if they contain exactly thesame text or not.

String constants are delimited with the single quote character, eg. 'Some text'. The '&' operator concatenates two strings together, eg. 'Some text'&'ures' becomes 'Some textures'. Any text based property, variable, element of array variable, or the reference %text% can be used in a string expression.

The following string functions are available:

  • UCase(text) Converts <text> to all upper case
  • LCase(text) Converts <text> to all lower case
  • PCase(text) Converts <text> to proper case, i.e. a capitalises the first letter of each word, with the rest in lower case
  • Left(text, length) Returns the <length> leftmost characters of <text>
  • Right(text, length) Returns the <length> rightmost characters of <text>
  • Mid(text, start, length) returns <length> characters of <text>, starting at <start>

Complex Expressions

An expression can be partly an integer expression and partly a string expression. For example an integer expression can be used to calculate the index of a text variable, and the result of that used in a string expression.

The following functions can be used in mixed expressions:

  • Len(text) Returns the length of <text>
  • Val(text) Converts <text> to a number (or zero if it can't match)
  • Str(x) Converts an integer value x to text form
  • instr(text, search) Returns the position of <search> within <text> eg. instr(“hello”,”e”) = 2