--- title: MD variables and expressions summary: Strings, lists, tables, existence checks and explicit variable namespaces. section: scripting kind: guide status: example updated: 2026-10-27 tags: ["X4","modding","md","variables"] sources: ["https://wiki.egosoft.com/X%20Rebirth%20Wiki/Modding%20support/Mission%20Director%20Guide/"] --- ## Values and expressions Many MD attributes contain expressions. XML quotes delimit the attribute, while inner single quotes make an MD string literal. Lists and tables have different access patterns: `$Items.{1}` reads a list position; `$ByID.{$Key}` reads a named table key. ```xml ``` ## Validate the request shape The example checks the incoming UI payload inside its handler. A correct list length does not prove element types. Validate the text and switch value before applying a setting. ```xml ``` ## Existence and scope `not $Records?` checks variable existence. Objects additionally need `.exists`. A cue reference such as `Init.$Records` explicitly selects context. False, an empty list and a missing variable are distinct states; do not substitute a content check for existence. ## Explicitly choose variable ownership Do not assume `$Value` and `this.$Value` always refer to the same storage: `$Value` resolves through the namespace. Choose an appropriate `namespace="this"` for instance-local handler data; store shared state in an explicitly chosen context. Compare two consecutive requests: does the intended field change, and does the first request leak into the second? See [MD libraries](wiki:md-libraries) for a parameterized reusable cue.