Logic gate: Difference between revisions
Hurricaaane (talk | contribs) No edit summary |
Le Glaconus (talk | contribs) (formatting) |
||
(27 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
A '''logic gate''' is a model used to perform boolean operations on several logic | {{LanguageBar}} | ||
A '''logic gate''' is a model used to perform [[boolean]] operations on several logic [[input]]s 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 | In {{src|4}}, there is no specific logic gate entity provided for use by level designers (except in {{strata|2}}, where {{ent|logic_gate|eng=Entity}} exists). 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 {{ent|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. | |||
An AND gate can be performed using a | |||
==Implementation== | ==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 {{ent|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 | 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 {{ent|logic_branch_listener}} listens to a {{ent|logic_branch}} containing a boolean value. When the value changes, the {{ent|logic_branch_listener}} sends an output. | ||
<br> | |||
For combination uses of logic gates, the {{ent|logic_branch_listener}} can output the result to another {{ent|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 {{code|preset=1|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. | |||
However, if a branch changes value, if this change is not | |||
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. | 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 | Multiple listeners can listen to the same {{ent|logic_branch}}. | ||
==Standard gates== | ==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 most basic logic gates can be implemented using a single {{ent|logic_branch_listener}} per logic gate. Considering every single {{ent|logic_branch_listener}} in the logic gate outputs its result in another {{ent|logic_branch}}, this {{ent|logic_branch}} can be used as a input for another logic gate. | ||
=== Repeater Gate === | |||
{{ModernConfirm|What is a "glitchy branch", is this ever actually necessary?}} | |||
<!--Editors note: I cannot take any technical documentation that uses the word "glitchy" seriously, so if this is in fact an actual problem, it should still be reworded.--> | |||
Sometimes a combination of {{ent|logic_branch}} and {{ent|logic_branch_listener}} can make Source crash under certain values (experienced in {{p2branch|2}}). 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 {{mono|MyBranch}}. | |||
Get rid of {{mono|MyBranch}} outputs, if applicable. | |||
Create a {{ent|logic_branch_listener}} to monitor {{mono|MyBranch}}. | |||
Create a {{ent|logic_branch}}, say {{mono|MyNewBranch}}. | |||
Now, use this combination of outputs: | |||
<center> | |||
{| border=1 cellpadding="2" cellspacing="1" | |||
|+Repeater Gate (one input) | |||
|- align=left style="background:#DCDCDC; color:black" | |||
! | |||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |||
| [[File:Io11.png]] || OnAllFalse || {{mono|MyNewBranch}} || SetValue || 0 || 0 || No | |||
|- | |||
| [[File:Io11.png]] || OnAllTrue || {{mono|MyNewBranch}} || SetValue || 1 || 0 || No | |||
|} | |||
</center> | |||
=== | {{mono|MyNewBranch}} is now the image of the previous branch, minus the glitch. Use it in the first {{ent|logic_branch_listener}}. | ||
The AND gate listens to a list of multiple | |||
===AND Gate=== | |||
The {{w|AND gate}} listens to a list of multiple input {{ent|logic_branch}}es and outputs a TRUE value when all the branches have a TRUE state. | |||
In the {{ent|logic_branch}} entity, set it to output TRUE when all inputs are TRUE. | |||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+AND Gate (multi-input) | |+AND Gate (multi-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
=== | ===OR Gate=== | ||
The OR gate works whenever one or several logic_branches the entity listens to have a TRUE state. | The {{w|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. | In the {{ent|logic_branch}} entity, set it to output TRUE when the input have mixed values of TRUE and FALSE, or when all are TRUE. | ||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+OR Gate (multi-input) | |+OR Gate (multi-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
=== | ===NOT Gate=== | ||
The NOT gate outputs the boolean opposite | The {{w|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. | When the input branch is TRUE, it outputs FALSE and when the input branch is FALSE, it outputs TRUE. | ||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+NOT Gate (single-input) | |+NOT Gate (single-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
===NAND Gate=== | |||
The {{w|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 {{ent|logic_branch}} entity, set it to output TRUE when the input have mixed values of TRUE and FALSE, or when all are FALSE. | |||
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. | |||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+NAND Gate (multi-input) | |+NAND Gate (multi-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
=== | ===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. | The {{w|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. | In the logic_branch entity, set it to output TRUE when all are FALSE. | ||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+NOR Gate (multi-input) | |+NOR Gate (multi-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
=== | ===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. | The {{w|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. | In the {{ent|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. | 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. | ||
Line 130: | Line 162: | ||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+XOR Gate (two-input) | |+XOR Gate (two-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
=== | ===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. | The {{w|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. | In the {{ent|logic_branch}} entity, set it to output TRUE when all are TRUE, or when all are FALSE. | ||
<center> | <center> | ||
{| | {| border=1 cellpadding="2" cellspacing="1" | ||
|+XNOR Gate (two-input) | |+XNOR Gate (two-input) | ||
|- style="background: # | |- align=left style="background:#DCDCDC; color:black" | ||
! | ! | ||
! My Output > !! Target Entity !! Target Input !! Parameter !! Delay !! Only Once | |||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllFalse || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnAllTrue || {{mono|branch_Z_N}} || SetValue || 1 || 0 || No | ||
|- | |- | ||
| [[ | | [[File:Io11.png]] || OnMixed || {{mono|branch_Z_N}} || SetValue || 0 || 0 || No | ||
|} | |} | ||
</center> | </center> | ||
Line 165: | Line 201: | ||
* [[Portal Level Creation]] | * [[Portal Level Creation]] | ||
[[Category:Portal]] | [[Category:Portal]] | ||
[[Category:Portal 2]] | |||
[[Category:Portal 2 Level Design]] | |||
[[Category:Level Design]] | |||
[[Category:Tutorials]] |
Latest revision as of 11:30, 8 July 2025
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 Source, there is no specific logic gate entity provided for use by level designers (except in
Strata Source, where logic_gate exists). 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

Sometimes a combination of logic_branch and logic_branch_listener can make Source crash under certain values (experienced in Portal 2 engine branch). 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:
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | MyNewBranch | SetValue | 0 | 0 | No |
![]() |
OnAllTrue | MyNewBranch | SetValue | 1 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 0 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 1 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 0 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 1 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 0 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 0 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 1 | 0 | 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.
My Output > | Target Entity | Target Input | Parameter | Delay | Only Once | |
---|---|---|---|---|---|---|
![]() |
OnAllFalse | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnAllTrue | branch_Z_N | SetValue | 1 | 0 | No |
![]() |
OnMixed | branch_Z_N | SetValue | 0 | 0 | No |