Garry's Mod

Garry's Mod

181 ratings
Guide to Wiremod (Expression 2)
By Skeleton Man
A guide to the popular Expression 2 chip in Wiremod. This will cover the Syntax, Datatypes, and some basic functions to get you started.
2
2
   
Award
Favorite
Favorited
Unfavorite
The first few lines
Expression 2 is essentially scripting your own chip to do things for you. It has a syntax of its own that you must learn if you want to be effective with it.
This image explains all of the @directives you will have to initally use. To define a variable in @inputs, @outputs, or @perists, it must always start with an uppercase letter. Sometimes, you may have to use a different Datatype. These can be defined by putting a colon (:) after the variable and typing the corresponding datatype, as shown.

Datatypes are special, they define what kind of data that a variable contains. Typically you cannot directly compare or operate on different datatypes without converting them to the same datatypes, either temporarily or permanently. Example:

Number
Numbers are referenced as they appear, with no special characters. They are light red in the E2's editor. They can logically be added, subtracted, multiplied, ect, as required. Ex: 69

String
Strings are any collection of letters, spaces, numbers or otherwise characters. They must be enclosed in quotes. Strings can be a pain to work with as you usually have to use functions to manipulate them, such as adding or subtracting characters from it, or finding and replacing things in it. If a string is composed entirely of number characters, you should be able to use functions to convert the string to a number so you can preform mathematical operations on it. Ex: "69", or "This is a test string."

List
Lists are one reference to a number of distinct values. They start with the list() function and label out all of their values. Different datatypes can be contained within a list, however this is not always wise as whatever code you run will have to take into account potentially different datatypes throughout a list you are looking through. Ex: list(69,"69","Test String.")

Vector
A vector is a list of three numbers. It is distinct because these vectors (commonly known as 3D Vectors) are often used to reference locations within the game world, among other things. Vectors are somewhat distinct from a simple list of three numbers as many more functions relating to position become available when you define something as a vector. Additionally, for those math-inclined, 2D and 4D vectors exist with the vec2() and vec4() functions, however these only have use in very specialized applications. Ex: vec(-100,200,0)

Angle
Angles are simply a number. Angles are distinct as they have specialized rules for adding and subtracting, and many more functions relating to angles become available. Ex: ang(90)

Array
Arrays are very similar to lists, except each value in the array has an 'index' automatically assigned to it, which essentially tells you what position the item is in the array. This has a number of specialized uses, but you will not find yourself mucking with arrays for quite a while. Arrays are typically more flexible than lists, however the simpler approach of a list may be more useful.

Entity
Entites are references to in-game objects, NPCs, or items. These are very special as they allow you to access all data and properties about that entity, and furthermore you are able to manipulate it. Special to NPCs are many functions that can handle rudimentary AI commands, such as walkto(), runto(), lookat(), and commands to set relationships between the NPC and other entities (which will determine if they attack it, among other things).

You cannot assign a variable with one datatype that of another. If you have variables that need to switch datatypes, you have to define two different variables.

If you're not using one of the @directives, it's okay to delete it.
All of the other lines, and the Syntax
Above is shown the basic syntax of E2.
There's a few additional rules:
  • When setting a Variable equal to something the variable always comes first, then the operation that makes it equal.
  • When checking to see if a varible equals something using the if() command, you always use two equals signs (==).
  • Functions must always have two closing parentheses after them, even if they have no arguments, such as owner(), or changed(Variable).
Syntax: Loops
A "Loop" is defined as something in the code that is run based on another condition. Loops are great to repeat functions of a code over and over. E2 supports the following loops:
  • If-Then-Elseif-Else
  • For
  • Foreach
  • While
  • Continue
  • Break
Each has its own application. We'll start with the basic If-Then loops, which you will use the most when coding.

If-Then-Elseif-Else
This loop works as such:
if(This Condition is True) {Do This}
So, for example:
if(A==1) {Output=1}
If the variable, A, equals exactly 1, then the variable, Output, will be set to 1. If A isn't equal to 1, the code between the curly brackets is ignored completely and the chip moves on. These are very common and will be great to make the chip only do certain things if certain conditions are met, and are fairly easy to set up.

