Multiple command inside one dcmd cmd?
#1

Код:
dcmd_house(playerid, params[])
{
	new
		command[128], owner[255];
	if(Account[playerid][pAdminLevel] >= 20)
	{
	  if (sscanf(params, "s", command)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /house <add>");
	  else
	  {
	    if(strcmp(command,"add", true ) == 0)
	    {
				if (sscanf(command, "s", owner)) SendClientMessage(playerid, 0xFF0000AA, "Usage: /house add <owner>");
				else
				{
					new Float:x,Float:y,Float:z,Float:angle;
					GetPlayerPos(playerid,x,y,z);
					GetPlayerFacingAngle(playerid,angle);
					AddHouse(owner,x,y,z,GetPlayerVirtualWorld(playerid),GetPlayerInterior(playerid),angle,0/*exitx*/,0/*exity*/,0/*exitz*/,1337/*exitinterior*/,0/*exitangle*/,0/*owned*/,0/*rentable*/,0/*rentcost*/,0/*houseprice*/,0/*locked*/);
				}
			}
			else
			{
			  return SendClientMessage(playerid, 0xFF0000AA, "Invalid house command.");
			}
	  }
	}
	return 1;
}
This is my command, i want to have multiple commands inside of one but i haven't coded in months and i'm a little rusty.

I can see where the problem is but i'm not sure how to solve it, i posted this ages ago and someone gave me a wonderful result which did the trick but i formatted and lost my script.
Reply
#2

pawn Код:
sscanf(command, "s", owner)
Remember that this is splitting it by spaces for the parameters, this will not work because its expecting 1 parameter, but it actually needs two. Example to fix:

pawn Код:
sscanf(command,"ss",command,owner)
Make sense now?
Reply
#3

Quote:
Originally Posted by JaTochNietDan
pawn Код:
sscanf(command, "s", owner)
Remember that this is splitting it by spaces for the parameters, this will not work because its expecting 1 parameter, but it actually needs two. Example to fix:

pawn Код:
sscanf(command,"ss",command,owner)
Make sense now?
I know that, i tried that before but now when i type say /house add JaTochNietDan.

I get no response from the command.

Thanks anyway for the fast response.
Reply
#4

Actually my code makes no sense either. I appear to have misread your original code.

What exactly does it do wrong with the original code you posted?
Reply
#5

Quote:
Originally Posted by JaTochNietDan
Actually my code makes no sense either. I appear to have misread your original code.

What exactly does it do wrong with the original code you posted?
/house add JaTochNietDan, Owners name in file = add because its reading the second line and not the 3rd which is my fault but still i tried your method before as it seemed logical earlier on today with no good results
Reply
#6

Actually try:

pawn Код:
sscanf(params,"ss",command,owner)
Reply
#7

Quote:
Originally Posted by JaTochNietDan
Actually try:

pawn Код:
sscanf(params,"ss",command,owner)
I tried that aswell, same as the other post command doesn't reply.
Reply
#8

Ok the issue is caused by the string itself. sscanf doesn't split the last part, it continues the string. So when you're typing /house add JaTochNietDan, the "command" string therefore becomes add JaTochNietDan, which strcmp will not match with "add", how can you fix it? I haven't found a way around it in sscanf.

I've used this in some of my other commands before however I've stripped the parameters into the "command" string like you've done using strtok instead of sscanf.
Reply
#9

Hopefully the guy that gave me the solution like 6 months ago still regularly visits the forum lol.
Reply
#10

Here is a demonstration of the method I use for second commands in a command:
pawn Код:
// somewhere on the top of the script:
#define COLOR_RED 0xFF0000AA

dcmd_house(playerid, params[])
{
    new
      cmd2[64], // second command will be stored in this array
      string[128]; // for additional parameters in the second commands (see below)
    if (Account[playerid][pAdminLevel] >= 20) // admin level check
    {
        if (sscanf(params, "s ", cmd2) != 0) // cmd2 will become the first parameter, sscanf is tricked with the extra space to stop processing the string
        {
            // send proper command syntax for player, include only the first parameters (the second commands):
            SendClientMessage(playerid, COLOR_RED, "Usage: /house <add / whatever more params here>");
            return 1; // exit
        }

        if (strcmp("add", cmd2, true, 3) == 0) // check if the first 3 (!) characters of cmd2 are the same as 'add'
        {
            if (sscanf(params, "ss", cmd2, string) != 0) // read the additional parameters after the second command
            {
                // send proper syntax again, now with the needed parameters for the second command the player typed (in this case 'add'):
                SendClientMessage(playerid, COLOR_RED, "Usage: /house <add> <owner>"); // we are expecting only one extra parameter, the 'owner'
                return 1; // exit
            }
            /*
            // you may want to add this extra check for more security ^^
            if (strlen (string) > MAX_PLAYER_NAME) return SendClientMessage(playerid, COLOR_RED, "The given owner name is too long!");
            */

            // command code follows (yours), note that string is now the owner
            new
                Float:x,
                Float:y,
                Float:z,
                Float:angle;
            GetPlayerPos(playerid,x,y,z);
            GetPlayerFacingAngle(playerid,angle);
            AddHouse(owner,x,y,z,GetPlayerVirtualWorld(playerid),GetPlayerInterior(playerid),angle,0/*exitx*/,0/*exity*/,0/*exitz*/,1337/*exitinterior*/,0/*exitangle*/,0/*owned*/,0/*rentable*/,0/*rentcost*/,0/*houseprice*/,0/*locked*/);
            return 1; // successful, exit
        }
        /*
        // additional second commands may come here in this format:
        else if (strcmp("remove", cmd2, true, 6) == 0)
        {
          // check for other params if any
          // check validness of other params
          // code follows
        }
        else if (strcmp("info", cmd2, true, 4) == 0)
        {
          // check for other params if any
          // check validness of other params
          // code follows
        }
        // you may add as many as you want
        */

    }
    /*
    // you may want to send this warning if player if not a level 20 admin:
    else SendClientMessage(playerid, SOME_COLOR, "This is a level 20 admin command!");
    */

    return 1; // end of command
}
This works for me, I use it in multiple commands, hope it helps.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)