Lua Language Basics
This page introduces the basic syntax and concepts of the Lua programming language. If you are already familiar with Lua, you can skip this page and continue with the Globals.
Lua is a lightweight and easy-to-learn scripting language. It is designed to be simple, flexible, and embeddable. Traintastic uses Lua to allow you to write scripts that interact with your model railway world.
Variables
Variables are used to store values.
- Strings are written in quotes (
"text"). - Numbers can be integers or decimals.
- Booleans are
trueorfalse.
Comments
Comments are ignored by Lua but help you explain your code.
Control structures
Lua uses familiar programming constructs.
If / else
Loops
Functions
Functions group code into reusable blocks.
Tables
Tables are Lua’s only data structure. They work as lists, dictionaries, or objects.
car = { type = "freight", cargo = "coal" }
log.debug(car.type) -- "freight"
numbers = { 10, 20, 30 }
log.debug(numbers[1]) -- 10
Putting it together
With just these basics—variables, control flow, functions, and tables—you can already create useful scripts in Traintastic.
Tip
Don’t worry about learning everything at once. Start simple and build up as you go.
Next steps
- See what Traintastic adds on top of Lua in Globals.
- Explore Persistent variables to keep data between sessions.
- Browse the Examples to learn by doing.