Use sscanf to check the entry of 2nd params?
#1

Hi!

I've always been using strcmp & strtok until recently I realized how dumb I was as sscanf & ZCMD are SO MUCH easier. The problem I have with sscanf tho, is how to check if the first params were entered while the other were not?

Here is the command I am converting:

Код:
	COMMAND:makeadmin(playerid, params[])
	{
		if(PlayerInfo[playerid][pAdmin] >= 5)
		{
		if(sscanf(params, "ui", newadmin, adminlevel)) return SendClientMessage(playerid, -1, "USAGE: /makeadmin [player-id] [admin-level]")
		new newadmin;
		new adminlevel;
		new prom[128];
	 	if(strlen(tmp) >= 0 && !strlen(tmp2))
 		{
		    SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Available Adminstration levels are:");
		    SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 1: Moderator");
		    SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 2: Junior Admin");
            SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 3: General Admin");
            SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 4: Community Manager");
            SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 5: Executive Admin");
            return 1;
	 	}
        if(IsPlayerConnected(newadmin))
 		{
			new newadminname[MAX_PLAYER_NAME];
			GetPlayerName(newadmin, newadminname, MAX_PLAYER_NAME);
			format(prom, sizeof(prom), "You have promoted %s to a level %d administrator/moderator", newadminname, adminlevel);
			SendClientMessage(playerid, 0xFFFFFFFF, prom);
			format(prom, sizeof(prom), "You have been promoted by Admin %s to a level %d administrator/moderator", RemoveUnderScore(playerid), adminlevel);
			SendClientMessage(newadmin, 0xFFFFFFFF, prom);
			PlayerInfo[newadmin][pAdmin] = adminlevel;
			return 1;
     	 }
     	 else { SendClientMessage(playerid, 0xFFFFFFFF, "ERROR: That player is offline!"); }
		 }
	}
Now I am not sure how to check if the first params [u - playerid] has been entered but the 2nd were not so it would list the admin levels, any ideas? thanks!


EDIT: I used a different solution to fix the issue but I would really love to know if there's a method that would do what I need done.
Reply
#2

use capital "I" specifier for second parameter, this makes the parameter optional to enter, then first you check if player is online, then check if level is entered. Here i make it optional so it can be handled by your code instead of resulting "USAGE: /makeadmin [player-id] [admin-level]".
Код:
COMMAND:makeadmin(playerid, params[])
{
	if(PlayerInfo[playerid][pAdmin] >= 5)
	{
		new newadmin, adminlevel;
		if(sscanf(params, "uI", newadmin, adminlevel)) return SendClientMessage(playerid, -1, "USAGE: /makeadmin [player-id] [admin-level]")
		if(newadmin == INVALID_PLAYER_ID)
		{
			return SendClientMessage(playerid, 0xFFFFFFFF, "ERROR: That player is offline!");
		}
		else // player is connected
		{
			if(1 <= adminlevel <= 5) // check if entere admin level is 1 to 5
			{
				new newadminname[MAX_PLAYER_NAME], prom[128];
				GetPlayerName(newadmin, newadminname, MAX_PLAYER_NAME);
				format(prom, sizeof(prom), "You have promoted %s to a level %d administrator/moderator", newadminname, adminlevel);
				SendClientMessage(playerid, 0xFFFFFFFF, prom);
				format(prom, sizeof(prom), "You have been promoted by Admin %s to a level %d administrator/moderator", RemoveUnderScore(playerid), adminlevel);
				SendClientMessage(newadmin, 0xFFFFFFFF, prom);
				PlayerInfo[newadmin][pAdmin] = adminlevel;
				return 1;
			}
			else // level was not entered or not 1 to 5
			{
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Available Adminstration levels are:");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 1: Moderator");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 2: Junior Admin");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 3: General Admin");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 4: Community Manager");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 5: Executive Admin");
				return 1;
			}
		}
	}
}
Not tested yet, hope that helps.
Reply
#3

Quote:
Originally Posted by RoboN1X
Посмотреть сообщение
use capital "I" specifier for second parameter, this makes the parameter optional to enter, then first you check if player is online, then check if level is entered. Here i make it optional so it can be handled by your code instead of resulting "USAGE: /makeadmin [player-id] [admin-level]".
Код:
COMMAND:makeadmin(playerid, params[])
{
	if(PlayerInfo[playerid][pAdmin] >= 5)
	{
		new newadmin, adminlevel;
		if(sscanf(params, "uI", newadmin, adminlevel)) return SendClientMessage(playerid, -1, "USAGE: /makeadmin [player-id] [admin-level]")
		if(newadmin == INVALID_PLAYER_ID)
		{
			return SendClientMessage(playerid, 0xFFFFFFFF, "ERROR: That player is offline!");
		}
		else // player is connected
		{
			if(1 <= adminlevel <= 5) // check if entere admin level is 1 to 5
			{
				new newadminname[MAX_PLAYER_NAME], prom[128];
				GetPlayerName(newadmin, newadminname, MAX_PLAYER_NAME);
				format(prom, sizeof(prom), "You have promoted %s to a level %d administrator/moderator", newadminname, adminlevel);
				SendClientMessage(playerid, 0xFFFFFFFF, prom);
				format(prom, sizeof(prom), "You have been promoted by Admin %s to a level %d administrator/moderator", RemoveUnderScore(playerid), adminlevel);
				SendClientMessage(newadmin, 0xFFFFFFFF, prom);
				PlayerInfo[newadmin][pAdmin] = adminlevel;
				return 1;
			}
			else // level was not entered or not 1 to 5
			{
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Available Adminstration levels are:");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 1: Moderator");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 2: Junior Admin");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 3: General Admin");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 4: Community Manager");
				SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: Level 5: Executive Admin");
				return 1;
			}
		}
	}
}
Not tested yet, hope that helps.
Yep, perfect.

Thank you!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)