[Tutorial] How to NOT code a bad script?
#1

How to NOT code a bad script?

Introduction:

Ok im going to make this simple and short, infact in steps to make it clearer.
Im making this tutorial to try show scripters out there at my stage or less or/and even people who dont know how to optimize yet how to script in proper efficency, i learnt the hard way, i created a massive script clearing 15k lines basically on all the knowledge i had from the past, the bad thing i did was use individual strings, default functions in mass amounts creating 1000s of practically useless lines, besides that my individual strings ranged from 64 cells to 128 cells and was averageing 40-60% CPU usage< lmao. What i should of been doing was using global variable, player variables. I basically coded 2000+ lines of a system in my script to 887 lines, using 16 variables rather then 100s to 1000s and stocks for typically used functions to break down lines. You want your script to run at the best efficency, 1 to keep your players happy with less lag and crashes and 2 to keep your host happy so you dont use massive amounts of CPU to run your bad script so they crash/restart your server.

Step 1 - What to code with?


The best thing to do is start from scratch and rebuild your script so it will determine global efficency.
Im not going to tell you what to use but the best command processors/functions are:

- zcmd (Fast command proccessor by ZeeX here)

- ycmd (Fast command proccessor by ****** here)

- sscanf (Epic plugin/function to make commands with fields on one line(ie. /command [field1] [field2] by ****** here)

And the best file saving plugins/includes are:

- my_sql (There is more then one release/creator ****** "samp mysql plugin")

- y_ini (By ****** here)

If you dont know pvars yet, learn it now, player variables are so handy and determains less lines and efficency. You can use them for everything from file values to new variables.

Step 2 - How to code a command efficently?

I could simply brag on how to code efficently but im going to just show you and im sure you will get it:

Exibit A: Bad command

pawn Код:
CMD:command(playerid, params[])//This is zcmd, use a fast proccessor = maximium efficency
{
    new string[64], pName[24];//We dont need to make strings under every command, Imagine your script with 200 commands. 100s of useless cells = more CPU usage
    GetPlayerName(playerid, pName, sizeof(pName));//This is a typical function, its used all the time, Why make another line for it? When we can make a stock and use the function in the strings = less lines/cells/less CPU usage.
    format(string, sizeof(string), "Your name is %s with id %d", pName, playerid);//This is also a typical fucntion, imagine how many times this is used? Why make individual strings for each fucntion? When we can make 1 for the future use of the function.
    SendClientMessage(playerid, 0x0, string);
    return true;
}
(Scroll right)

Exibit B: Efficency rebuild

pawn Код:
//top
new GlobalMessageString[100];//This is going to be our global message string for every "format" function(Can also be used again for every other use of the "format" function.

//Anywhere in script
stock GetName(playerid)//We can now use GetName(playerid) in our fomat function etc to get the players name
{
    new pName[24];//This way its typed once(BTW 24 cells is not too small type a 24 charactor name and tell me thats a name)
    GetPlayerName(playerid, pName, sizeof(pName));//This way this function is typed once.
    return pName;//return the players name.
}

//Rebuilt command
CMD:command(playerid, params[])
{
    format(GlobalMessageString, sizeof(GlobalMessageString), "Your name is %s with id %d", GetName(playerid), playerid);//We can use this same function with the strings again without redefining strings and adding more cell usage.
    SendClientMessage(playerid, 0x0, GlobalMessageString);
    return true;
}
(Scroll right)

Step 3 - How to code a saving system efficently?

Basically instead of using an enum for file values, use pvars(SetPVarInt/GetPVarInt), just call the variable the enum value and set it on login/register after you get your saved values, and then simply get the values when needed.
(If this dont make sense reply here and ill try make it clearer)

---[End of Tutorial]---

Feel free to post any comments/questions/step add-ons, i know i have not covered all areas but enough to optimize your script.

Tutorial by Godhimself

Reply


Messages In This Thread
How to NOT code a bad script? - by Godhimself - 03.08.2011, 08:50
Re: How to NOT code a bad script? - by Calgon - 03.08.2011, 09:18
Re: How to NOT code a bad script? - by Godhimself - 03.08.2011, 09:29
Re: How to NOT code a bad script? - by System64 - 03.08.2011, 10:04
Re: How to NOT code a bad script? - by Chrillzen - 05.08.2011, 03:10
Re: How to NOT code a bad script? - by Deskoft - 05.08.2011, 04:00
Re: How to NOT code a bad script? - by Godhimself - 05.08.2011, 04:05
Re: How to NOT code a bad script? - by Chrillzen - 05.08.2011, 04:26
Re: How to NOT code a bad script? - by dr.pepper - 05.08.2011, 04:32
Re: How to NOT code a bad script? - by Godhimself - 05.08.2011, 04:35

Forum Jump:


Users browsing this thread: 2 Guest(s)