Overview
Expression 2 (often abbreviated as E2) is a scripting language created for the Wiremod addon in Garry's Mod. It enables users to programmatically control in-game objects (e.g., motors, lights, displays) while remaining sandboxed and safe. E2 is designed to be relatively easy to use yet powerful enough to build complex contraptions. Its syntax is similar to many C-like languages and supports variables, conditionals, loops, vector arithmetic, and more.
Below is a comprehensive example of an E2 chip that demonstrates many of these features. In this example, the chip acts as a Rotating Platform Controller. It accepts a rotation speed input and an optional reset signal, and it outputs the current angle of the platform. The chip continuously increments an internal angle value, normalizing it between 0 and 360 degrees. This example shows how to use persistent variables, conditionals, a loop to “wrap” the angle within the desired bounds, and how to structure an E2 chip with its directives.
Comprehensive Example: Rotating Platform Controller
@name Rotating Platform Controller
@description This chip controls a rotating platform. It increments an internal angle based on a rotation speed input and resets the angle when a reset signal is active.
@inputs RotSpeed:number Reset:wirelink
@outputs PlatformAngle:number
@persist Angle:number
@trigger all
# Initialization: Check if the chip is running for the first time.
if (first()) {
Angle = 0 # Start with an angle of 0 degrees.
}
# If the reset input is activated, reset the angle to 0.
if (Reset & 1) {
# The Reset input is treated as a truthy number; using bitwise AND with 1 ensures it’s seen as non-zero.
Angle = 0
} else {
# Increment the angle by the given rotation speed.
# (Note: In a more refined chip you might multiply RotSpeed by a time delta (dt) for framerate independence.)
Angle = Angle + RotSpeed
# Normalize the angle so that it remains within the 0–360 degree range.
while (Angle >= 360) {
Angle = Angle - 360
}
while (Angle < 0) {
Angle = Angle + 360
}
}
# Output the current platform angle.
PlatformAngle = Angle
Explanation of Key Elements
-
Header Directives:
@name: Names the chip.
@description: Provides a brief description of what the chip does.
@inputs: Declares the incoming signals. In this case:
RotSpeed is a numeric input to control how fast the angle increments.
Reset is a wirelink (could be a button or other digital signal) used to reset the angle.
@outputs: Declares the data the chip outputs; here, the computed PlatformAngle.
@persist: Declares persistent variables that keep their value between executions. We use Angle to store the current angle.
@trigger all: Ensures the chip executes every time any input changes.
-
Initialization:
- The
if (first()) { ... } block guarantees that the Angle variable is initialized only once when the chip first runs.
-
Input Handling and Arithmetic:
- When a reset signal is received (i.e.,
Reset is active), the chip resets the angle to 0.
- Otherwise, it adds the rotation speed (
RotSpeed) to the current Angle every execution tick.
-
Normalization Loop:
- Two
while loops ensure that the angle value stays between 0 and 360 degrees. This “wrap-around” technique is typical in rotational control.
-
Output:
- The final computed
Angle is sent out as PlatformAngle.
Usage in Garry's Mod
To use this chip:
- Place a Wire Expression 2 chip in your Garry's Mod setup.
- Copy and paste the code into the E2 chip’s editor.
- Connect a numeric source (like a dial or slider) to the
RotSpeed input and a digital signal (like a button) to the Reset input.
- The chip will output the current angle, which you can then wire to other devices (e.g., a motor controller) to visually or mechanically represent the rotation.
This example should give you a thorough insight into the basics of Expression 2 programming, along with practical usage of variables, loops, and conditional logic. Enjoy experimenting with your own E2 scripts in Garry's Mod!