Author Topic: E2 Guide (Wiremod Expression 2)  (Read 9445 times)

Trainjumper

  • Starting Member
  • *
  • Posts: 4
  • Karma: +4/-0
    • View Profile
E2 Guide (Wiremod Expression 2)
« on: April 27, 2015, 01:22:56 am »
Hello fellow Spacebuilders!

Since I started playing on the server I have noticed many people showing an interest in E2 but never getting around to learning it.

For those of you that don't know what E2 is, it stands for Expression 2,
It's a special chip in wiremod which you code yourself to do whatever you need it to.

It uses its own language, which has a syntax similar to C++ and Lua, and is relatively easy to pick up, even if you've never coded before.

I spent some time over the last few days putting together this guide for anybody who would be interested in learning it.
It's designed to be viewed in the E2 Editor, so just copy paste the text from the attachment into the GUI. (Although you don't have to)

If you have any questions, I'm on the server a lot of the time, so please drop by and ask if you see me!


-Trainjumper♥

↓Attachment Below↓ [Must be logged in to view attachment]
« Last Edit: January 07, 2017, 11:36:18 am by Trainjumper »
*Picks up phone* "Hello?"

bloxgate

  • Supreme Forum Overlord
  • Administrator
  • Hero Member
  • ******
  • Posts: 546
  • Karma: +50/-4
    • View Profile
    • Bloxgate's Website
  • IGN: bloxgate
Re: E2 Guide (Wiremod Expression 2)
« Reply #1 on: April 27, 2015, 05:18:48 pm »
Very nice, but perhaps you could put this in code tags so that people without an account can read it? It'll come out looking like
Code: [Select]
this. Also you didn't mention anything about @autoupdate which can come in quite handy for some situations, and if you don't mind I'd like to tack a few more advanced features onto here.
Bloxgate's Intermediate E2 Guide
Read Trainjumper's Guide First!

Logical Operators:
Most modern programming languages, including E2, have 3 basic logical operators:
  • Logical AND (&& or &)
  • Logical OR (|| or |)
  • Logical NOT (!)
These 3 operators can be used inside the conditions of control statements for various purposes. Let's say that you needed to execute an if statement when and only when two conditions were true. Now you could use a nested if statement, but that would just add extra lines and clutter increasing the potential for confusion. With the logical and operator you can simply do:
Code: [Select]
if(cond1 && cond2) Isn't that neat? The same applies for the logical OR and NOT operators, though I'll leave it to you to figure out uses for them.

Advanced Logical Operations
E2 includes several more advanced logical operators:
  • ( ? : )  Used as follows: A ? B : C short for if A is true return B, otherwise return C
  • ~ Returns 1 if current execution was caused by a change in that variable.
  • -> If used on an input returns 1 if it's wired. When used on outputs returns number of wires wired to that output

Intermediate Functions
In Trainjumper's guide he talked about using the built in functions of E2. I'm going to cover creating your own. Creating a function is fairly simple:
Code: [Select]
function name(){ #code } Then whenever you need to do your function you can just write name() instead of having to rewrite all the code.
Functions can be provided with arguments like so:
Code: [Select]
function myFunc(myArg:string){ print(myArg) } This function will take a string as an argument then print that string into the user's chat. This function would be called like so: myFunc("Print Me!"). You can do many things with functions, even calling or creating other functions inside a function. For the purposes of this guide, you should never call a function inside itself as that will create an infinite loop and crash your E2. Variables defined inside a function will be local to that function and can only be used in said function or its child functions. Variables defined outside of a function can be used any where inside your code, including inside a function.

Checking For The Existence of A Function
Warning: Potential Mindblowing Ahead: not all things that start with # are comments! There are 4 functions that start with #, 3 of which we will discuss here.
  • #ifdef
  • #else
  • #endif
#ifdef is used to check if a function exists. If your code attempts to call a nonexistent function, the e2 will not run! By using #ifdef you can check if a function exists and do a certain set of actions if it does. #ifdef is sensitive to the arguments you use, myFunc() is not the same as myFunc(string). #ifdef  Example:
Code: [Select]
#ifdef myFunction() print("Function exists")#else is used just like your standard else statement, unfortunately there is no equivalent of elseif for the function definition functions. #endif is the equivalent of a closing bracket for #ifdef and #else statements, analogous to the fi statement in some programming languages.

Includes
What if you want to do something really, really complicated that would require you to write hundreds, or thousands of lines of code just to make one thing work? No problem! There are various libraries for E2 on the wiremod forums, these can do all sorts of things for creating hologram letters to displaying images from the internet on a wire screen! How do I use these you ask? Simple download and save a library into your extension2 folder (garrysmod/data/extension2), then call #include in your code. Let's say you've download fancyLib that let's you do fancy things. In your code you can simply put
Code: [Select]
#include "fancyLib" and you'll be able to use all the functions from fancyLib. Fancy!

Wirelinks
Stay tuned for more E2 goodness. I will be updating this guide as often as I can!
« Last Edit: June 01, 2015, 03:10:54 pm by Rain »

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: E2 Guide (Wiremod Expression 2)
« Reply #2 on: April 27, 2015, 09:27:54 pm »
I'd love to see an example of how the @autoupdate works.  I've been meaning to check on that rather than trying to manually version my chips.

bloxgate

  • Supreme Forum Overlord
  • Administrator
  • Hero Member
  • ******
  • Posts: 546
  • Karma: +50/-4
    • View Profile
    • Bloxgate's Website
  • IGN: bloxgate
Re: E2 Guide (Wiremod Expression 2)
« Reply #3 on: June 09, 2015, 03:45:27 am »
@kevinminion @autoupdate is really simple. You simply put that in with all of the @ statements at the top of your code, then whenever the chip is duped it checks if its copy of the code (stored in the dupe) is older than your copy, if it is it updates the chip to the version you have saved.

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: E2 Guide (Wiremod Expression 2)
« Reply #4 on: June 09, 2015, 06:07:06 am »
Ah, that does sound simple.  I'll start using it!

kevinminion

  • Global Moderator
  • Full Member
  • *****
  • Posts: 147
  • Karma: +15/-0
    • View Profile
  • IGN: Kevin Minion
Re: E2 Guide (Wiremod Expression 2)
« Reply #5 on: October 14, 2015, 01:30:37 am »
Here's something that I learned tonight, which was driving me nuts.
 
Error:  UDFunction undefined at runtime
When the chip is pasted, duped( ) is called instead of first( ). dupefinished() on the other hand is used the when the duplicator has finished pasting the contraption. Your e2 will not wait for the duplicator to finish pasting everything before running its first execution.
 
I had my functions in an if (first())  block, which worked fine while I was working on the code, but when I duped it and pasted it back in, I got the above error.
 
Changing my code to if (first() | duped())   resolved the error.