April 07, 2020, 12:08 am

Author Topic: [Tutorial] Functions in E2!  (Read 10292 times)

Offline Ginny Weasley

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 612
  • I'm not here. Sod off.
    • View Profile
[Tutorial] Functions in E2!
« on: June 25, 2013, 09:28 pm »
OK, so I quite often see coders (of all levels of experience) using repeated bits of code which just clog up their E2. The solution? Functions.
I'll not waste time patronising you as far as possible.
If you've done any kind of coding before, you'll probably know what a function is, so I'll skip that out

Ok, so an E2 function looks something like this:
Code: [Select]
function normal isCP(D:entity){
    if(teamName(D:team()):find("SWAT") | teamName(D:team()):find("Police") |  teamName(D:team()):find("Mayor") ){
        return 1
    }
    else{
        return 0   
    }
}

So then, how to create one. Well, we start, obvious, with the keyword 'function', to tell the chip we're creating one (duh...).
Code: [Select]
function NORMALNext is one of the most important parts. The next word (here 'normal') tells the E2 what data type will be returned by the function. Any data type can in theory be returned from a function, but it's most likely to be a 'normal', a 'number' or 'void'. Void is a keyword which simply means nothing will be returned form the function.
Code: [Select]
function normal ISCPNext up, we have the name of the function. It can be pretty much anything, provided it doesn't already exist in the standard E2 functions. I've named this one isCP, because it tells me whether a player is Civil Protection or not. It's generally a good idea to name a function for what you want it do, to avoid confusion, but you don't have to.
Code: [Select]
function normal isCP(D:ENTITY)The code between the brackets is the arguments of the function. If you've ever done functions in other coding languages, you'll understand how this works. If not, then put simply, a function's arguments are the pieces of information that need to be input. As with most compiled languages (C++, VB.NET etc), the arguments need to be assigned a specific data type. Here, I've used D:entity. This means my argument is an entity, which I can then refer to generally as D. When the function is called, any mention of D within it will be replaced with the entity I give it as an argument.
Code: [Select]
function normal isCP(D:entity){
  --snippity snip--
}
Ok yes, I cut it out. The code in between the braces {} is the re-usable code we want our function to do. Each time we call (use) the function, the 'D's will be exchanged for the entity given, the code will be evaluated, and an outcome reached. Note that the exchanging isn't permanent - we can keep re-using the same function as many times as we like with different entiies
In this function, and indeed any function without the return type 'void', the important line is 'return'. This tells the function what to output. Once an output has been returned, the function will cancel - so having two consecutive returns is pointless. Most returns in functions will depend on if statements - depending on the circumstances, either a 1 (true) or 0 (false) will be returned.
If you try to return something that doesn't match your specified return type (for example returning "<string>" to a 'number' function), this will create an error that will stop your code from running once that function executes.


LAST THING: CALLING THE FUNCTION
Code: [Select]
Driver = Ranger:entity
if(isCP(Driver)){
   #blah blah blah
}
This is an example of calling our isCP function. As you can see, we write the function name with brackets after it, containing our argument - in this case, an entity found via a ranger. This particular example was from a speed camera, which we didn't want to try and /wanted cops. If our player satisfies our isCP function (i.e. he's a cop), then the function returns 1, and the if statement will execute.


I wrote this in about 10 minutes, so please, feel free to give criticism/additional things as you please.
I'm hopefully gonna add this to a big E2 tutorial I'm writing for new supporters, to help them get started with E2. So yeah, lemme know what you think.

All insults & praise gratefully accepted.

Offline Freddie

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 816
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #1 on: June 25, 2013, 10:23 pm »
This was made because I didn't use functions in my speed cera code wasn't it :)

Thanks for the help though, I will add this to my E2's later.

Offline Moosaphine

  • Root of all Evil
  • ByB Champion
  • *
  • Posts: 3145
  • I am amazing xo
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #2 on: June 26, 2013, 12:07 am »
Nice tutorial,

Made it easy to understand, going to play around with functions a lot more now;

Advice :

Maybe include a bit of an example return etc. and then include if(!functionTitle) etc. Just so we can get an idea of how to use it afterwards :)
Jamie : Oli we are so having a LAN party soon
... wait, Oli can you get up stairs?
*Facepalm*


