Skip to content

Scripting basics

How to use function parameters in Code: Terraform

Last checked September 19, 2026 · Community-tested on Early Access BuildID 25282646

The short answer

Define and call functions almost as you would in Python. Code: Terraform currently supports positional arguments, keyword arguments, default values, *args, and **kwargs.

Test a small call in the in-game editor first. Keyword-only parameters are unverified, and nonlocal is rejected.

On this page

1. Define a function and pass two values

This complete example uses only the game's scripting language. Paste it into the in-game editor and run it before adapting the names to your machine:

def has_reserve(available, minimum):
    return available >= minimum

print(has_reserve(40, 25))

available receives 40, minimum receives 25, and the function returns True. Once that works, replace the two numbers with values read from your own component.

This is game code, not website code. Run it in a Code: Terraform script or Playground. A printed True is the success check for this small test.

2. Use a default value or a named argument

def has_reserve(available, minimum=25):
    return available >= minimum

print(has_reserve(40))
print(has_reserve(40, minimum=50))

The first call keeps the default minimum of 25. The second call replaces it with 50 by name. Named arguments are useful when a function has several settings and you want the call to explain itself.

Like Python, a default object is created once and shared by later calls. Prefer numbers, strings, or None as defaults; create a new list or dictionary inside the function when each call needs its own container.

3. Accept a variable number of values

SyntaxWhat the function receivesUse it when
*argsExtra positional valuesThe number of readings or item IDs can vary.
**kwargsExtra named valuesOptional settings are clearer by name.
*values in a callA list or tuple unpacked into positionsYou already collected arguments in sequence.
**settings in a callA dictionary unpacked into namesYou already collected settings in a dictionary.
def total(*values):
    result = 0
    for value in values:
        result += value
    return result

readings = [4, 7, 9]
print(total(*readings))

This should print 20. Test the helper by itself before using it to drive a machine.

4. Avoid the parameter traps that differ from Python

  • Keyword-only parameters are not yet verified. Do not build a critical script around def f(*, setting) until current in-game testing confirms it.
  • nonlocal is not supported. A nested function may read an enclosing value, but do not use nonlocal to reassign it.
  • Type annotations do not enforce a type. Current testing found that annotations are treated as inert metadata, so validate important values in the function itself.
  • self already refers to the current machine script. A class method may still use its own self; keep the two scopes clear when moving helpers into classes.
Code: Terraform script editor beside the Supply Dock orders screen
The editor in an official Supply Dock screenshot. Test the small function examples above in your own script before connecting a running machine.Open full image ↗

If the function does not run

  1. Run only the function and one print(...) call so the first error is easy to see.
  2. Count positional values from left to right and check that every required parameter received one.
  3. Check the spelling of named arguments; a keyword must match its parameter name.
  4. Remove nonlocal, keyword-only syntax, or copied library imports that the game's Python-like interpreter may not support.
  5. Read the first console error, fix it, and run again. The game may reveal only the first rejected construct at a time.

How to know your helper is ready

  • The small test prints the expected result with positional values.
  • The default value is used when you omit that argument.
  • A named argument replaces the correct default.
  • Your *args or **kwargs test works before component calls are added.
  • Current autocomplete and console show no syntax or argument error.

Watch scripting in the game world

Watch the game

See the editor alongside working systems

Kerbo’s recorded Early Access first look connects script editing with the live planet. It is a game overview, not a reference for every supported argument form; use the examples and limits above.

By Kerbo

Return to the first function →

Playing connects to YouTube. Open on YouTube ↗

Sources for this guide