Scripting basics
How to use function parameters in Code: Terraform
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.
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
| Syntax | What the function receives | Use it when |
|---|---|---|
*args | Extra positional values | The number of readings or item IDs can vary. |
**kwargs | Extra named values | Optional settings are clearer by name. |
*values in a call | A list or tuple unpacked into positions | You already collected arguments in sequence. |
**settings in a call | A dictionary unpacked into names | You 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. nonlocalis not supported. A nested function may read an enclosing value, but do not usenonlocalto 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.
selfalready refers to the current machine script. A class method may still use its ownself; keep the two scopes clear when moving helpers into classes.

If the function does not run
- Run only the function and one
print(...)call so the first error is easy to see. - Count positional values from left to right and check that every required parameter received one.
- Check the spelling of named arguments; a keyword must match its parameter name.
- Remove
nonlocal, keyword-only syntax, or copied library imports that the game's Python-like interpreter may not support. - 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
*argsor**kwargstest 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.
Return to the first function →Sources for this guide
- Player-tested Early Access language comparison, including default values, keyword arguments,
*args,**kwargs, annotations, and unsupportednonlocal. Tests were recorded on BuildID 25282646 on September 13–14, 2026. - Official Code: Terraform announcements, including the September 15 v0.1.23 notes used for the current version review.
- Official Code: Terraform Steam page for the game's Python-like scripting context.