Expression 2 (commonly abbreviated as E2) is a scripting language used in the game Garry's Mod, specifically within the Wiremod addon. It allows players to create complex logic and automation systems by writing code that interacts with in-game entities, physics, and other components.
Expression 2 is designed to be efficient and sandboxed, meaning it runs safely within the game environment without affecting the core game engine. It is often used to control contraptions, simulate logic circuits, or automate tasks using Wiremod components like gates, sensors, and actuators.
🧠 Key Features of Expression 2:
- Strongly typed scripting language
- Real-time execution in the game world
- Interacts with Wiremod components
- Can control props, apply forces, read inputs, and more
- Runs on an E2 chip (a special in-game entity)
📘 Basic Syntax Overview:
- Functions:
functionName(arguments)
- Variables:
variable = value
- Control structures:
if, while, for
- Comments:
# This is a comment
- Outputs:
outputName = value
- Inputs:
inputName (defined in the editor)
🧪 Comprehensive Example: Automatic Door Controller
Let’s create an E2 script that opens a door when a player is nearby and closes it when they leave.
🎯 Goal:
- Detect when a player is within 100 units of the door
- Open the door (e.g., move a prop or trigger a constraint)
- Close the door when no players are nearby
🧱 Setup in Garry's Mod:
- Place a prop to act as the door
- Attach a Wiremod E2 chip to the door
- Optionally, use a servo or constraint to simulate opening/closing
🧾 Expression 2 Code:
@name Auto Door Controller
@inputs Door:entity
@persist IsOpen
@trigger
interval(100) # Run every 100ms
# Get all players
players = findPlayers()
# Check if any player is within 100 units of the door
PlayerNearby = 0
foreach (P = players) {
if (P:pos():distance(Door:pos()) < 100) {
PlayerNearby = 1
break
}
}
# Open the door if a player is nearby and it's not already open
if (PlayerNearby & !IsOpen) {
Door:setPos(Door:pos() + vec(0, 0, 50)) # Move door up
IsOpen = 1
}
# Close the door if no players are nearby and it's open
if (!PlayerNearby & IsOpen) {
Door:setPos(Door:pos() - vec(0, 0, 50)) # Move door down
IsOpen = 0
}
🧩 Explanation:
@inputs Door:entity: The door prop is wired to the E2 chip as an input.
@persist IsOpen: Stores the current state of the door (open or closed).
interval(100): Runs the code every 100 milliseconds.
findPlayers(): Gets a list of all players on the server.
distance(): Measures how far each player is from the door.
setPos(): Moves the door prop up or down to simulate opening/closing.
🛠️ Enhancements You Could Add:
- Use a servo or motor to rotate the door instead of moving it
- Add a delay before closing the door
- Play a sound when the door opens or closes
- Add a keypad or button override
📚 Resources:
Let me know if you want a different example (e.g., vehicle controller, turret, elevator, etc.)!