Hey I am getting pretty good at E2 but my codes are very sloppy and hard to read. What are some good tips and tricks to clean up the code?
I know the ++ one but what about some others.
tab it.
tab?
oh
Other good coding practices include commenting your code and storing commonly used variables in the beginning of the chip.
Even something like #Hip stabilization helps a lot when it comes to explaining your code.
use tab indent and multiple lines, especially if you are using a lot of if statements (having brackets)
two good examples might be
[code]if (foo) {
bar = 1
}[/code]
[code]
if (foo)
{
bar = 1
}[/code]
and yes, put common variables at the beginning of the code. It is a good way to have them located at one spot, and you avoid the "variable does not exist" problem that might occur if you assign it after you call it. Unless you "group" the code by using #comments, then a logical hierarchy of variables and statements is also useful. Whatever suits you! :smile:
I sometimes use ### to divide areas of my code.
so some thing like
[code]
blah blah blah
###
yabba yabba yabba
[/code]
Store things like owner():lastsaid() in a veriable if calliing it multiple times.
There is now multiple line comments for E2
EDIT:
It was now made like Lua
#I made a single line comment!
/*
First line
Second Line
...
Um MOAR LINES!!!
*/
whitespace (Tab, empty lines spaces between arithmetic symbols/numbers) and comments == readable code.
Thanks how is this:
[code]@name Beam Turret beta.10
@inputs W A S D Mouse1 Active R Selector
@outputs Tright Tleft Weld H1 H2 Cannon1 Cannon2 Cannon3 Cannon4
@persist CannonSel
if( first() | duped() )
{Weld=0}
if(W){H1++ H2--}
if(S){H1-- H2++}
if(A){Tright=100}
else{Tright=0}
if(D){Tleft=-100}
else{Tleft=0}
if(A){Weld=0}
elseif(!D){Weld=1}
else{Weld=0}
if(Active & ~Active){hint("Press R to see instuctions",5)}
if(R & ~R){hint("Welcome,W and S are to move up and down",7)}
if(R & ~R){hint("A and D are to move Left and Right",7)}
if (Selector & ~Selector) {
if (CannonSel == 5) {# Why does this change after i pass 5 and not when it is 5?
CannonSel = 1 # If we're at cannon five, go to cannon 1.
}
else {
CannonSel++
}
if(CannonSel == 1) {
hint("Cannon " + (CannonSel),3)#if you want to name a specific cannon then write it like this "hint("Cannon name here",4)"
}
elseif(CannonSel == 2) {
hint("Cannon " + (CannonSel),3)#if you want to name a specific cannon then write it like this "hint("Cannon name here",4)"
}
elseif(CannonSel == 3) {
hint("Cannon " + (CannonSel),3)#if you want to name a specific cannon then write it like this "hint("Cannon name here",4)"
}
elseif(CannonSel == 4) {
hint("Cannon " + (CannonSel),3)#if you want to name a specific cannon then write it like this "hint("Cannon name here",4)"
}
elseif(CannonSel == 5) {
hint("All Cannons",3)
}
}
if(CannonSel == 1 & Mouse1){Cannon1=1 Cannon2=0 Cannon3=0 Cannon4=0
}
elseif(CannonSel == 2 & Mouse1){Cannon1=0 Cannon2=1 Cannon3=0 cannon4=0
}
elseif(CannonSel == 3 & Mouse1){Cannon1=0 Cannon2=0 Cannon3=1 Cannon4=0
}
elseif(CannonSel == 4 & Mouse1){Cannon1=0 Cannon2=0 Cannon3=0 Cannon4=1
}
elseif(CannonSel == 5 & Mouse1){Cannon1=1 Cannon2=1 Cannon3=1 Cannon4=1
}
else{Cannon1=0 Cannon2=0 Cannon3=0 Cannon4=0
}[/code]
looks good.
OK thanks guys.
Still a little nasty but it's definitely readable.
It really comes down to personal preference as to how you structure, find a way that looks good for you, but you've got the basics figured.
My only suggestions are if you have an if statement that's just declaring variable data, like your if (first()) line, you can just put that all on the one line and that'll do too. Same goes with a single small function to. Say you have a nested If statement like if (A = B) {if (C = D) {print("Awesome!")}} That'll do just as well.
I like to struct my code like this:
[lua]
#################################################################################################
# "findinfo" function
if (Var2 == "entinfo") {
PointEnt = owner():aimEntity()
if (PointEnt) {
if (PointEnt:isPlayer()) {
hint("Use \"playerinfo\" for players!",10)
}
else {
hint("Type: "+PointEnt:type(),10)
hint("R: "+PointEnt:getColor():x():toString(),10)
hint("G: "+PointEnt:getColor():y():toString(),10)
hint("B: "+PointEnt:getColor():z():toString(),10)
hint("Owned by: "+PointEnt:owner():name(),10)
hint("Ent ID: "+PointEnt:id():toString(),10)
hint("Model: "+PointEnt:model(),10)
}
}
else {hint("You can't use this on the world, silly.",7)}
}
#################################################################################################
# "playerinfo" function
if (Var2 == "playerinfo") {
if (Var3 != "") {
Player = findPlayerByName(Var3)
if (Player) {
if(Player:isAdmin()==1){IsAdmin="Yes"}
else {IsAdmin="No"}
if(Player:isSuperAdmin()==1){IsSuperAdmin="Yes"}
else {IsSuperAdmin="No"}
hint("Name: "+Player:name(),10)
hint("SteamID: "+Player:steamID(),10)
hint("EntID: "+Player:id():toString(),10)
hint("Ping: "+Player:ping():toString(),10)
hint("Model: "+Player:model(),10)
hint("Is an Admin?: "+IsAdmin,10)
hint("Is a SuperAdmin?: "+IsSuperAdmin,10)
}
else {hint("Name was not found.",10)}
}
else {hint("That is not a Player! Use \"entinfo\"!",10)}
}
#################################################################################################
[/lua]
Makes for easy seperation.