SSCANF parameter issue.
#1

Hello!

Basically, what I am trying to create is a /set command that has a lot of options.

For example, /set adminlevel, /set weather, /set time and so on.

This is what I have so far:

Код:
CMD:set(playerid,params[]) {
	new str[128], option[30], targetid, level;
	if(sscanf(params, "s[30]", option)) {
        SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: /set [option]");
        return SendClientMessage(playerid, 0xFFFFFFFF, "Available options: adminlevel");
	}
	if(strmatch(params, "adminlevel"))
	{
		if(sscanf(params, "s[30]ui", option, targetid, level))
		format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetid, level);
		SendClientMessage(playerid,-1,str);
		PlayerInfo[targetid][pAdmin] = level;
	}
	else return cmd_set(playerid, "");
	return 1;
}
Obviously checking if the player is online, if the player is admin etc is missing but that's not the issue for now.

My issue is, that it only checks the first sscanf line. ( this one. )
Код:
	if(sscanf(params, "s[30]", option)) {
It completely ignores this one:
Код:
		if(sscanf(params, "s[30]ui", option, targetid, level))
How do I solve this problem?
Reply
#2

Try

Код:
if(sscanf(params, "rs[30]i", option, targetid, level)) {
Reply
#3

Quote:
Originally Posted by Hunud
Посмотреть сообщение
Try

Код:
if(sscanf(params, "rs[30]i", option, targetid, level)) {
Thanks for your reply, but it doesn't seem to solve the issue. Could you answer more specifically? I tried replacing it for both SSCANF lines and seperately, but it doesn't work.
Reply
#4

Not tested, try this.

Код:
CMD:set(playerid,params[])
		{
			new string[128], giveplayerid, statcode, level;
			if(sscanf(params, "udd", giveplayerid, statcode, level)) {
			
        	SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: /set [option]");
        	return SendClientMessage(playerid, 0xFFFFFFFF, "Available options: adminlevel");
		}
		if(IsPlayerConnected(giveplayerid))
		{
			switch (statcode)
			{
  				case 1:
				{
					format(string,sizeof(string),"You gave %s(%i) admin level %i!",GetName(giveplayerid), giveplayerid, level);
					SendClientMessage(playerid,-1, string);
					PlayerInfo[giveplayerid][pAdmin] = level;
				}
			}
			return 1;
  		}
Reply
#5

PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel))
        
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
This code of yours now acts like this:
PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel))
        {
                
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        }
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
Notice the brackets around format. Your if statement does that block of code.

You didn't provide anything for the sscanf to do if there aren't enough params there.
What happens now is that it always sends you an empty message (str variable) because the format function is only called when sscanf doesn't pass correctly.

PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel)) 
        {
                return 
SendClientMessage(playerid,-1"/set adminlevel [ID] [Level]"); 
        }
        
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
See if this fixes up your problem.

If it doesn't, please be more specific, can you print out the params before sscanf, then print out the scanned variables after using sscanf or something more? It would help knowing some more info on what to look for.

Also, it might be problematic that you only scan for a string at the start. This is pure speculation, as I don't correctly remember if this is how it works but if you write "/set adminlevel user 1" could possibly make option = "adminlevel user 1" and then strmatch will not pass. Although, I don't know how strmatch works as I use strcmp for these cases.
If you don't want to change your code to accommodate strcmp, you can try using strfind, don't forget to look it up in the wiki to see how it works beforehand. This would probably fix the problem.
Or you can modify the first sscanf and only scan to the first space, which would probably work, I suppose.
Reply
#6

Quote:
Originally Posted by AdamsLT
Посмотреть сообщение
PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel))
        
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
This code of yours now acts like this:
PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel))
        {
                
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        }
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
Notice the brackets around format. Your if statement does that block of code.

You didn't provide anything for the sscanf to do if there aren't enough params there.
What happens now is that it always sends you an empty message (str variable) because the format function is only called when sscanf doesn't pass correctly.

PHP код:
        if(sscanf(params"s[30]ui"optiontargetidlevel)) 
        {
                return 
SendClientMessage(playerid,-1"/set adminlevel [ID] [Level]"); 
        }
        
format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetidlevel);
        
SendClientMessage(playerid,-1,str);
        
PlayerInfo[targetid][pAdmin] = level
See if this fixes up your problem.

If it doesn't, please be more specific, can you print out the params before sscanf, then print out the scanned variables after using sscanf or something more? It would help knowing some more info on what to look for.

Also, it might be problematic that you only scan for a string at the start. This is pure speculation, as I don't correctly remember if this is how it works but if you write "/set adminlevel user 1" could possibly make option = "adminlevel user 1" and then strmatch will not pass. Although, I don't know how strmatch works as I use strcmp for these cases.
If you don't want to change your code to accommodate strcmp, you can try using strfind, don't forget to look it up in the wiki to see how it works beforehand. This would probably fix the problem.
Or you can modify the first sscanf and only scan to the first space, which would probably work, I suppose.
I'll try to be more specific and hope you can help me.

I've updated the code with what you gave and this is what happens:

If I type "/set" it displays this:

If I type "/set adminlevel" it displays this:

If I type "/set adminlevel 0 5" ( give myself level 5 admin ) it displays this:


So the issue I have is that it seems to always fall back on the first sscanf line and doesn't go with the second one or something like that. It's kinda difficult to explain but I hope you can help me fix this problem, as I've been trying for 3 hours.

The current code:
Код:
CMD:set(playerid,params[]) {
	new str[128], option[30], targetid, level;
 	if(sscanf(params, "s[30]", option)) {
        SendClientMessage(playerid, 0xFFFFFFFF, "SERVER: /set [option]");
        return SendClientMessage(playerid, 0xFFFFFFFF, "Available options: adminlevel");
	}
	if(strmatch(option, "adminlevel"))
	{
        if(sscanf(params, "s[30]ui", option, targetid, level))
        {
                return SendClientMessage(playerid,-1, "/set adminlevel [ID] [Level]");
        }
        format(str,sizeof(str),"You gave %s(%i) admin level %i!",GetName(targetid), targetid, level);
        SendClientMessage(playerid,-1,str);
        PlayerInfo[targetid][pAdmin] = level;
	}
	else return cmd_set(playerid, "");
	return 1;
}
Thanks for your help so far.
Reply
#7

http://forum.sa-mp.com/showthread.ph...85#post3980785
Reply
#8

Could you try to print out the option variable after the first sscanf? Both inside the if statement and outside of it.
Also print out params before the first sscanf and try the same commands again.
Reply
#9

I got it working thanks to Dayrion's post on this thread: http://forum.sa-mp.com/showthread.ph...85#post3980785

Thanks a lot for everyone who tried to help!
Reply
#10

Quote:
Originally Posted by Stefhan
Посмотреть сообщение
I got it working thanks to Dayrion's post on this thread: http://forum.sa-mp.com/showthread.ph...85#post3980785

Thanks a lot for everyone who tried to help!
You are welcome. c:
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)