07.08.2011, 15:59
(
Последний раз редактировалось ylleron; 08.08.2011 в 17:01.
)
How to make a Tidy script
Hi, everyone.
Today i'm gonna learn you how to make a tidy and clean script. A lot of pawners (me too) have bad style of writting code. Big problem is in case, if you share this code as free. A lot of programmers have problem read your script. Anyway in future you will have to make tidy and clean script for any case.
At the begining i show you simple script, which can be modified to more easily.
pawn Код:
public OnPlayerSpawn(playerid)
{
new money = GetPlayerMoney(playerid);
if (money > 100000)
{
new str[50];
format(str, sizeof (str), "You are rich ( $%i )" money);
SendClientMessage(playerid, -1, str);
}
else
{
new str[50];
format(str, sizeof (str), "You have only $%i" money);
SendClientMessage(playerid, -1, str);
}
return 1;
}
So we can change it like that:
pawn Код:
public OnPlayerSpawn(playerid)
{
new money = GetPlayerMoney(playerid);
new str[50];
if (money > 100000)
{
format(str, sizeof (str), "You are rich ( $%i )", money);
}
else
{
format(str, sizeof (str), "You have only $%i", money);
}
SendClientMessage(playerid, -1, str);
return 1;
}
Second part of this lesson is styling the code.
A lot of people have alignment in code of some char and symbols.
pawn Код:
public OnPlayerSpawn(playerid)
{
new money = GetPlayerMoney(playerid), str[50];
if (money > 100000) format ( str, sizeof (str ), "You are rich ( $%i )", money );
else format ( str, sizeof (str), "You have only $%i", money );
SendClientMessage(playerid, -1, str);
return 1;
}
pawn Код:
public OnPlayerSpawn(playerid)
{
new money = GetPlayerMoney(playerid), str[50];
if (money > 100000)
{
format (str, sizeof (str ), "You are rich ( $%i )", money );
}
else
{
format (str, sizeof (str), "You have only ( $%i )", money );
}
SendClientMessage(playerid, -1, str);
return 1;
}
Third part of this lesson are variables.
Variables are unnecessary things in every programming language. In PAWN too. Variables can be named as you want. Classic vars are 'var[]'. Different are enums 'var[][enum]'. In every case you should make name of vars readable and it should express what it doing or what store in. Example you shouldn't use playerMoney and save players score in. Next part are lowercase and uppercase. A lot of expert programmer use 'Camel Method'. It is example 'playerInfo' or 'someVar' (second word have uppecase first char).
pawn Код:
new pInfo, playerInfo...
We all know what cycle is (for, while...). And we know that it uses varbiable too. Often is used i. It is basically and most used name of variable in cycle. But what we have to do if we have cycle in cycle? - Right names and sequence of variable names are i, j, k, l.
Example here:
pawn Код:
for (new i=0;i<MAX_PLAYERS;i++)
{
for (new j=0;j<MAX_TEAMS;i++)
{
// make code here
}
}
I am surprised that you've read this tutorial. Thank you for read.
In future i may add more informations.