Jump to content

View New Content
- - - - -

E2 for beginners

  • This topic is locked This topic is locked
No replies to this topic

#1 my_hat_stinks


  • 4,340 posts

    Not a people person

Posted 22 February 2011 - 14:59

Remember, if you need to use restricted tools, click Here!

This guide will turn someone without the slightest clue about scripting into an E2 expert! :D (Or good enough to use E2, at least)

I'll start with the basics, and add to the guide often
The later parts will be focussed more on spacebuild (Which is why I'm putting this in the spacebuild section)

The thread will start locked, as I'm not sure how much extra space I'd need, and don't want to split up the guide

Lesson 1
Understanding Variables

First you should get familiar with the E2 tool
Left click will place an E2 chip, and right click will open the E2 editor
I usually write the code and save it before placing the chip, but it makes no real difference which order you do it in

Posted Image
The rest of the code (Not shown) is what's known as a "comment" and is ignored. It is used to tell us mreo about the code, so we don't get lost when editting

You can see in the editor that there are 5 lines starting with @

You can ignore @trigger, I don't know what it does and haven't yet needed to use it. I usually just remove this line

@name will name your script, eg "@name Hello World!"

@input, @output, and @persist declare* variables**
@input <Variable(s)> Declares variables that will receive values from other wire sources
@output <Variable(s)> Declares variables that will give values to other wire equipment
@persist <Variable(s)> Declares variables that will be used only within the script

Variable declaration is important, if you don't declare a variable you can't use it in the script

Variables in E2 must always start with a capital letter, and can't use spaces. This means that instead of "@input input button" you should use "@input InputButton" or "@input Input_Button"

The three main variable types in E2 that we will use are Number, String, and Vector
Number is a value, and if you don't specify a type of variable it will be number. Eg "@input Button" will be a number
String is text, and is specified with the :string extension. Eg "@output Target_Name:string" will be a string
Vector is a set of three X Y Z values, stored in the format (X,Y,Z). Eg "@input Target_Location:vector"

Variable types only need to be stated when the variable is declared (ie "Name:string" can be used during the script as just "Name")

Posted Image
You may notice I have two "@output" lines, this works just the same as one line, but helps keep it organised

That's the end of lesson one. If you understand variables, you're ready to start E2. If you don't, then you can either ask me through steam or try and pick it up as we go

*I'm using declare here as "Name the variable" rather than "Set the value"
**Variable: A storage location. In this guide, you will use Number, String, and Vector variables

Lesson 2
Hello World

Now it's time for our first script
As with any language, a nice starting script is "Hello World!", basically just an output saying hello world

All we need to know for this is how to make an output and how to set a string, and put "impulse(1)" after the variables to renew every 1 millisecond
Declaring variables has been covered in the previous section, so you should already know that.

To set a variable to a constant value depends on what type of variable it is you are setting
Number: Constant_Value = 1
String: Constant_String = "Words! :D"
Vector: Vector_Red = vec(255,0,0)

Now, see if you can put this together into a simple script to output "Hello World!" to a text screen
Make sure you have the text screen displaying something else first, or you won't be able to tell if it changes.

Posted Image

A possible solution (Highlight to see) :
[color=#FFFFFF]@name Hello World
@output Hello:string

#Setting the constant value
Hello = "Hello World"[/color]

Congratulations, you just made E2!

Lesson 3
If Then Else

An If statement is one of the most basic ways in a few languages to make something happen conditionally

An If statement in E2 can take the forms:
if (<Variable/value> <Operator> <Varible/value>) {<Actions when true>}

if (<Variable/value> <Operator> <Varible/value>) {<Actions when true>} else {Actions when untrue}

if (<Variable/value> <Operator> <Varible/value>) {<Actions when true>} elseif <If statement>

Posted Image
An example of an If statement

There are a few different operators you can use in an If statement:
"==" Equals
"!=" Does not equal
">=" Greater than or equal to
"<=" Smaller than or equal to
">" Greater than
"<" Smaller than

Now try and use an if statement and a button to change the text displayed on screen

Posted Image

Possible solution:
[color=#FFFFFF]@name If (Tutorial == 1) {Example = 1}
@inputs Button
@outputs Output:string
@persist On:string Off:string

On = "The input is active"
Off = "The input is inactive"

if (Button == 1) {Output = On} else {Output = Off}[/color]

Congratulations! You have completed lesson 3!

Lesson 4
E2 and Life Support

AND and OR can be added to an if statement, to make the conditions more refined
An AND is usefull if you need an If within an If, while an OR is usefull if you need an If following the same If

if ((Button == 1) & (Active == 1)) {Screen = "Active"}

if ((Button == 1) | (Active == 1)) {Screen = "Active"}
As you can see, you will need a set of brackets around each condition, as well as around the set of conditions

For this lesson, the practice equipment can be found in your advanced duplicator, saved under /=Public Folder=/my_hat_stinks/Tutorials/E2/lesson 4

In this lesson, you have 3 objectives

The first is to wire both the button to the E2 so that it activates both the water pump and the H2O splitter

The second is to wire the storage cache so that when it has less than 10% oxygen or water, the appropriate light turns red
The lights should be green by defualt
Hint: RGB on the light is a vector, vectors make life simpler
Hint: You have both a max storage and current storage output from the storage cache

The third objective is to wire both the water pump and the H2O splitter into the E2 to work 200 times as fast
Hint: Use the "Multiplier" input on the generators

Possible Solution for the first task (Untested) :
[color=#FFFFFF]@name Supply Station
@inputs Button_In
@outputs Buton_Out

Button_Out = Button_In[/color]

Possible Solution for the second task (Untested) :
[color=#FFFFFF]@name Supply Station
@input Oxy_Current Oxy_Max Water_Current Water_Max
@outputs Oxy_Light:vector Water_Light:vector
@persist Oxy_Percent Water_Percent

#Calculating % stored of each resource
Oxy_Percent = ((Oxy_Current / Oxy_Max) * 100)
Water_Percent = ((Water_Current / Water_Max) * 100)

#Oxygen light colour. If smaller than 10%, shows red, otherwise green
if (Oxy_Percent < 10) {Oxy_Light = vec(255,0,0)} else {Oxy_Light = vec(0,255,0)}

#Water light colour. If smaller than 10%, shows red, otherwise green
if (Water_Percent < 10) {Water_Light = vec(255,0,0)} else {Water_Light = vec(0,255,0)}

Possible solution for the third task:
[color=#FFFFFF]@name Supply Station
@outputs Multiplier

Multiplier = 200[/color]

Lesson 5

If you have played spacebuild with me, chances are you have came accross one of my "Stalker" missiles.
These missiles stalk a target, often through the ground, and perform various actions
For example, one stalks for ever, one explodes when it reaches the target, and some explode in some way when the target retreats

These missiles can be designed quite easily using E2
Load the file =Public Files=/my_hat_stinks/tutorials/E2/Lesson5 <Not yet created>
This will supply enough equipment to start a stalker

Your task is to display the target name on the screen, and fly the stalker to the target only while the button is active

Extra variable data types you may need:
:entity Stores an entity

Data extraction you may need:
<Variable>:pos() Gives vector coordinates from the variable (entity) stored
<Variable>:name() Gives the name of the variable (entity) stored

Edited by my_hat_stinks, 24 February 2011 - 15:07.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users