COMMAND:tele(playerid, params[])
{
// Setup local variables
new Player1, Player2, Float:x, Float:y, Float:z, PortMsg[128], IntID, WorldID, Name[24], AdminName[24];
// Send the command to all admins so they can see it
SendAdminText(playerid, "/tele", 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)
{
if (sscanf(params, "uu", Player1, Player2)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /tele [PlayerToPort] [TargetPlayer]");
else
{
// Check if player1 is online
if (APlayerData[Player1][LoggedIn] == true)
{
// Check if player2 is online
if (APlayerData[Player2][LoggedIn] == true)
{
// Get the name of the admin and the second player
GetPlayerName(playerid, AdminName, sizeof(AdminName));
GetPlayerName(Player2, Name, sizeof(Name));
// Get the location of the second player
GetPlayerPos(Player2, x, y, z);
IntID = GetPlayerInterior(Player2);
WorldID = GetPlayerVirtualWorld(Player2);
// Port the first player to player2's location
SetPlayerVirtualWorld(Player1, WorldID);
SetPlayerInterior(Player1, IntID);
SetPlayerPos(Player1, x, y, z + 3.0);
// Let the first player know he's been ported
format(PortMsg, 128, "You have been ported to player %s by %s", Name, AdminName);
SendClientMessage(Player1, 0xFFFFFFFF, PortMsg);
}
else
SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player2 isn't online");
}
else
SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player1 isn't online");
}
}
else
return 0;
}
else
return 0;
// Let the server know that this was a valid command
return 1;
}
[debug] Run time error 4: "Array index out of bounds" [debug] Accessing element at index 65535 past array upper bound 49 [debug] AMX backtrace: [debug] #0 00058e50 in public cmd_tele () from PPC_Trucking.amx [debug] #1 native CallLocalFunction () [00471e90] from samp-server.exe [debug] #2 00000770 in public OnPlayerCommandText () from PPC_Trucking.amx
COMMAND:tele(playerid, params[])
{
if (!APlayerData[playerid][LoggedIn] || !APlayerData[playerid][PlayerLevel]) return 0;
new Player1, Player2;
if(sscanf(params, "rr", Player1, Player2)) {
//If you want the UNKNOWN COMMAND thing, remove this return and put return 0; under this sendclientmessage
return SendClientMessage(playerid, 0xFF0000AA, "Usage: /tele [PlayerToPort] [TargetPlayer]");
}
if(Player1 == INVALID_PLAYER_ID || Player2 == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /tele [PlayerToPort] [TargetPlayer]");
if (!APlayerData[Player1][LoggedIn]) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player1 isn't logged in.");
if (!APlayerData[Player2][LoggedIn]) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player2 isn't logged in.");
//Create some variables only if the other players are logged in
new Float:x, Float:y, Float:z,
PortMsg[128], Name[MAX_PLAYER_NAME], AdminName[MAX_PLAYER_NAME];
GetPlayerName(playerid, AdminName, sizeof(AdminName));
GetPlayerName(Player2, Name, sizeof(Name));
GetPlayerPos(Player2, x, y, z);
SetPlayerVirtualWorld(Player1, GetPlayerVirtualWorld(Player2));
SetPlayerInterior(Player1, GetPlayerInterior(Player2));
SetPlayerPos(Player1, x, y, z + 3.0);
format(PortMsg, 128, "You have been ported to player %s by %s", Name, AdminName);
SendClientMessage(Player1, 0xFFFFFFFF, PortMsg);
SendAdminText(playerid, "/tele", params); //No point in this being at the top...If the command isn't successful why tell everyone?
return 1;
}
|
It's more a matter of taste, I'd prefer to avoid using returns that are unneeded if you structure it correctly the code would just go to the end of your scope and return anyways also to me code looks more elegant when it has a visual flow through indentation than a series of single tabbed lines which is just annoying to read.
|
COMMAND:tele(playerid, params[])
{
if (APlayerData[playerid][LoggedIn] && APlayerData[playerid][PlayerLevel]) {
new Player1, Player2;
if(sscanf(params, "rr", Player1, Player2)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /tele [PlayerToPort] [TargetPlayer]");
else if(Player1 == INVALID_PLAYER_ID || Player2 == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Usage: /tele [PlayerToPort] [TargetPlayer]");
//If it were me I would have these as 1 and make it just say "One of the players isn't connected." or somethin
else if (!APlayerData[Player1][LoggedIn]) SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player1 isn't logged in.");
else if (!APlayerData[Player2][LoggedIn]) SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player2 isn't logged in.");
else {
new Float:x, Float:y, Float:z,
PortMsg[128], Name[MAX_PLAYER_NAME], AdminName[MAX_PLAYER_NAME];
GetPlayerName(playerid, AdminName, sizeof(AdminName));
GetPlayerName(Player2, Name, sizeof(Name));
GetPlayerPos(Player2, x, y, z);
SetPlayerVirtualWorld(Player1, GetPlayerVirtualWorld(Player2));
SetPlayerInterior(Player1, GetPlayerInterior(Player2));
SetPlayerPos(Player1, x, y, z + 3.0);
format(PortMsg, 128, "You have been ported to player %s by %s", Name, AdminName);
SendClientMessage(Player1, 0xFFFFFFFF, PortMsg);
SendAdminText(playerid, "/tele", params);
}
}
else return 0; //or a message saying they aren't logged in
return 1;
}
... I'd really like to read what ****** has to say about it though.
CMD:something(playerid, params[])
{
if()
return x;
if()
return x
if()
return x
// process the actual command
return 1;
}
CMD:something(playerid, params[])
{
if()
{
if()
{
if()
{
// process the actual command
}
else // do something
}
else // do something
}
else // do something
return 1;
}