Expression Gates

From Garry's Goons

Jump to: navigation, search

Image:Expression.jpg

Contents

Introduction

The expression gate is a must-learn for anyone interested in creating wiremod stuff that would otherwise use more than a handful of chips. After trying to explain this to some of you, and failing to convince that you don't need to be a programming student to use expression gates, I've decided to put together this basic guide. I've literally only used it for a couple of weeks, so I'm not a supreme master, but I know enough to get you started.

Image:Protip1.jpg

What in Zeus' sweatsocks is an expression gate?

When you use a bunch of gates and wire them together to perform some function, you're creating a basic logic circuit. What the expression gate, or EG, allows you to do is to pack almost everything you need for your logic circuit into one chip. This cuts down on props, which cuts down on lag, which cuts down on general unhappiness. To do this, you use an extremely basic programming language.

Let me emphasize that this language is extremely basic and everyone can learn it if they keep reading this and play with EGs for fifteen minutes.

Image:Comparison.jpg

Before I go on, you should be aware that there are a number of fine resources already available on the Expression Gate:



The Expression Gate menu

Here is what your EG menu looks like:

Image:Expgate_menu.jpg

A. NEW EXPRESSION - Will open up the menu on the right (highlighted in green) with blank fields.

B. EXPRESSIONS - This window lists all of the EG files you have in your garrysmod/data folder.

C. REFRESH - Occasionally you may not see a newly-created EG -- click Refresh to find it.

D. EDIT EXPRESSION - Will open up the menu on the right, but allow you to edit existing information.

E. BROWSE EXPRESSIONS/FILENAME - Returns you to the menu on the left. GMod saves each of your EGs as a separate .txt file in the garrysmod/data directory. The filename is the name of this file. Incidentally, the sky is blue.

F. LOAD/SAVE/DELETE - They do just what they say. Be very careful of the delete button. If you accidentally click it, immediately click SAVE.

G. LABEL - The label is the name in the yellow box that people will see when they mouse over the chip.

H. INPUTS (variables) - In this field you will be defining the input variables that you will use in your EG. In other words: your chip will typically take in values and do something with them, and it is in this field that you will give names to these values. When wiring other components to your EG, you will pick from these input variables to designate where values are going. Variables must be capitalized and separated by a space. In the example above, the program needs to take in a bearing value and a 1 or 0 value from a numpad input, so the user has defined the input variables "Bear" and "On". If this doesn't make complete sense yet, just keep reading.

I. OUTPUTS (variables) - Output variables are formatted the same way as inputs -- they are capitalized and separated by spaces. They work just like inputs, but rather than defining what variables your EG takes in, you will here be defining the variables that hold OUTgoing values. In the example above, the user only requires an output for his thrusters, so his only output variable is "Thrust".

J. LINE 1-... - This is where your code will go.

Image:Protip2.jpg

Basic code

Again, I strongly recommend that you check out the links posted above. I'll go over a few of the basics, but I'm going to leave a lot out on purpose, as there's no need to repeat everything. I'm including some of the basics that I think are enough to get you started.

Intro: Never programmed before?

At this point I hope you realize that this isn't a problem: anybody with half a brain and some patience can do this. If you want a real mind-fuck, go check out the documentation for the CPU gate.

The basic programs you create work by looping your commands over and over again. As inputted values change, the program will operate continuously on these changing values.

At times I will show you sample code in the format that follows. This is how your code will appear in the text files in your garrysmod/data folder:

   code:N@Beacon Calibrator
   I@Dist Bearing Elev
   O@DistA BearingA ElevA
   DistA = Dist + 0
   BearingA = Bearing + 0
   ElevA = Elev - 10

The N@, I@, and O@ lines are, respectively, the EG label, Input variables, and Output variables. The remaining lines are the code. If you wanted to, you could set up a text file like this in notepad and write your code without ever opening GMod.

Variables

Variables store values, just like they did in algebra class. The main difference between those variables and these is that you can call these variables whatever you want, with the provision that they start with a capitalized letter (examples: VariableA VarA VB Boobs). We'll be concerned with two basic types of variables: (a) input/output variables and (b) interstitial variables.

I/O Variables: These are the types of variables that I described above. I/O variables store values that are coming in from outside sources, or values that are meant to go out. These variables are defined in the Input and Output fields.

Interstitial Variables: These are variables that exist only within the code and can help you organize and manipulate your numbers. Unlike I/O variables, interstitial variables don't need to be pre-defined, you create them in the code itself. So, while you'll list your input variables in the proper field like this: "Bearing Distance Elevation Pants", interstitial variables are created when you write something like this in your code: "BearingMinusDistance = Bearing - Distance".

Variables are important. Without them, expression gates can't do jack shit. It's important that you understand what they do.

Assignment Operator

