[Tutorial] To optimize our GM and our Server improves.
#27

I've also found out to optimize command a bit.

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;
}
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:
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;
}
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.
Reply


Messages In This Thread
To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 05:37
Re: To optimize our GM and our Server improves. - by nGen.SoNNy - 23.12.2013, 06:14
Re: To optimize our GM and our Server improves. - by Emmet_ - 23.12.2013, 06:54
Re: To optimize our GM and our Server improves. - by Ada32 - 23.12.2013, 07:02
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 07:16
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 10:29
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 11:07
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 23.12.2013, 11:38
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 11:43
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 11:50
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 11:54
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 12:11
Re: To optimize our GM and our Server improves. - by Ada32 - 23.12.2013, 12:14
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 12:21
Re: To optimize our GM and our Server improves. - by Riddick94 - 23.12.2013, 12:25
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 12:27
Re: To optimize our GM and our Server improves. - by Riddick94 - 23.12.2013, 12:31
Re: To optimize our GM and our Server improves. - by newbienoob - 23.12.2013, 14:14
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 14:29
Re: To optimize our GM and our Server improves. - by newbienoob - 23.12.2013, 14:40
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 14:42
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 14:44
Re: To optimize our GM and our Server improves. - by SuperViper - 23.12.2013, 14:50
Re: To optimize our GM and our Server improves. - by xVIP3Rx - 23.12.2013, 14:56
Re: To optimize our GM and our Server improves. - by Konstantinos - 23.12.2013, 15:07
Re: To optimize our GM and our Server improves. - by Patrick - 23.12.2013, 15:12
Re: To optimize our GM and our Server improves. - by PowerPC603 - 23.12.2013, 16:03
Re: To optimize our GM and our Server improves. - by Bakr - 23.12.2013, 20:10
Re: To optimize our GM and our Server improves. - by Djole1337 - 23.12.2013, 20:32
Re: To optimize our GM and our Server improves. - by SuperViper - 23.12.2013, 22:35
Respuesta: To optimize our GM and our Server improves. - by Swedky - 24.12.2013, 18:09
Re: To optimize our GM and our Server improves. - by Patrick - 24.12.2013, 20:21
Respuesta: To optimize our GM and our Server improves. - by Swedky - 24.12.2013, 20:27
Re: To optimize our GM and our Server improves. - by Mister0 - 15.08.2016, 10:56
Respuesta: Re: To optimize our GM and our Server improves. - by Swedky - 17.10.2016, 07:18

Forum Jump:


Users browsing this thread: 11 Guest(s)