Expression 2 (E2) Programming Language: A Comprehensive Guide
Expression 2 (E2) is a powerful and versatile scripting language embedded within the sandbox game Garry's Mod (GMod). It's designed specifically for creating dynamic and interactive entities, contraptions, and systems within the GMod environment. Think of it as the "brains" behind complex creations in GMod, allowing you to go far beyond simple physics interactions.
Key Characteristics of Expression 2:
- Event-Driven: E2 code primarily reacts to events happening in the game world. These events can be anything from a player pressing a button, an entity colliding with something, a timer ticking, or data being received over the network.
- Entity-Centric: E2 is deeply integrated with GMod's entity system. You can manipulate entities, access their properties (position, velocity, color, models, etc.), create new entities, and control their behavior.
- Data-Driven: E2 excels at processing and manipulating data. It supports various data types like numbers, strings, vectors, angles, entities, tables (arrays/dictionaries), and booleans.
- C-like Syntax: E2's syntax is inspired by C and other procedural languages, making it relatively accessible to programmers familiar with these paradigms.
- Chip-Based: E2 code is typically written and executed within "Expression 2 Chips" (E2 Chips). These chips are in-game entities that act as containers for your scripts.
- Networking Capabilities: E2 allows for networking, enabling you to create multiplayer interactions, synchronize data between clients, and build networked systems.
- Extensive Function Library: E2 boasts a rich library of built-in functions for manipulating entities, math, strings, networking, file I/O, and more.
- Performance-Oriented: While scripting languages are generally slower than compiled languages, E2 is designed to be relatively performant within the GMod environment, allowing for complex systems without excessive lag (when used responsibly).
What can you do with Expression 2?
The possibilities with E2 are vast, limited primarily by your creativity and understanding of the language. Here are some common use cases:
- Interactive Contraptions: Create complex vehicles, robots, machines, and devices with custom controls, behaviors, and interactions.
- Automated Systems: Build automated doors, lights, sensors, security systems, factories, and more.
- Custom Game Mechanics: Implement unique game rules, scoring systems, timers, and gameplay elements.
- Visual Effects and Displays: Generate dynamic visual effects, animations, text displays, and HUD elements.
- Data Processing and Logic: Create systems that process data from sensors, user input, or external sources and react accordingly.
- Networking and Multiplayer Interactions: Build networked games, chat systems, synchronized contraptions, and collaborative tools.
- Tools and Utilities: Develop in-game tools for building, debugging, and managing your creations.
Comprehensive Example: A Smart Light with Proximity Sensor and Toggle Switch
Let's create a simple yet illustrative example: a "smart light" that:
- Turns on automatically when a player gets close.
- Can be toggled on/off manually using a button.
- Changes color based on its state (on/off).
E2 Code (inside an E2 Chip):
@persist Light:entity Button:entity ProximityRange:number = 100 LightOn:number = 0
@inputs Toggle:number
@outputs LightColor:color LightBrightness:number
if (!isentity(Light)) {
Light = entity():createProp("models/props_lab/light_tube_short.mdl") // Create a light prop
Light:setPos(entity():getPos() + vec(0, 0, 50)) // Position it slightly above the chip
Light:setParent(entity()) // Parent it to the chip for easy movement
Light:setVar("E2Light", 1) // Mark it as an E2 controlled light (optional, for identification)
}
if (!isentity(Button)) {
Button = entity():createProp("models/props_c17/button001a.mdl") // Create a button prop
Button:setPos(entity():getPos() + vec(0, 20, 0)) // Position it slightly to the side
Button:setParent(entity())
Button:setVar("E2Button", 1)
}
ProximityRange = 150 // Adjust proximity range as needed
if (Toggle == 1) { // Button input received
LightOn = !LightOn // Toggle light state (0 becomes 1, 1 becomes 0)
Toggle = 0 // Reset toggle input
}
local closestPlayer = null
local closestDistance = ProximityRange
foreach(Player, player():getAll()) { // Iterate through all players
if (player():isPlayer(Player)) { // Ensure it's a player entity
local dist = distance(entity():getPos(), Player:getPos())
if (dist < closestDistance) {
closestDistance = dist
closestPlayer = Player
}
}
}
if (closestPlayer != null) { // Player within proximity
LightOn = 1 // Turn light on
} else if (LightOn == 0 && Toggle == 0) { // No player nearby and not manually toggled on
LightOn = 0 // Keep light off (or turn off if it was on due to proximity earlier)
}
if (LightOn == 1) {
LightColor = color(255, 255, 200) // Warm white color when on
LightBrightness = 1 // Full brightness
} else {
LightColor = color(50, 50, 50) // Dim grey color when off
LightBrightness = 0 // No brightness (effectively off)
}
Light:setLightColor(LightColor)
Light:setLightBrightness(LightBrightness)
Explanation of the Code:
-
@persist Light:entity Button:entity ProximityRange:number = 100 LightOn:number = 0:
@persist: Declares variables that retain their values between E2 chip executions (ticks).
Light:entity, Button:entity: Declares Light and Button as entity variables (to store entity references).
ProximityRange:number = 100: Declares ProximityRange as a number, initialized to 100 (units in GMod).
LightOn:number = 0: Declares LightOn as a number, initialized to 0 (representing "off").
-
@inputs Toggle:number:
@inputs: Declares input variables that can be controlled from outside the E2 chip (e.g., from a button or wire).
Toggle:number: Declares Toggle as a number input. We'll use this to receive a signal from a button press.
-
@outputs LightColor:color LightBrightness:number:
@outputs: Declares output variables that can be sent to other entities or wires.
LightColor:color: Declares LightColor as a color output, which we'll use to control the light's color.
LightBrightness:number: Declares LightBrightness as a number output, controlling the light's brightness.
-
Entity Creation (Initialization):
if (!isentity(Light)) { ... }: Checks if the Light entity variable is currently valid (i.e., if a light entity is already assigned). If not (first run or light was removed):
Light = entity():createProp("models/props_lab/light_tube_short.mdl"): Creates a new prop entity using the specified model path and assigns it to the Light variable.
Light:setPos(entity():getPos() + vec(0, 0, 50)): Positions the light slightly above the E2 chip. entity() refers to the E2 chip itself. vec(0, 0, 50) is a vector representing an offset in the Z (upward) direction.
Light:setParent(entity()): Makes the E2 chip the parent of the light. This means if you move the E2 chip, the light will move with it.
Light:setVar("E2Light", 1): Sets a custom variable "E2Light" on the light entity. This is optional but can be useful for identifying entities controlled by E2.
How to Use this Example in GMod:
- Spawn an E2 Chip: In GMod, open the spawn menu (usually Q), go to the "Wiremod" category, and find "Expression 2 Chip." Spawn an E2 chip.
- Open the E2 Editor: Right-click on the E2 chip and select "Open Editor."
- Paste the Code: Copy the E2 code provided above and paste it into the E2 editor window.
- Compile and Save: Click the "Compile" button (usually a green play button) to check for errors. If there are no errors, click "Save."
- Spawn Props: Spawn a button prop (e.g., "Button" from the "Props" category) and a light prop (e.g., "Light Tube Short" from "Props -> Lab").
- Wire the Button: Use the Wire Tool (from the Wiremod category) to wire the button's "Pressed" output to the E2 chip's "Toggle" input.
- Wire the Light: Wire the E2 chip's "LightColor" output to the light prop's "Color" input, and the E2 chip's "LightBrightness" output to the light prop's "Brightness" input.
Now, test it out!
- Walk near the light, and it should turn on automatically.
- Press the button, and the light should toggle on/off manually, overriding the proximity sensor.
- Observe the light changing color based on its state.
Conclusion:
This example demonstrates several fundamental E2 concepts:
- Entity manipulation: Creating and controlling entities (light and button).
- Event-driven programming: Reacting to player proximity and button presses.
- Data manipulation: Using variables, numbers, booleans, and colors.
- Control flow: Using
if, else if, foreach statements.
- Input and Output: Using
@inputs and @outputs to interact with the game world.
- Functions: Using built-in functions like
entity(), createProp(), setPos(), setParent(), distance(), color(), setLightColor(), setLightBrightness(), player():getAll(), player():isPlayer(), isentity().
Expression 2 is a powerful tool for creating complex and interactive experiences in Garry's Mod. This example is just a starting point. As you learn more about E2's features and functions, you can build increasingly sophisticated and impressive creations. Remember to explore the E2 documentation and community resources to further your understanding and unlock the full potential of this scripting language.