Logic gate

From Valve Developer Community
Revision as of 20:55, 20 January 2011 by Omnicoder (talk | contribs) (grammar)

Jump to: navigation, search

A logic gate is a model used to perform boolean operations on several logic inputs to produce a single output. In a 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 actual direct implementation of logic gates 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, but in tricky cases, logic gates can eventually be a valid approach.

Implementation

Considering an entity which have 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, so it listens and keeps track of when its input value changes, and nothing else. Giving a true input opens them, and false closes them. A logic_branch_listener listens to a logic_branch containing a boolean value. When the boolean value changes, the state is read once again and processed.


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 supposedly set it to the same state as it currently is, it will not trigger the outputs.

However, if a branch changes value, if this change is not ought to affect the output of a listener, it will still update it and trigger 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 a 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.

The AND Gate

The AND gate listens to a list of multiple output 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

The OR Gate

The OR gate works whenever one or several logic_branches the entity listens to have a TRUE state.

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

The NOT Gate

The NOT gate outputs the boolean opposite value of the single branch it listens to.

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


The 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

The 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 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

The 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

The 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.

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