+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Pac's E2 Tutorial

  1. #1
    Banned Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,251

    Default Pac's E2 Tutorial

    Welcome to my tutorial, my name is Pac.
    In this tutorial I will teach you the very basics and more in the Expression2 syntax and the chip itself.
    The more you scroll down the more advanced the code will get.
    I have never made a tutorial before so this is my first try.
    This being said, let's start the damn tutorial!
    I also recommend basic/advanced knowledge of gates before starting on Expression chips.

    What is an Expression2?
    The Expression2 chip is a chip that you can code to fit whatever you wish to build/create. This allows you to actually replicate wire gates and put hundreds of gates in one as well as doing hi-speed calculations and data transfer.
    The Expression2 can handle different types of data, including: Vectors, strings, angles and numbers.
    This reduces lag and increases efficiency as well as simplifying contraptions. With this tutorial and the Expression2-wiki you will be able to learn the basics and then experiment your way to becoming an advanced user of Expression2 chip.


    The very basics.
    We will start off with a basic I/O (Input/Output) chip, acting as an Identity(No change) chip.
    Code:
    @inputs In
    @outputs Out
    In = Out
    See, that wasn't so hard after all. What we do here is making an input called In and an output called Out, they are variables. Variables are used to mostly used to store a single value, but they can also be used for storing vectors, strings and angles. But no vectors and strings, just for now. You can also give variables every kind of name you wish, including: "Pie" "OMFGLOLWTFBBQ" (A variable has to ALWAYS start with a capital letter!"

    Also note that I have removed the @persist and @trigger that usually shows up when you make a new Expression2 code. It's because they are not needed unless you are using them for something.

    IE: Storing a persistent (@persist) variable inside the expression chip. The @trigger is for something more complicated and we will reach to that later.

    Now for something else a little longer syntax, but not less understandable!
    What we are moving on to now is the "If-Then" functions. These are written as "if (This) {Then this}" in Expression2 code. You can put commas after the "Then this" like: "if (This) {Then this, and this}"
    A simple code: (Note: I will also be using a persistent variable in this code, "A")
    Code:
    @inputs In
    @outputs Out
    @persist A
    A = In - In
    if (A == 0) {Out = 1} else {Out = 0}
    In this code, A equals to In subtracted by In which will always equal to zero. The "If-Then" function is also using an "else" in the code, why?
    the "else" function will check if the argument started after the "if" is false or true, if it's false, then whatever is behind the "else" function will be true.

    Back to the code. In the code you can see the strange "if (A == 0)", what does that mean? I'll explain it. The "==" is one of the most used functions, it asks a question: "Is this equal to that?", in the code this means if A equals to 0 (As it will always do because of A = In - In) then argument is true and go read what's inside the { and }.
    In the code above, you can see the else {Out = 0}, that will never happen because the argument after the "if" function is always true

    If-Then statements can also be written in a completely other way! This way is the one I use mostly. It may be a little more difficult to handle but it is the best for making a single variable equal something else something.
    It's written as "Out = (A == 0 ? 10 : 0)" That will make "Out" equal to 10 if A equals to 0, else it will output 0. An example code below:
    Code:
    @inputs In
    @outputs Out
    Out = (In == 0 ? 1 : 0)
    If In equals to 0 then it will return 1 and Out will output 1. The question mark is pretty much like the { and } ("Then" function"), the first variable being the one outputted if the statement is true, the second being the "else" function which will get outputted if the argument is false. Next chapter.


    The basic syntax.
    In this chapter, I will tell you all the functions asking questions like the "==" function in the previous chapter.

    Is it equal?
    ==

    Is it greater than?
    >

    Is it less than?
    <

    Is it not equal?
    !=

    Is it greater or equal?
    >=

    Is it less or equal?
    <=

    All of these commands can be inserted instead of the "==" we learned about in the previous chapter.


    Interval, timers and @trigger.
    Interval and timers are used to trigger the expression (Getting the Expression2 chip to read the code again).
    Expression2 can go as low as interval(10), making the chip continuously re-read the code each 10 millisecond (ms).

    Timers are pretty much the same except you can give them names and they can be used for example: Creating an actual output switching back and forth 0 and 1 each 1 second even though interval(10) is added.

    And do not start thinking that a timer is like a Timer-Gate, they are used to trigger the expression and nothing else, really...
    An example code of remaking the actual Timer-Gate could be this:
    Code:
    @inputs Run Reset
    @outputs Out
    interval(100)
    if (Run) {Out += 0.1}
    if (Reset) {Out = 0}
    Note the "+=", it's quite simple and just increases the Variable by a number.
    In a way more advanced way, this timer could be written as this:
    Code:
    @inputs Run Reset
    @outputs Out
    interval(100)
    Out =(Out+Run)*!Reset
    A quick explanation of that is this: Out equals to Out+Run, increasing it all the time. Multiplied by !Reset (! being Logical-Not (Outputting 1 when Reset is 0 and invert)) So if Reset is 1, the Out will output 0.
    The @trigger is quite simple actually, you just write either "all" or "none" or an Input or more. Example: Writing "@trigger all" will make it trigger from all inputs, timers and intervals. "@trigger none" will only trigger it from intervals and timers. (More on this in the update)


    & and |
    & and | are both very commonly used in the Expression2 coding language. They are known as "And" and "Or", & being and, | being or.
    An example use of this could be:
    Code:
    @inputs A B
    @outputs Out
    if (A == 1 & B == 1) {Out = 1} else {Out = 0}
    If A is equal to 1 and B is as well then Out will return 1. Replace & with | to get Out equal to 1 if A or B equals to 1. You could also write "if (A & B ) {Out = 1} else {Out = 0}", writing just a variable name is as saying if "Variable is not equal to 0" (!= 0) then return 1. It could also be written as "Out = (A & B )".
    This was a quick explanation but it's also very simple to use.


    Gate examples
    In this chapter I will post Expression2 codes on how to actually make stuff like Timer gates, Add gates, Or gates and so on...

    Arithmetic and time gates

    Add
    Code:
    @inputs A B C D E F G H
    @outputs Out
    Out = A+B+C+D+E+F+G+H
    Or
    Code:
    @inputs A B C D E F G H
    @outputs Out
    Out = (A | B | C | D | E | F | G | H)
    Accumulator/Timer
    Code:
    @inputs Run Reset
    @outputs Out
    interval(100)
    Out = (Out+Run)*!Reset
    Inc/Decrement
    Code:
    @inputs Up Down Reset
    @outputs Out
    Out = (Out+Up-Down)*!Reset
    These are all examples of how you could do a replication of a normal wire gate.
    Now you should try and do some experiments in-game.


    The more advanced user.
    In this chapter we will learn more advanced syntax and more.
    We will start off with "~", "clk()" and "$".

    ~ and $
    The "~" function is used to see if an input has changed. This allows the creation of easy "toggle" codes and hundreds of other things.
    You can achieve the same effect through delta.
    Delta is written as "$", think of it as money, because it's really worth gold.
    Delta is the rate of change of a variable. You can think of it as a virtual-speedometer measuring the speed of a number.
    The delta ($) only works on variables. An example use of the delta could be to create a self stabilizing platform.
    Let's see how we would create an toggle gate using "$" or "~"
    Code:
    @inputs A
    @outputs Out
    if (A & ~A) {Out = !Out}
    Now what does !Out mean? It means the opposite of what the current number is. It inverts the number so if the current Out was 0, it would then go 1 and reverse.
    You can replace the "~A" with "$A" and archive the same effect. The code could also be written as this:
    Code:
    @inputs A
    @outputs Out
    Out = (A & ~A ? !Out : Out)
    You will get the same effect as the code above.
    Now for the "clk()"

    clk()
    "clk()" is used to check if the Expression2 is being run by an interval. It can be used with timers as well by just putting "NameOfTimer" between the parentheses.
    This can be used to also output 1 each X millisecond.
    Code:
    @outputs Out
    interval(1000)
    Out = clk()
    Out would return 1 every second. Using it with timers would then be:
    Code:
    @outputs Out
    timer("HELLO", 1000)
    Out = clk("HELLO")
    Not that hard.


    Updates coming!
    Last edited by Nicolai1; 04-09-2009 at 05:37 AM. Reason: UPDATED

  2. #2
    Expressionism 2.0 Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,573

    Default Re: Pac's E2 Tutorial

    Very nice!
    Something like this is definately needed, however, I think you're getting at the if-statements a tad bit too fast. It's usually where it starts getting tricky for people. Especially since that means you have buttons and need to start thinking about ~Button, clk(), etc.

    (EDIT: and I think you forgot to mention @trigger :P, but it feels like it could also be a way to overcomplicate a bit stuff a bit too fast)

    Also you are using @persist wrong, @persist should ONLY be used when you want the expression to remember the value, aka, when you do "A += 1" not when you do "A = 1".

  3. #3
    Banned Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,251

    Default Re: Pac's E2 Tutorial

    Quote Originally Posted by Syranide View Post
    Very nice!
    Something like this is definately needed, however, I think you're getting at the if-statements a tad bit too fast. It's usually where it starts getting tricky for people. Especially since that means you have buttons and need to start thinking about ~Button, clk(), etc.
    Thanks!
    It's my very first tutorial so I highly appreciate critics and I might also just think about adding another chapter in the beginning explaining a bit about the wiring of it.

  4. #4
    Banned Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,251

    Default Re: Pac's E2 Tutorial

    Quote Originally Posted by Syranide View Post
    forgot to mention @trigger :P, but it feels like it could also be a way to overcomplicate a bit stuff a bit too fast)

    Also you are using @persist wrong, @persist should ONLY be used when you want the expression to remember the value, aka, when you do "A += 1" not when you do "A = 1".
    Added the @trigger and I have only used persist one time and it was an example, I could just write 0 instead but this was to show how it was done.

  5. #5
    Wire Sofaking Echo51's Avatar
    Join Date
    Feb 2009
    Location
    Denmark
    Posts
    1,489

    Default Re: Pac's E2 Tutorial

    What about OR's/||'s in a if? :P
    Proud of my non-existant life...
    In russia, Expression 2 codes you!
    Quote Originally Posted by Snowden42 View Post
    Nooblishious? That's a signature keeper.

  6. #6
    Wirererer malum's Avatar
    Join Date
    Feb 2009
    Posts
    217

    Default Re: Pac's E2 Tutorial

    One |, Echo

  7. #7
    Wire Sofaking Echo51's Avatar
    Join Date
    Feb 2009
    Location
    Denmark
    Posts
    1,489

    Default Re: Pac's E2 Tutorial

    Ah, thankie
    Proud of my non-existant life...
    In russia, Expression 2 codes you!
    Quote Originally Posted by Snowden42 View Post
    Nooblishious? That's a signature keeper.

  8. #8
    Expressionism 2.0 Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,573

    Default Re: Pac's E2 Tutorial

    Quote Originally Posted by Nicolai1 View Post
    Added the @trigger and I have only used persist one time and it was an example, I could just write 0 instead but this was to show how it was done.
    Yeah of course, but what I am saying is that it is the wrong way to use it, using it like that will make people think you have to use it for ALL variables that aren't inputs or outputs which isn't true (and many already believe that).

    A better example is an accumulator or counter.

  9. #9
    Banned Nicolai1's Avatar
    Join Date
    Nov 2008
    Location
    Denmark.
    Posts
    1,251

    Default Re: Pac's E2 Tutorial

    Quote Originally Posted by Syranide View Post
    Yeah of course, but what I am saying is that it is the wrong way to use it, using it like that will make people think you have to use it for ALL variables that aren't inputs or outputs which isn't true (and many already believe that).

    A better example is an accumulator or counter.
    If you read the text beneath the code you'll see that I explained it would always be 0 and more...

  10. #10
    Expressionism 2.0 Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,573

    Default Re: Pac's E2 Tutorial

    Quote Originally Posted by Nicolai1 View Post
    If you read the text beneath the code you'll see that I explained it would always be 0 and more...
    It's still bad mmmmkay.

    Code:
    @persist A
    A = 1 # bad! we don't care what A was previously set to
    Code:
    @persist A
    A++ # good! we want it to remember what A was so it can count up
    That's what I mean.

    The ONLY reason why one should use persist is for when you have values that should be remembered between executions, aka, like counters, timers, smoothers, etc, etc or when you are using delta on them.

+ Reply to Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. My first tutorial of a series
    By Donatti in forum Gate Nostalgia (Old School Wiring) Discussion & Help
    Replies: 3
    Last Post: 02-16-2009, 06:53 PM
  2. Need tutorial :(
    By busido95 in forum CPU, GPU, and Hi-speed Discussion & Help
    Replies: 5
    Last Post: 02-10-2009, 12:10 PM
  3. Need tutorial
    By fredrik123 in forum Installation and Malfunctions Support
    Replies: 2
    Last Post: 11-11-2007, 11:35 AM
  4. Putting a video tutorial in the gmod tutorial list
    By Def the world in forum Installation and Malfunctions Support
    Replies: 1
    Last Post: 08-21-2007, 10:04 PM
  5. Missle Tutorial Req.
    By alan in forum Gate Nostalgia (Old School Wiring) Discussion & Help
    Replies: 9
    Last Post: 08-15-2007, 12:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots