23.12.2013, 16:03
I've also found out to optimize command a bit.
This my old /fuel command:
You can see that this code can go pretty far to the right if even more if-statements are under it.
And it's sometimes hard to see where a code-block ends if there are many instructions in a block.
I've optimized it to this:
Here you can see that any condition that would fail the command is on top of the command.
And the rest of the code doesn't need to be nested in alot of underlaying if-statements.
Also the new command has more functionality in it but has become shorter as well by this new approach.
This my old /fuel command:
Code:
// Refuel the player's vehicle COMMAND:fuel(playerid, params[]) { // Setup local variables new vID; // Send the command to all admins so they can see it SendAdminText(playerid, "/fuel", params); // Check if the player has logged in if (APlayerData[playerid][LoggedIn] == true) { // Check if the player's admin-level is at least 1 if (APlayerData[playerid][PlayerLevel] >= 1) { // Check if the player is inside a vehicle if (IsPlayerInAnyVehicle(playerid)) { // Get the vehicleid vID = GetPlayerVehicleID(playerid); // Refuel the vehicle AVehicleData[vID][Fuel] = MaxFuel; // Let the player know about it SendClientMessage(playerid, 0x00FF00FF, "Your vehicle is refuelled"); } else SendClientMessage(playerid, 0x00FF00FF, "You're not driving a vehicle"); } else return 0; } else return 0; // Let the server know that this was a valid command return 1; }
And it's sometimes hard to see where a code-block ends if there are many instructions in a block.
I've optimized it to this:
Code:
// This command allows you to refuel your vehicle for free (admins only) COMMAND:fuel(playerid, params[]) { // If a player hasn't logged in properly, he cannot use this command if (APlayerData[playerid][LoggedIn] == false) return 0; // If the player has an insufficient admin-level (he needs level 1), exit the command if (APlayerData[playerid][AdminLevel] < 1) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Only admins level 1 can use this command"); // If the player is on the class-selection menu, block the command if ((GetPlayerState(playerid) == PLAYER_STATE_WASTED) || (GetPlayerState(playerid) == PLAYER_STATE_NONE)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}You cannot use this command while using class-selection"); // Exit the command if the player is not the driver of a vehicle if (GetPlayerVehicleSeat(playerid) != 0) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}You're not driving a vehicle"); // Setup local variables new vid, engine, lights, alarm, doors, bonnet, boot, objective; // Get the vehicleid vid = GetPlayerVehicleID(playerid); // Refuel the vehicle AVehicleData[vid][Fuel] = MaxFuel; // Also (re-)start the engine and turn on the lights in case the vehicle was completely out of fuel GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective); SetVehicleParamsEx(vid, 1, 1, alarm, doors, bonnet, boot, objective); // Let the player know about it SendClientMessage(playerid, 0xFFFFFFFF, "{00FF00}Your vehicle is refuelled"); // Let the server know that this was a valid command return 1; }
And the rest of the code doesn't need to be nested in alot of underlaying if-statements.
Also the new command has more functionality in it but has become shorter as well by this new approach.