The basic assignment operator assigns values to variables. There are other types that are more complicated and useful in various circumstances, but the one you'll use most often will go like this. If I have a variable "Greatanswer" and I want to assign a value of "42" to it, I would use the following line in my program:

   code:Greatanswer = 42

That's right, to assign a value to a variable, you write the variable, followed by a "=", followed by the value/expression you want that variable to take.

Arithmetic Operators

Alright, let's next learn how to do basic arithmetic. To do the basic four functions, you use the symbols [+ - * /] and parentheses as you normally would on a calculator. An example of how you might use arithmetic operators follows. The variable "Fart" is being assigned a value using pre-established variables Beans and Cheese, and the constant value 34:

   code:Fart = ((Beans + Cheese)/34)

Honestly, this is enough to get you started, but for the sake of giving you something more to work with, I'll teach you one more.

Using Comparison Operators in Conditional Statements

That may sound like a mouthful, but I promise you can swallow it. Basically, conditional statements check for a condition, then perform some function if that condition is met. In the layman's terms, you'll have an if/then line that does this:

   code:If (1)some condition is true, (2)then (3)do this thing that I specify.

Translating this into a line of code is slightly more complicated, but really not that bad. To make it easier, I've broken it down into three parts.

(1) "some condition"

This is where you define the condition. Usually you'll do this with comparison operators. For simplicity's sake I'm going to copy the available comparison operators directly from the Expression Gate Documentation: (N is a variable/expression)

   code:N == N : equal
   N != N : not equal
   N > N  : greater than
   N < N  : less than
   N >= N : greater or equal
   N <= N : less or equal

(2) "then"

In EG code, you translate this literally using the code:

   code:->

(3) "do this thing that I specify"

This is where you'll want to do stuff you've already learned involving assignment operators.


Putting it all together, a sample line could go like this:

   code:Range >= 50 -> Toofar = 1;

Important: There are two important things to know about conditional statements: (A) You need to include a semicolon at the end of each line. (B) Conditionals stay true once they're true unless you say otherwise. In the above example, once "Range" goes above 50, Toofar will keep holding a value of 1. Typically, you will want it such that when Range < 50, you'll want Toofar = 0, so you need to be sure to say that.

Example: A walk-through

To help wrap your heads around what you've just learned, follow along with me in this example; it includes everything you've just learned. It may look intimidating, but just try taking it one piece at a time.

Image:Sampleprog.jpg

What the Machine Does

The diagram is a loose sketch of the "Target Locker", a machine you would use with this expression chip. You'll see that it includes a beacon sensor and a ranger, both pointing in the same direction (not pictured: target locator and targets for beacon sensor). It also includes a gcombat turret, a numpad input, and a wired light. The machine does the following:

Hitting the numpad input will 'arm' the machine, turning on a warning light on top of the device. Then, when the target passes in front of the device, it will decide whether the object in front of it is the target or not. If it is, the turret will fire automatically. In other words, if the target is in front of the device, but a wall lies in-between, then the device will not fire. This is pretty handy to have on autoturrets that could potentially hit your own space station.

How it Works

From the perspective of the Expression Gate, here is what happens in one cycle of the program.

First, it identifies that it has three Input variables; 'BSdist', 'Rdist', and 'Arm'. 'BSDist' is the 'Distance' measure sent from the Beacon Sensor. 'Rdist' is the 'Distance' measure sent from the Ranger. 'Arm' takes in a 1 or a 0 from the numpad input. Next, the program identifies that it will have two out-going values; 'Fire' and 'LightOn'. 'Fire' will send the 1 value to the turret telling it to fire, while the 'LightOn' variable will turn on the wired light.

Next, it goes through each line sequentially. It's important to realize that the order of the lines can, and almost always does, make a difference. First, an interstitial variable called 'Diff' takes the value of the difference between BSdist and Rdist. The next line uses the "abs()" operator which simply takes the absolute value. Notice that 'Diff' is operating on itself to create a new value. A series of conditionals follows.

First, it checks to see if 'Diff' is less than 50; if it is, it sets another new variable, 'DiffA', to be 1. If 'Diff' is greater than or equal to 50, then 'DiffA' is set to 0. At this point, the program has checked to see if the distance to the target and the distance to the thing in front of the ranger are within 50 units of each other. If they are, then DiffA is 1.

Next, the program checks to see if DiffA is equal to 1, and if Arm is equal to 1 (that is, if you armed the system). If these two conditions are satisfied, then 'Fire' is set to 1 and your contraption will fire its weapon. If, on the other hand, 'DiffA' is equal to 0, then the device does not fire. The same is true if 'Arm' is equal to zero.

The last portion is for the Arming light. It first checks to see if 'Arm' is equal to 1. If so, "LightOn' takes a value of 255 which, when wired to a light, turns on a light to that degree of color. If 'Arm is equal to 0, then the light will not turn on.

Personal tools
Garry's Mod Information
Getting Started with Goons