SA-MP Forums Archive
SSCANF parameter issue. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SSCANF parameter issue. (/showthread.php?tid=652825)



SSCANF parameter issue. - Stefhan - 19.04.2018

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?


Re: SSCANF parameter issue. - Hunud - 19.04.2018

Try

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



Re: SSCANF parameter issue. - Stefhan - 19.04.2018

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.


Re: SSCANF parameter issue. - Hunud - 19.04.2018

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;
  		}



Re: SSCANF parameter issue. - AdamsLT - 19.04.2018

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.


Re: SSCANF parameter issue. - Stefhan - 19.04.2018

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.


Re: SSCANF parameter issue. - kovac - 19.04.2018

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


Re: SSCANF parameter issue. - AdamsLT - 19.04.2018

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.


Re: SSCANF parameter issue. - Stefhan - 19.04.2018

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!


Re: SSCANF parameter issue. - Dayrion - 19.04.2018

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: