It seems like you might be referring to "Expression 2" (E2), a scripting language used within the game Garry's Mod (GMod). Expression 2 is not a general-purpose programming language but rather a domain-specific language designed for use with the Wiremod addon in GMod. Wiremod is a popular modification that allows players to create complex contraptions, machines, and logic systems within the game, and E2 is one of its most powerful tools for scripting logic.
If this is not what you meant, please clarify, and I'll provide information on the correct topic. Below, I'll explain what Expression 2 is, its purpose, and provide a comprehensive example of how it can be used in GMod.
What is Expression 2 (E2)?
Expression 2 (E2) is a scripting language integrated into the Wiremod addon for Garry's Mod. It allows players to write scripts to control in-game entities, create logic systems, and automate tasks. E2 is particularly useful for creating complex contraptions, such as automated turrets, vehicles, calculators, or even mini-games within GMod.
Key Features of E2:
- Event-Driven: E2 scripts are event-driven, meaning they respond to specific triggers or conditions (e.g., timers, player input, or sensor data).
- Variables and Data Types: E2 supports various data types, including numbers, strings, vectors, angles, and arrays.
- Functions: E2 includes built-in functions for mathematical operations, entity manipulation, input/output, and more.
- Integration with Wiremod: E2 can interface with Wiremod components, such as sensors, gates, and thrusters, to create interactive systems.
- In-Game Execution: E2 scripts are executed in real-time within the game, making it a powerful tool for dynamic contraption behavior.
Syntax:
E2 syntax is similar to other programming languages, with elements resembling Lua, C, or Python. It uses variables, conditionals (if statements), loops, and functions to define behavior.
Comprehensive Example of an E2 Script
To illustrate how E2 works, let's create a practical example: an automated turret that detects nearby players, aims at them, and fires a weapon. This example will demonstrate key E2 concepts, such as variables, conditionals, loops, and interaction with in-game entities.
Scenario:
- We want to create a turret that:
- Uses a Wiremod ranger to detect nearby players.
- Aims a turret (a Wiremod turret or a prop with thrusters) at the nearest player.
- Fires a weapon when a target is in range.
- Displays the target's name on a Wiremod screen.
Prerequisites:
- You need Garry's Mod installed with the Wiremod addon.
- You need to spawn the necessary Wiremod components (e.g., ranger, turret, screen) and link them to an E2 chip.
E2 Script Example:
@name Automated Turret
@inputs Ranger:wirelink Turret:entity Screen:wirelink
@outputs Fire
@persist Target:entity TargetPos:vector
# Run the script every tick (continuously)
interval(100)
# Use the ranger to find the nearest player
if (first() | duped()) {
# Initialize variables when the script starts or is duplicated
Target = noentity()
TargetPos = vec(0,0,0)
Fire = 0
}
# Check if the ranger is linked and working
if (Ranger["Active", number]) {
# Get the position of the nearest entity detected by the ranger
local RangerData = Ranger["HitPos", vector]
local RangerEntity = Ranger["Entity", entity]
# Check if the detected entity is a valid player
if (RangerEntity:isValid() & RangerEntity:isPlayer()) {
Target = RangerEntity
TargetPos = RangerData
} else {
Target = noentity()
TargetPos = vec(0,0,0)
}
}
# If we have a valid target, aim the turret and fire
if (Target:isValid()) {
# Calculate the direction to the target
local TurretPos = Turret:pos()
local Direction = (TargetPos - TurretPos):normalized()
# Convert the direction to angles for aiming
local AimAngle = Direction:toAngle()
# Apply the aiming angles to the turret
Turret:setAng(AimAngle)
# Fire the turret
Fire = 1
# Display the target's name on the screen
if (Screen:isValid()) {
Screen:writeString("Target: " + Target:name(), 0, 0)
}
} else {
# No target, stop firing
Fire = 0
# Clear the screen
if (Screen:isValid()) {
Screen:writeString("No Target", 0, 0)
}
}
# Debugging output to the console (optional)
print("Target: " + Target:name() + " | Fire: " + Fire)
Explanation of the Script:
-
Directives:
@name: Sets the name of the E2 chip (useful for organization).
@inputs: Declares inputs from Wiremod components, such as a ranger (for detecting targets), a turret (to aim and fire), and a screen (to display information).
@outputs: Declares outputs, such as the Fire signal to control the turret's firing.
@persist: Declares variables that persist between script executions (e.g., Target and TargetPos).
-
Initialization:
- The
first() | duped() block runs when the script starts or is duplicated. It initializes variables to default values.
-
Main Logic:
- The
interval(100) function ensures the script runs every 100 milliseconds (adjustable for performance).
- The ranger is used to detect nearby entities. If a player is detected, their entity and position are stored in
Target and TargetPos.
- If a valid target is found, the script calculates the direction to the target, converts it to angles, and applies it to the turret.
- The
Fire output is set to 1 to trigger the turret's weapon.
- The target's name is displayed on a Wiremod screen.
-
No Target:
- If no valid target is detected, the turret stops firing, and the screen displays "No Target."
-
Debugging:
- The
print statement outputs information to the in-game console, helping you debug the script.
How to Use This Script in GMod:
-
Setup Wiremod Components:
- Spawn an E2 chip (Wiremod > Expression 2).
- Spawn a Wiremod ranger and link it to the E2 chip (right-click the ranger, then left-click the E2 chip, and select the "Ranger" input).
- Spawn a Wiremod turret (or a custom turret made with props and thrusters) and link it to the E2 chip as the "Turret" input.
- Spawn a Wiremod screen and link it to the E2 chip as the "Screen" input.
- If using a Wiremod turret, wire the
Fire output of the E2 chip to the turret's Fire input.
-
Paste the Script:
- Open the E2 editor by right-clicking the E2 chip and selecting "Open Editor."
- Paste the script above into the editor and click "Save and Exit."
-
Test the Turret:
- Walk into the ranger's detection range. The turret should aim at you, fire, and display your name on the screen.
- Walk out of range, and the turret should stop firing and clear the screen.
Key E2 Concepts Demonstrated
-
Variables and Data Types:
Target:entity stores an in-game entity (e.g., a player).
TargetPos:vector stores a 3D position.
Fire is a number (0 or 1) used as an output signal.
-
Conditionals:
if statements are used to check conditions, such as whether a target is valid or a ranger is active.
-
Entity Manipulation:
- Functions like
setAng and pos are used to control the turret's orientation and retrieve its position.
-
Wiremod Integration:
- The script interfaces with Wiremod components (ranger, turret, screen) using wirelinks and entity inputs/outputs.
-
Debugging:
- The
print function helps monitor the script's behavior in real-time.
Additional Notes
- Performance: E2 scripts are executed on the server, so complex scripts can cause lag if not optimized. Use
interval wisely and avoid unnecessary calculations.
- Security: On multiplayer servers, E2 scripts may be restricted to prevent abuse. Always follow server rules.
- Learning Resources: The Wiremod community provides extensive documentation and tutorials. Check the official Wiremod wiki or forums for more examples and advanced techniques.
If you meant a different "Expression 2" or another programming language, please let me know, and I'll provide the appropriate information!