Offline Racr1425

Re: [Tutorial] Functions in E2!
« Reply #3 on: June 26, 2013, 12:31 am »
Isn't your example kind of pointless? You can just use this:
Code: [Select]
Entity:isAuthorisedPersonnel() On ByB

Offline StupidWeasel

  • Supporter
  • ByB Champion
  • *
  • Posts: 2646
    • View Profile
    • Moustaches through the ages: A Historial Account of Facial Hair
Re: [Tutorial] Functions in E2!
« Reply #4 on: June 26, 2013, 12:57 am »
Isn't your example kind of pointless? You can just use this:
Code: [Select]
Entity:isAuthorisedPersonnel() On ByB
That inbuilt/byb function was broken for a quite some time.

FunnyClaps

Offline Spoofzor

  • ByB Fanatic
  • ****
  • Posts: 902
  • What do you mean that's as loud as it can go!?!
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #5 on: June 26, 2013, 02:30 am »
Another way you can use inputs is as follows:

Code: [Select]
function number entity:isCP(){

}

Just as before number is the type of data being returned.

In my example we are using entity:isCP() as oppsed to isCP(D:entity)
How you use this is as follows:

Code: [Select]
function number entity:isCP(){
    if(This:team():teamName():find("SWAT") | This:team():teamName():find("Police"){
        return 1
    }else{
        return 0
    }
}

You use the variable This to call the data in this case an entity.
The same method also works for arrays and tables iirc.

Code: [Select]
function number array:isCool(){
    for(I=1,This:count()){
        if(This[I,entity]:isCP()){
            return 1
        }else{
            return 0
        }
    }
}

Idk. Just some example of how you would do something like Player:isCP()

Offline Ginny Weasley

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 612
  • I'm not here. Sod off.
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #6 on: June 26, 2013, 05:30 am »
Damn you Spoof, knew I'd forgot something!
Although, I'm pretty sure isCool() was a Lexi function for finding people on his friends list XD

Isn't your example kind of pointless?
That's why it was just an example, and I totally didn't copy from a piece of code I made a few days ago... But then, like Weasel said, it's been broken pretty much forever.

Offline Moosaphine

  • Root of all Evil
  • ByB Champion
  • *
  • Posts: 3145
  • I am amazing xo
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #7 on: June 26, 2013, 10:58 am »
Spoofy

When you use

This:team()

Would I have to put This:entity in persists?

Can someone do a similar tutorial on arrays please? and loops? Ill love you forever, the wiki ones arent that good.
Jamie : Oli we are so having a LAN party soon
... wait, Oli can you get up stairs?
*Facepalm*


Offline Ginny Weasley

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 612
  • I'm not here. Sod off.
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #8 on: June 26, 2013, 02:58 pm »
Can someone do a similar tutorial on arrays please? and loops? Ill love you forever, the wiki ones arent that good.
Can someone do this? As I said, I'm trying to create a generic tutorial for new E2ers, which is quite hard to do when I'm not perfect at it myself. If anyone wants to help, that'd be great.

Offline Kgwilson

  • Admin
  • ByB Fanatic
  • *
  • Posts: 383
  • who am i again?
    • View Profile
Re: Re: [Tutorial] Functions in E2!
« Reply #9 on: June 26, 2013, 04:18 pm »
Spoofy

When you use

This:team()

Would I have to put This:entity in persists?

Can someone do a similar tutorial on arrays please? and loops? Ill love you forever, the wiki ones arent that good.
bear with me this is all from memory:
arrays are a variable that can hold multiple entries of most datatypes (tables, arrays cannot be put into arrays) and indexed by number and type within the variable
for example
Code: [Select]
Words=array("hello","there","moo sucks")
the way e2 would index the Words array would be: Words[1,string] would be "hello", Words[2,string] would return "there", etc
[1,string] means the first entry of the array, and the data type we're attempting to get is a string
that being said..
for loops work like this:
Code: [Select]
for(I=1,5) { print(I) }
this would print like this:
1
2
3
4
5
For loops run the code a specified amount of times within 1 op
the syntax is as follows: for(Variable=1,5) {
Variable is the nickname for the number that changes each calculation, 1,5 means the numbers begin at 1, and end at 5
so for something as such, if we have an array named Players with 3 players as entity data types, we know you'd grab the first players name by using Players[1,entity]:name()
Instead of writing lik...
Code: [Select]
print(Players[1,entity]:name()+" "+Players[2,entity]:name()+" "+Players[3,entity]:name())
As you see the only number that changes within these 3 players names inside the array would be 1,2, and 3
you could plug this equation into a for loop as such:
Code: [Select]
if(first()) {
Players=array(owner(),findPlayerByName("spoof"))
}
for(I=1,Players:count()) { #Beginning at 1, and ending at the amount of entries in Player, incrementing I each execution, do:
print(Players[I,entity]:name())
}
This would print your name, and Spoofzor's name in chat (provided spoof is online), because within 1 op, the E2 executes print(Players[1,entity]:name()) and Players[2,entity]:name() within one loop
... Sorry if im bad at explaining so heres something id use for a autogunshop (excuse my code im out of game)
Code: [Select]
@inputs [R1 R2 R3 R4 R5]:entity ConsoleScreen:wirelink
@persist Profit
if(first()|duped()) { Profit=100 }
Guns=array(R1,R2,R3,R4,R5)
for(I=1,Guns:count()) {
     ConsoleScreen:writeString(Guns[I,entity]:shipmentName(),0,I,999,0)
     ConsoleScreen:writeString(""+(Guns[I,entity]:shipmentIndividualCost()+Profit),20,I,999,0)
}
#Writes each entry's shipment name in Guns array on line I (meaning Array[1,entity] is on line 1, Array[2,entity] is line 2, et cetera, instead of using 5 lines, 1 for each ranger, you can write all the lines + prices within 1 loop and 2 lines of code

« Last Edit: June 26, 2013, 04:24 pm by Kgwilson »

Offline Moosaphine

  • Root of all Evil
  • ByB Champion
  • *
  • Posts: 3145
  • I am amazing xo
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #10 on: June 26, 2013, 06:06 pm »
KG FOR MAYOR

Lol no.

Nice tut :) stickying thread and gonna play around with this sheet.

/stickied
Jamie : Oli we are so having a LAN party soon
... wait, Oli can you get up stairs?
*Facepalm*


Offline Ginny Weasley

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 612
  • I'm not here. Sod off.
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #11 on: June 26, 2013, 06:10 pm »
Hehe, I've started writing my tutorial on an E2 chip for my own benefit (I like the formatting etc)
Currently on line 234, only 53 of which are code, and I'm just finishing while loops.

I might transfer it to a forum post, but I'm not sure about the length limit.
Anyway, back to GMod. This smeg isn't gonna write itself...

Hehe, stickied :) Never had that happen before. If any supers/roots want to edit my original post to add in the bits I missed, go ahead. Makes it easier for everyone.

