Logic gate

From Valve Developer Community
Jump to navigation Jump to search

A logic gate is a model used to perform boolean operations on several logic inputs to produce a single output. In an in-game case scenario, the model can be used for a level designer to trigger a door open or closed, only if a certain set of buttons are positioned in either on/off state. A basic real case scenario one would eventually need in a level design could be lights in a room which have to turn on or off whenever someone switches one of the several light switches in the room.

In the Source Engine, there is no specific logic gate entity provided for use by level designers. However, two particular entities have the ability, when combined, to produce that effect.

There are very few cases in which logic gates should be used. Most of the time, simple thinking is enough to produce a good result on simple systems using the classic input and output system.

An AND gate can be performed using a nice set of inputs and outputs on a math_counter. The previously stated lights in a room can be done using Toggle inputs. Therefore, logic gates have little use in usual scenarios, but in tricky cases, logic gates can eventually be a valid approach.

Implementation

Consider an entity which has two states, such as a button, this entity is given a value for each of its two states (on/off): 1 / 0. In a Boolean aspect, it can be translated as true for 1 and false for 0. This state has to be stored in a logic_branch. In this scenario, the button acts as an input device.

A door can be considered as an entity that receives an output: It takes an input and changes from its default state (opening or closing depending on its settings). Giving a true input opens them. A logic_branch_listener listens to a logic_branch containing a Boolean value. When the value changes, the logic_branch_listener sends an output.


For combination uses of logic gates, the logic_branch_listener can output the result to another logic_branch to be listened by another gate.

It should be noted that when a listener listens to several branches, if one of the branches receives a SetValue input which will set it to the same state as it currently is in, it will not change the triggered outputs.

However, if a branch changes value, if this change is not likely to affect the output of a listener, it will still update its outputs.

It is possible to prevent this behavior by always outputting the result of a listener to another branch, and not directly trigger the output entity. The output entity can therefore listen to any change of the last branch using another listener.

Multiple listeners can listen to the same logic_branch.

Standard gates

The most basic logic gates can be implemented using a single logic_branch_listener per logic gate. Considering every single logic_branch_listener in the logic gate outputs its result in another logic_branch, this logic_branch can be used as a input for another logic gate.

Repeater Gate

Confirm:What is a "glitchy branch", is this ever actually necessary?

Sometimes a combination of logic_branch and logic_branch_listener can make Source crash under certain values (experienced in Portal 2 engine). Therefore you need to get rid of the glitchy branch. The Repeater Gate allows you to do it.

Let's say the glitchy branch is MyBranch. Get rid of MyBranch outputs, if applicable. Create a logic_branch_listener to monitor MyBranch. Create a logic_branch, say MyNewBranch. Now, use this combination of outputs:

Repeater Gate (one input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse MyNewBranch SetValue 0 0.00 No
Io11.png OnAllTrue MyNewBranch SetValue 1 0.00 No

MyNewBranch is now the image of the previous branch, minus the glitch. Use it in the first logic_branch_listener.


AND Gate

The AND gate listens to a list of multiple input logic_branches and outputs a TRUE value when all the branches have a TRUE state.

In the logic_branch entity, set it to output TRUE when all inputs are TRUE.

AND Gate (multi-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 0 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 1 0.00 No
Io11.png OnMixed branch_Z_N SetValue 0 0.00 No

OR Gate

The OR gate works whenever one or several logic_branches the entity listens to have a TRUE state. The mnemonic is "either a, b, OR both." In other words, it is an "either/or" relationship.

In the logic_branch entity, set it to output TRUE when the input have mixed values of TRUE and FALSE, or when all are TRUE.

OR Gate (multi-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 0 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 1 0.00 No
Io11.png OnMixed branch_Z_N SetValue 1 0.00 No

NOT Gate

The NOT gate outputs the boolean opposite of the single branch it listens to. This is usually not necessary as activate/deactivate inputs can simply be reversed on the entity producing the outputs.

When the input branch is TRUE, it outputs FALSE and when the input branch is FALSE, it outputs TRUE.

NOT Gate (single-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 1 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 0 0.00 No

NAND Gate

The NAND gate listens to a list of multiple output logic_branches and outputs a TRUE value when not all the branches the entity listens to have a TRUE state. It is essentially a NOT AND gate.

In the logic_branch entity, set it to output TRUE when the input have mixed values of TRUE and FALSE, or when all are FALSE.

NAND Gate (multi-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 1 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 0 0.00 No
Io11.png OnMixed branch_Z_N SetValue 1 0.00 No

NOR Gate

The NOR gate listens to a list of multiple output logic_branches and outputs a TRUE value when neither of the branches the entity listens to have a TRUE state. It is essentially a NOT OR gate. In other words, it is a "neither/nor" relationship where "neither a, b, NOR both" is true when the output is true.

In the logic_branch entity, set it to output TRUE when all are FALSE.

NOR Gate (multi-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 1 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 0 0.00 No
Io11.png OnMixed branch_Z_N SetValue 0 0.00 No

XOR Gate

The XOR gate listens to a fixed number of two branches and outputs only when one, or the other is TRUE, not both.

In the logic_branch entity, set it to output TRUE when the input have mixed values of TRUE and FALSE.

It should be noted that a XOR gate in a real implementation, only outputs TRUE when there is a odd number of inputs in a TRUE state. This implementation however, only works for a usage with two inputs.

In a real case scenario, a XOR gate is a simple model to visualize how multiple light switches interact with a single group of lights in a room. Whenever a light switch is turned into another state, it will always change the state of the lights.

XOR Gate (two-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 0 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 0 0.00 No
Io11.png OnMixed branch_Z_N SetValue 1 0.00 No

XNOR Gate

The XNOR gate listens to a fixed number of two branches and outputs TRUE only when both have the same state, either TRUE or FALSE, but not mixed.

This type of logic gate is what a mathematical comparator functions as. When the inputs are equal, the comparator outputs a TRUE. The comparator outputs a FALSE when the inputs are not equal.

In the logic_branch entity, set it to output TRUE when all are TRUE, or when all are FALSE.


XNOR Gate (two-input)
My Output Target Entity Target Input Parameter Delay Only Once
Io11.png OnAllFalse branch_Z_N SetValue 1 0.00 No
Io11.png OnAllTrue branch_Z_N SetValue 1 0.00 No
Io11.png OnMixed branch_Z_N SetValue 0 0.00 No

See also