In Addition to merely using an If-Then, you can add the next line as else{}. For example:
if(This Condition is True) {Do This} else {Do this if the previous condition wasn't true}
This code means that if the previous condition wasn't true, such as in the first example A not being equal to 1, then the code would skip the first set of commands in the curly brackets and execute the commands inside the second set, after the else loop.

Instead of having one line of code run automatically after an if-then, you can create an If-Then-Elseif loop. For Example:
if(This Condition is True) {Do This} elseif(This Condition is True) {Do This} else{Do This}
This means that if the first condition wasn't true, skip the information in the brackets and check if the next condition is true. If THAT condition isn't true, then it runs the commands inside the else{} brackets.

You can also put loops inside other loops. For example:
if(This Condition is True) {if(Check if this condition is true) {Do this if both are true}}
This script will check if the first condition is true. If it is, it checks if the second is true also. If it also is, it does whatever's after the second condition's curly brackets.

This is sometimes redundant or not needed, depending on what you are trying to do. The if statement can take multiple conditions and check if the whole statement is true, not just one condition at a time. Ex: if(A==1 && B==1) checks if both A and B are equal to one. If either value does not equal one, the following code is ignored. Using this would make the second if() statement redundant, but that's merely a matter of making it look good. Use whatever you think works.

Rules:
  • An else loop must always come after an If statement or an Ifelse statement.
  • An elseif loop must always come after an If statement.
  • You have to use two equals signs (==) to check if a condition is equal to something in any if condition. Use one equals sign (just =) will SET the values equal to one another. You don't want that majority of the time.

While
A while loop is simple. While the condition is the parentheses is true, it performs the code inside the curly brackets. For example:
while(This Condition is true) {Perform this code}
Note, that the condition between the parenthesis is only checked at the beginning of the code. If you try to set the condition's value to something different inside the curly brackets of the while loop, it won't stop the rest of the code in the curly brackets from executing.

You can, however, change the value for the next check of the While loop. Ex:
while(A < 10) {A++}
Presuming A starts at 0, this adds one to A until it is 10. Every time the code finishes adding one to A, it checks the while loop again to see if it can be run.

For
For loops are like While loops, but more flexible. A for loop works like this:
for(A, B, [C]) {Do These commands}
Where A is the starting value, B is the target value, and C is the increment value. C is optional, so if you do not have a third argument C is automatically set to 1.
The for loop will execute the code between the brackets until A = B.
Remember that A, B, and C are only calculated at the start of the loop, and if the value is changed inside the loop, it will not stop the for loop mid-calculation. These are especially useful to loop through every value in a list of values, or something similar.

Foreach
A special type of for loop for tables. Works as such:
foreach(Variable,Value:datatype=Table)
Not often used because Arrays are usually easier than tables. However, it will assign the information it retrieves from the table to Variable. Anything that doesn't match the defined datatype will be ignored. The loop will not process data that was added to the table from inside the loop.

Continue
Can only be used inside a For or While loop. When used, this will cause the E2 to immediately ignore all the code below it in the loop and return to the start of it. This could be useful to skip executing of some of the code if certain conditions are met (by using an If-Then statement).

Break
Can only be used inside a For or While loop. When used, it will cause the E2 to stop everything the loop is doing and stop executing it until the condition in which causes it to execute is met again. This essentially stops a loop completely, as opposed to using Continue, which just skips it one time.

Again, this is a list of all loops. You will usually only be using If-Then-Elseif-Else until you have some more advanced problems that you would need to solve.
Syntax: Shorthand Operations/Conditional Statements
In addition to checking if a value is directly equal to something using if(A==Value), you can use the following:
  • == - Is equal to
  • != - Is not equal to
  • > - Greater Than
  • < - Less Than
  • >= - Greater than or Equal to
  • <= - Less than or Equal to

Other shorthand includes the following:
  • A++
    Using this will increase A by one every time the chip runs this code. If A were 1, and the code ran this, it would be 2. Once the code ran it again, it would be 3, and so on. This is the same as doing A=A+1.
  • !A
    The ! means Not. This means, Not A. It is used inside any conditional loop, such as if(!A). This will invert the output of any code it is set before. If the output were to be 1, it would then be 0 if it had a ! infront of it, and vice versa.
  • A&B
    The ampersand (&) signifies an And statement, and is used in a conditional loop, such as if(A&B). For an And statement to return true, all of the inputs to it must be true. So for this condition, A and B must be 1 in order for the if statement to run the code between its curly brackets.
  • A|B
    The pipeline (|) signifies an Or statement, and is used in conditional loops, such as if(A|B). For an or statement to return true, at least one of the inputs must be true. For this condition, the only way it can return false is both A and B are 0.
  • ~A
    The tilde symbol (~) is used to check if the current execution of the chip was caused by a change in the variable, A. This means, that if the chip is running because A changed value, it will return a 1. Often, this is used in conditional statements, such as if(~A&A), which would mean, If the button changed, AND the button is not off, then the statement is true. Can only be used on an input variable.
  • $A
    The dollar sign ($) will give you the Current Value of A minus the value of A on the last execution of the chip, essentially giving you the difference between A on the last run and A now. Useful for finding the amount of change in something. Can only be used on variables that can logically be subtracted.
  • ->A
    If A is an input, it will return 1 if A has been wired to something. If A is an output, it will return the number of objects it is wired to.
Syntax: Common Functions
A function in Expression 2 is anything in dark blue. These commands can call information or manipulate variables. Here I will be listing common functions that you can use to help detect inputs or outputs, or detect variables. However, most of the battle of E2 is learning how functions work and how to apply them, so if you need help, you should consult the wiremod wiki for information on how a function works. A function always starts with a lowercase letter, and every word after the first has an uppercase letter.

Keep in mind, calling a function takes some processing power. Some functions, such as ones that find entities or manipulate them, take more processing power than others, such as ones that only create vectors or angles. To cut down on processing power, you should save commonly used values into variables so you don't have to use functions to recall them in the same execution of a chip. This amount of usage is measured in "ops" or "Operations per Second". E2 has a hard limit of 5000 Ops, which would show up as 100%. E2's that use 2000+ ops can usually be considered poorly optimized, depending on their functions. If your E2 uses too many Ops, it will shut down to prevent the server from lagging and must be manually restarted.

  • changed(Variable)
    Very useful function. Used in conditional statements, such as if(Changed(Variable)). It will check to see if the variable has changed from its last execution. If it has, it will return 1. Add this along with an and statement, such as if(Changed(Variable)&Variable==1) to prevent the code in the brackets from repeating every time the chip updates, such as if you want something to add together only once.
  • hint("text",number)
    This displays a hint on the bottom right of your screen, much like when you undo a prop. Replace "text" with anything you please, just make sure it is in quotations. The Number is the length of time in seconds the hint displays, up to a max of 7 seconds.
  • first()
    Very, Very useful function. Used in conditional statements, such as if(first()). This function checks to see if this is the first execution of the chip. If so, it returns a one. This allows you to set defaults for variables.
  • interval(number)
    Using this function will tell the chip to run every number of milliseconds you specify. Good to ensure your code keeps updating without having to change inputs or outputs.
  • runOnTick(number, 1 or 0)
    Using this command will make the Expression update every game tick if it is set to 1. Be careful as with large amounts of code this can generate a very high number of Operations on your chip, potentially causing lag. Keep in mind that a game tick is a very, very short amount of time.
  • runOnChat(number, 1 or 0)
    Will make the expression update every time there is a new chat message. Good for chat based expressions.
  • vec2(number,number) vec(number,number,number) vec4(number,number,number,number)
    These commands are used to create 2D, 3D, and 4D vectors respectively. If you do not specify any numbers, such as using vec(), it will create a vector where all the arguements are 0. Good for defining vectors.
  • Entity:pos()
    The pos() argument will return the position in world coordiantes of the entity. Good for getting your own position or the position of things in the world so you can manipulate them. It always returns a 3D vector. To get your own position, use owner():pos()
  • owner()
    Returns the owner of the expression chip. If you put an Entity before it, such as Entity:owner(), it will return the owner of the specified entity.
  • owner():lastSaid():lower()
    A very useful group of functions. Will return the last said chat message from the owner in all lowercase. Very useful for making the chip run on chat commands.
  • reset()
    Using this command will compeltely reset the Expression, effectively the same as respawning it.
These are functions you will probably use the most. Consult the wiki at https://github.com/wiremod/wire/wiki/Expression-2 for a near-complete list of functions.
Closing
Add me on steam if you need specific help, or drop a comment. I almost always reply to intelligent comments.
128 Comments
Skeleton Man  [author] Jul 6 @ 2:32pm 
this guide is so obscenely outdated that wiremod might have moved on to other avenues. Unfortunately I couldn't tell you, sorry.
alfwhitto Jul 6 @ 4:28am 
where can i find E2 now?
Skeleton Man  [author] Jun 4 @ 7:35am 
I dont have any experience with spawning props via E2 and its been a long while since ive messed with it. However, it would seem that the functions you normally use may be from an add-on or mod that allow e2 to run those functions. For setPos(), since its giving you “no such method”, this could mean that whatever “e” is set to is not currently an entity, and you wouldnt be able to set the position of something that isn’t an entity. Id check or ask what mods the server is running and see if they can help.
Qyasar Jun 4 @ 3:04am 
I know there is a way to spawn props using e2 as i am in a server with some such value or setting turned on that allows for it but whenever i go into different servers/private servers ill get a warning of some variation that explains "No such function: propSpawnEffect(n)" or "no such function: seatSpawn(s, v, a, n). or "no such method: e:setPos(v). Please let me know how to turn this on
NFL Youngboy Sep 6, 2023 @ 8:44pm 
wiremod is basically modding beginners stage 2
[TR] James [YT] Mar 28, 2023 @ 8:35pm 
I need help with trying to get velocity from props in E2 like vel but then convert it to number but also with findIncludeModel and findInSphere
Martin Luther king Mar 17, 2023 @ 9:59pm 
how long will this take to learn this coding
FoxMechanic Jan 7, 2023 @ 2:42pm 
thanks! I'll try it later today
Skeleton Man  [author] Jan 7, 2023 @ 2:27pm 
Create a variable that gets tripped only when that condition is met the first time and add that variable to your condition
@persist ConditionLatch
if (MyCoolCondition and !ConditionLatch)
{Do thing
ConditionLatch = True}
FoxMechanic Jan 5, 2023 @ 3:46pm 
I need some help. I'm trying to find a way to code an if () then statement that updates once when the condition is met and never again during the condition being met.