NML:Elementary values: Difference between revisions

From GRFSpecs
Jump to navigation Jump to search
Content deleted Content added
note about ternary using information from JGR and glx
mNo edit summary
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
Elementary values in expressions are
Elementary values in expressions are


{| class="t"
{| class="wikitable sortable"
! Element
! Element
! Syntax (Python RE)
! Syntax (Python RE)
Line 42: Line 42:
|-
|-
| Unit
| Unit
| nfo|mph|km/h|m/s|hp|kW|hpI|hpM|tons|ton|kg
| nfo|mph|km/h|m/s|hp|kW|hpI|hpM|tons|ton|kg|snow%
|
|
See the section on [[NML:Units|units]].
See the section on [[NML:Units|units]].
Line 51: Line 51:
* Bitwise/logic operators: &, |, ^, &&, ||, <<, >>
* Bitwise/logic operators: &, |, ^, &&, ||, <<, >>
* Comparison operators: ==, !=, <=, >=, <, >
* Comparison operators: ==, !=, <=, >=, <, >
* Ternary operator: ? : (only for integers)

'''Ternary operator'''

A ternary operator is provided (for integers only) in the form:
condition ? branch if true : branch if false

The ternary operator should be used with caution:
* it can be inefficient in certain cases as it executes both branches
* it is only safe for readonly operations (i.e. cannot be used with expressions like STORE_TEMP or STORE_PERM)

Latest revision as of 22:16, 26 January 2024

Expressions

Elementary values in expressions are

Element Syntax (Python RE) Description
Decimal number [0-9]+ one or more digits
Hexadecimal number 0x[0-9A-Fa-f]+ "0x" followed by one or more hexadecimal digits
Floating point number [0-9]+\.[0-9]+ a decimal number, a dot, and another decimal number. Only supported for some properties that expect a floating point number.
Identifier [a-zA-Z_][a-zA-Z0-9_]* a letter or underscore, optionally followed by more letters, digits, or underscore characters
String "([^"\\]|\\.)*" A double quote character, followed by zero or more characters, ending with another double quote character. A character in-between is any single character except a double quote or a back-slash ("\"). It can also be "\\", "\n", "\t", or "\[0-9A-Fa-f][0-9A-Fa-f]" (a back-slash followed by exactly two hexadecimal digits).
Setting an Identifier

The name of a GRF setting

Parameter param[<num>] Read/write the parameter with the given number.
Parameter from another GRF param[<grfid>, <num>] Read the value of a parameter of another grf
Unit mph|km/h|m/s|hp|kW|hpI|hpM|tons|ton|kg|snow%

See the section on units.

You can combine those elementary values using operators like you're used to from other languages. The following operators are supported:

  • Arithmetic operators: +, -, *, /, %
  • Bitwise/logic operators: &, |, ^, &&, ||, <<, >>
  • Comparison operators: ==, !=, <=, >=, <, >
  • Ternary operator: ? : (only for integers)