[Tutorial] Bad scripting habits - What not to do in a script
#7

Some other bad habits
  • Wasting cells
  • Using fixed numbers in loops
    • Use sizeof array
    • Or Macros, so you only need to change one number instead of 20
  • The function PlayerName which creates each call an array with MAX_PLAYER_NAME (24) cells
    • New scripter tend to use the function each line (see some dudb codes)
    • Just save the name in an array or just use from the beginning GetPlayerName
  • Big numbers as decimal, use hex code (2139095040 = 0x7F800000)
  • Using format to often, its only useful if you insert more parameter
pawn Code:
public OnPlayerSpawn(playerid)
{  //Noone needs weapon at OnPlayerConnect :)
    new //declare the array with the text (9 cells long), no need for a function
        string[9 + MAX_PLAYER_NAME] = "Accounts\\";
    //GetPlayerName instead of PlayerName function
    GetPlayerName(playerid, string[9], MAX_PLAYER_NAME); //we insert the name at the 9 cell
    strcat(string, ".ini"); //and add at the end ".ini", strcat is much faster than format
    for(new i, tmp[16]; ; ++i) //infinite loop
    {
        tmp = "Weapon_"; //set tmp as "Weapon_" (7 cells long)
        valstr(tmp[7], i, false); //and add the number at the end
        tmp[0] = dini_Int(string, tmp); //save the weapon in a variable (because dini_Init is slow)
        if(tmp[0] == 0) //break the loop if no more weapon was found
        {
            break;
        }
        GivePlayerWeapon(playerid, tmp[0], 0x7F800000); //give the player the weapon
    }
    return 1;
}
The unnecessary brackets arent a bad habit, everyone has its own indention style

Nice tutorial in the end
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 4 Guest(s)