Offline Jelman

  • Jelmani
  • ByB Champion
  • *
  • Posts: 1679
    • View Profile
Re: Re: [Tutorial] Functions in E2!
« Reply #12 on: June 26, 2013, 06:18 pm »
KG FOR MAYOR

Lol no.

Nice tut :) stickying thread and gonna play around with this sheet.

/stickied

MOO FOR MAYOR OF LONDON.






His Grace Snoop of House Dogg, first in his name; Kushleesi; The Unblazed; Brother of Green Dragon Sativas; embarks on a quest to reclaim Weedsteros

Offline Ginny Weasley

  • Supporter
  • ByB Fanatic
  • *
  • Posts: 612
  • I'm not here. Sod off.
    • View Profile
Re: [Tutorial] Functions in E2!
« Reply #13 on: June 26, 2013, 07:05 pm »
No-one can ever replace Boris.

Offline Jelman

  • Jelmani
  • ByB Champion
  • *
  • Posts: 1679
    • View Profile
Re: Re: [Tutorial] Functions in E2!
« Reply #14 on: June 26, 2013, 07:31 pm »
No-one can ever replace Boris.

Off topic: We were having a conversation about the entire mayor thing between me, nightmare and moo since we all live in london.

On topic: Love this thread, very helpful for dem n00bs and 500 op e2 doors.






His Grace Snoop of House Dogg, first in his name; Kushleesi; The Unblazed; Brother of Green Dragon Sativas; embarks on a quest to reclaim Weedsteros