Your preferred scripting methods
#1

Your preferred scripting methods

Sup?

So anyways, it's that time of year again where I start developing my own gamemode (last year was a bust). Now I need opinions and I want you guys to tell me your preferred methods for scripting PAWN.

This is very similar to the coding style research thread from a while back, but this one is more advanced because I'm seeking more ways to do stuff.

1) Numerated lists

Code:
a)

#define DIALOG_PIZZA 1356
#define DIALOG_CARS 1483
#define DIALOG_GUNS 1753
#define DIALOG_STORE 1533


b)

enum
{
    DIALOG_PIZZA = 1356,
    DIALOG_CARS = 1483,
    DIALOG_GUNS = 1753,
    DIALOG_STORE = 1533
};
2) Player arrays

Code:
a)

new Money[MAX_PLAYERS];
new Score[MAX_PLAYERS];
new JailTime[MAX_PLAYERS];
new LoggedIn[MAX_PLAYERS];

b)

enum ePlayerInfo
{
    Money,
    Score,
    JailTime,
    LoggedIn
}
new PlayerInfo[MAX_PLAYERS][ePlayerInfo];
3) Functions

Code:
a)

stock GiveMoney(playerid, amount)
{
    Money[playerid] += amount;
    return 1;
}

b)

GiveMoney(playerid, amount)
{
    Money[playerid] += amount;
}

c)

forward GiveMoney(playerid, amount);
public GiveMoney(playerid, amount)
{
    Money[playerid] += amount;
}
4) Commands

Code:
a)

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp(cmdtext, "/help", true))
    {
        SendClientMessage(playerid, -1, "My help command.");
        return 1;
    }
    return 0;
}

b)

CMD:help(playerid, params[])
{
    SendClientMessage(playerid, -1, "My help command.");
    return 1;
}

c)

public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(help, playerid, 4);
    return 0;
}

dcmd_help(playerid, params[])
{
    #pragma unused params
    SendClientMessage(playerid, -1, "My help command.");
    return 1;
}
5) Single statement functions

Code:
a)

stock IsPlayerLogged(playerid)
{
    return LoggedIn[playerid];
}

b)

stock IsPlayerLogged(playerid) return LoggedIn[playerid];

c)

#define IsPlayerLogged(%0) (LoggedIn[(%0)])
6) Conditions

Code:
a)

if (a)
{
    b = a + 1;
}
else
{
    b = a + 2;
}

b)

if (a) b = a + 1;
else b = a + 2;

c)

b = (a) ? (a + 1) : (a + 2);
7) Loops

Code:
a)

for (new i = 0; i < MAX_ITEMS; i ++)
{
    // ...
}

b)

new i;

while (i < MAX_ITEMS)
{
    // ...
    i++;
}
8) Variable grouping

Code:
a)

new a, b, c;

b)

new
    a,
    b,
    c;

c)

new a;
new b;
new c;
9) Multiple conditions

Code:
a)

if (something == 1)
{
    ...
}
else if (something == 2)
{
    ...
}
else if (something == 3)
{
    ...
}

b)

switch (something)
{
    case 1:
    {
        ...
    }
    case 2:
    {
        ...
    }
    case 3:
    {
        ...
    }
}

c)

if (something == 1) ...
else if (something == 2) ...
else if (something == 3) ...
10) Data organization

Code:
a)

stock Spawn(playerid)
{
    if (gTeam[playerid] == 1)
    {
        SetPlayerPos(playerid, 0.0, 1.0, 2.0);
    }
    else if (gTeam[playerid] == 2)
    {
        SetPlayerPos(playerid, 5.0, 10.0, 15.0);
    }
    else if (gTeam[playerid] == 3)
    {
        SetPlayerPos(playerid, 25.0, 10.0, 155.0);
    }
}

b)

static const Float:g_SpawnArray[3][4] =
{
    {0.0, 1.0, 2.0},
    {5.0, 10.0, 15.0},
    {25.0, 10.0, 155.0}
};

stock Spawn(playerid)
{
    SetPlayerPos(playerid, g_SpawnArray[gTeam[playerid] - 1][0], g_SpawnArray[gTeam[playerid] - 1][1], g_SpawnArray[gTeam[playerid] - 1][2]);
}
11) Formatting strings

Code:
a)

new string[128];

format(string, sizeof(string), "Emmet is");
format(string, sizeof(string), "%s a boss.", string);

b)

new string[128];

strcat(string, "Emmet is");
strcat(string, " a boss");
12) Copying a string

Code:
a)

new str[128];

format(str, sizeof(str), "%s", string);

b)

new str[128];

strcat(str, string, sizeof(str));

c)

new str[128];

memcpy(str, string, 0, 512, sizeof(str));
13) Clearing or deleting a string

Code:
a)

string[0] = '\0';

b)

strdel(string, 0, strlen(string));

c)

for (new i = 0, length = strlen(string); i < length; i ++)
{
    string[i] = 0;
}
14) Prefixing

Code:
a)

new score, string[128];

b)

new iScore, szString[128];
Thanks!
Reply


Messages In This Thread
Your preferred scripting methods - by Emmet_ - 03.12.2013, 04:22
Re: Your preferred scripting methods - by Djole1337 - 03.12.2013, 05:15
Re: Your preferred scripting methods - by Bakr - 03.12.2013, 05:17
Re: Your preferred scripting methods - by Kyosaur - 03.12.2013, 05:27
Re: Your preferred scripting methods - by Pottus - 03.12.2013, 06:02
Re: Your preferred scripting methods - by Slice - 03.12.2013, 06:47
Re: Your preferred scripting methods - by iZN - 03.12.2013, 07:02
Re: Your preferred scripting methods - by Emmet_ - 03.12.2013, 08:04
Re: Your preferred scripting methods - by Slice - 03.12.2013, 09:47
Re: Your preferred scripting methods - by Kyle - 03.12.2013, 13:28

Forum Jump:


Users browsing this thread: 3 Guest(s)