Blocking Command
#1

Something is blocking my command.


Ports one player to another player
Код:
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
Код:
[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
How i fix it?
it work like

/tele 0 0
Reply
#2

pawn Код:
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;
}
Seems like an invalid player issue to me... You can try this out. I ripped out your comments..Most of them aren't needed as it's kind of self explanatory for most of the things (you can re-add them if you want).
Reply
#3

Thanks
Reply
#4

You get errors because all you do is copy and paste things from a script somewhere and put them into yours. You REALLY need to be fucking banned already. You have 747 posts of utter nonsense and no attempt to do anything for yourself.

And, for the last fucking time. It isn't "dis", it's "this!" Stop talking like a god damned 5 year old!
Reply
#5

Those multiple return; statements make me cringe Antonio!
Reply
#6

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Those multiple return; statements make me cringe Antonio!
What's the difference between returning 1 where it is, and returning 1 at the end of the command?
Reply
#7

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.
Reply
#8

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
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.
Ehh, Idk, I just prefer doing it the way I did it there :P

pawn Код:
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;
}
Would be basically the same
Reply
#9

Ya, it would be the same basically, that way looks better to me at any rate now just to get one thing straight I'm not saying not to use multiple returns but avoid using them when not needed that's all ... I'd really like to read what ****** has to say about it though.
Reply
#10

My community's lead developer ran some speed tests on the two command structure's below:

pawn Код:
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;
}
The second one is faster, however it's a minor speed increase. Nevertheless, I like the first command structure anyways- so that's basically the one I use.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)