Useful Snippets

Easy strcmp macros

pawn Код:
#define @[%1:%2] (strcmp(%1, %2, true) == 0)
Usage:
pawn Код:
printf("equal 1: %d", @["hello":"test"]);
    printf("equal 2: %d", @["hello":"hello"]);
    printf("equal 3: %d", @["hello":"HeLLo",false]);
    printf("equal 4: %d", @["hello":"HeLLo",true]);
Output:
Код:
equal 1: 0
equal 2: 1
equal 3: 0
equal 4: 1
And some kudos to westie
Reply

Simple as useful!
Nice one MaVe
Reply

Quote:
Originally Posted by MaVe;
Easy strcmp macros

pawn Код:
#define @[%1:%2] (strcmp(%1, %2, true) == 0)
Usage:
pawn Код:
printf("equal 1: %d", @["hello":"test"]);
    printf("equal 2: %d", @["hello":"hello"]);
    printf("equal 3: %d", @["hello":"HeLLo",false]);
    printf("equal 4: %d", @["hello":"HeLLo",true]);
Output:
Код:
equal 1: 0
equal 2: 1
equal 3: 0
equal 4: 1
And some kudos to westie
Something wrong in ur macro
Код:
	printf("equal 4: %d", @["hello":"HeLLo",true]);
This code will be translated to
Код:
	printf("equal 4: %d", (strcmp("hello","HeLLo",true,true)==0));
So it does only check one char.

If you do
Код:
	printf("equal 4: %d", @["hello":"Hxxxx",true]);
Still output 1, not a correct result at all.

Reply

Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
Reply

Quote:
Originally Posted by MaVe;
Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
EDIT: or try this maybe?
pawn Код:
#define @[%1:%2] (strcmp(%1, %2) == 0)
Reply

Quote:
Originally Posted by MaVe;
Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
I prefer this modtify:
pawn Код:
#define @[%1:%2] (!strcmp(%1, %2))
Effencify,and simple arg check
Reply

Quote:
Originally Posted by MaVe;
Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
I prefer this modtify:
pawn Код:
#define @[%1:%2] (!strcmp(%1, %2))
Effencify,and simple arg check
Reply

Quote:
Originally Posted by yezizhu
Quote:
Originally Posted by MaVe;
Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
I prefer this modtify:
pawn Код:
#define @[%1:%2] (!strcmp(%1, %2))
Effencify,and simple arg check
What! - sorry, we both used quote instead of modify. Check my second post, I added your way
Reply

Quote:
Originally Posted by MaVe;
Quote:
Originally Posted by yezizhu
Quote:
Originally Posted by MaVe;
Sorry then, use either no false/true argument, or try use this:

pawn Код:
#define @[%1:%2:%3] (strcmp(%1, %2, %3) == 0)
I prefer this modtify:
pawn Код:
#define @[%1:%2] (!strcmp(%1, %2))
Effencify,and simple arg check
What! - sorry, we both used quote instead of modify. Check my second post, I added your way
It's a fault , sorry all
My advise - edit ur first post to the edition we both refering.
Reply

/ me and /whisper commands by me

Код:
#define COLOR_INFO 0x80FF0099//green
#define COLOR_WHISPER 0xFFFF00AA//yellow
Код:
	if(!strcmp(cmdtext, "/me", true, 3))
	{
		if(!cmdtext[3])return SendClientMessage(playerid, COLOR_INFO, "[INFO] USAGE: /me [action]");
		new str[128];
		format(str, sizeof(str), "* %s",cmdtext[4]);
		SetPlayerChatBubble(playerid, str, COLOR_WHITE, 150.0, 10000);
		SendClientMessage(playerid, COLOR_INFO, "[INFO] Message Displaying for 10 seconds.");
		return 1;
	}
Код:
	if(!strcmp(cmdtext, "/whisper", true, 8))
	{
		if(!cmdtext[8])return SendClientMessage(playerid, COLOR_WHISPER, "[WHISPER] USAGE: /whisper [text]");
		new playername[MAX_PLAYER_NAME];
		new string[128];
		GetPlayerName(playerid,playername,sizeof(playername));
		format(string,sizeof(string),"[WHISPER] %s: %s",playername,cmdtext[9]);
		for(new i=0; i<MAX_PLAYERS; i++)
		{
			if(IsPlayerConnected(i))
			{
				if(GetDistanceBetweenPlayers(playerid,i) < 15)
				{
					SendClientMessage(i,COLOR_WHISPER,string);
				}
			}
		}
		return 1;
	}
Reply

/putincar [playerid/part of name]

playerid/part of name - ID or name of the player you want to put yourself into

This command uses ZCMD and SSCANF,
[iurl=#seat]IsSeatTaken(vehicleid, seatid);[/iurl] by Mikep and
[iurl=#pname]PlayerName(playerid);[/iurl] by Dracoblue:
pawn Код:
CMD:putincar(playerid, params[])
{
    if( !IsPlayerAdmin( playerid ))
        return SendClientMessage( playerid, 0xFF0000FF, "[ERROR]Admins only!" );
    new
        iPlayer,
        iSeat,
        szSuccess;
    if( sscanf( params, "ui", iPlayer, iSeat ))
        return SendClientMessage( playerid, 0xFFFF00FF, "[ERROR]USAGE: \"/putincar [playerid/part of name]\" - Put you inside this player's vehicle" );
    if( !IsPlayerConnected( iPlayer ))
        return SendClientMessage( playerid, 0xFF0000FF, "[ERROR]Invalid ID!" );
    if( !IsPlayerInAnyVehicle( iPlayer ))
        return SendClientMessage( playerid, 0xFF0000FF, "[ERROR]This player isn't inside any vehicle!" );
    if( iSeat < 0 || iSeat > 4 )
        return SendClientMessage( playerid, 0xFF0000FF, "[ERROR]Seats are 0-4!" );
    if( IsSeatTaken( GetPlayerVehicleID( iPlayer ), iSeat ))
        return SendClientMessage( playerid, 0xFF0000FF, "[ERROR]This seat is already taken!" );
    PutPlayerInVehicle( playerid, GetPlayerVehicleID( iPlayer ), iSeat );
    format( szSuccess, sizeof( szSuccess ), "[SUCCESS]You have put yourself in (%d)%s's vehicle, in seat %d", iPlayer, PlayerName( iPlayer ), iSeat );
    SendClientMessage( playerid, 0x00FF00FF, szSuccess );
    return true;
}
[anchor=seat]IsSeatTake(vehicleid, seatid);
pawn Код:
stock IsSeatTaken(vehicleid, seatid)
{
    for(new i=0; i<MAX_PLAY; i++)
    {
        if(GetPlayerVehicleID(i) == vehicleid && GetPlayerVehicleSeat(i) == seatid) return 1;
    }
    return 0;
}
[anchor=pname]PlayerName(playerid);
pawn Код:
stock PlayerName(playerid)
{
    new name[24];
    GetPlayerName(playerid, name, 24);
    return name;
}
NOTE: This isn't bug free, there are vehicles who only have 1-2 seats and maybe more than 4 (ambulance?).

Enjoy, lol :P
Reply

Quote:
Originally Posted by fps_doug
/ me and /whisper commands by me

Код:
#define COLOR_INFO 0x80FF0099//green
#define COLOR_WHISPER 0xFFFF00AA//yellow
Код:
	if(!strcmp(cmdtext, "/me", true, 3))
	{
		if(!cmdtext[3])return SendClientMessage(playerid, COLOR_INFO, "[INFO] USAGE: /me [action]");
		new str[128];
		format(str, sizeof(str), "* %s",cmdtext[4]);
		SetPlayerChatBubble(playerid, str, COLOR_WHITE, 150.0, 10000);
		SendClientMessage(playerid, COLOR_INFO, "[INFO] Message Displaying for 10 seconds.");
		return 1;
	}
Код:
	if(!strcmp(cmdtext, "/whisper", true, 8))
	{
		if(!cmdtext[8])return SendClientMessage(playerid, COLOR_WHISPER, "[WHISPER] USAGE: /whisper [text]");
		new playername[MAX_PLAYER_NAME];
		new string[128];
		GetPlayerName(playerid,playername,sizeof(playername));
		format(string,sizeof(string),"[WHISPER] %s: %s",playername,cmdtext[9]);
		for(new i=0; i<MAX_PLAYERS; i++)
		{
			if(IsPlayerConnected(i))
			{
				if(GetDistanceBetweenPlayers(playerid,i) < 15)
				{
					SendClientMessage(i,COLOR_WHISPER,string);
				}
			}
		}
		return 1;
	}
Not very familiar with strcmp .. but, doesn't
Код:
if(!strcmp ..
mean if the command entered does not match the text in the command?

For example:
pawn Код:
if(!strcmp(cmdtext, "/me", true, 3)) // if string entered does not match "/me" do not continue
{ // continue with processing if it isn't "/me"
Maybe I'm wrong.
Reply

Quote:
Originally Posted by » ραωпsтαг «
Not very familiar with strcmp .. but, doesn't
Код:
if(!strcmp ..
mean if the command entered does not match the text in the command?

For example:
pawn Код:
if(!strcmp(cmdtext, "/me", true, 3)) // if string entered does not match "/me" do not continue
{ // continue with processing if it isn't "/me"
Maybe I'm wrong.
Yes you are wrong, an easy check on the wiki would have told you the answer -> https://sampwiki.blast.hk/wiki/Strcmp
Reply

So what does the ! do? I see on some commands it has it and on other it doesn't Wiki didn't tell me much.
Reply

false stands 0 - (!statement) or (statement == false) or (statement == 0)
true stands for all positiv numbers - (statement) or (statement == true) or (statment > 0)

lately in irc, someone asked if (statement) is the same as (statement == 1)
and someone else said yes, I loled and switched the channel :O
Reply

Okay, lol.

Reply

Quote:
Originally Posted by ♣ ⓐⓢⓢ
false stands 0 - (!statement) or (statement == false) or (statement == 0)
true stands for all positiv numbers - (statement) or (statement == true) or (statment > 0)

lately in irc, someone asked if (statement) is the same as (statement == 1)
and someone else said yes, I loled and switched the channel :O
True stands for positive and negative numbers and the guy on irc was right. (statement) means (statement != 0), which is in some way (statement == 1)
pawn Код:
for(new statement = -1; statement < 3; statement ++)
    {
        if (statement)
        {
          printf("%d", statement);
        }
    }
This will print -1, 1 and 2
Reply

Quote:
Originally Posted by dice7
Quote:
Originally Posted by ♣ ⓐⓢⓢ
false stands 0 - (!statement) or (statement == false) or (statement == 0)
true stands for all positiv numbers - (statement) or (statement == true) or (statment > 0)

lately in irc, someone asked if (statement) is the same as (statement == 1)
and someone else said yes, I loled and switched the channel :O
True stands for positive and negative numbers and the guy on irc was right. (statement) means (statement != 0), which is in some way (statement == 1)
pawn Код:
for(new statement = -1; statement < 3; statement ++)
    {
        if (statement)
        {
          printf("%d", statement);
        }
    }
This will print -1, 1 and 2
No, lol
if (statement) is the same as if (statement == true) not if (statement == 1)
Bool and integer's aren't the same, I'm afraid
Reply

Quote:
Originally Posted by ♣ ⓐⓢⓢ
true stands for all positiv numbers - (statement) or (statement == true)
No, true (bool) stands only for 1 and you can only set true (1) or false (0) for the boolean variable.
Reply

Quote:
Originally Posted by lrZ^ aka LarzI
No, lol
if (statement) is the same as if (statement == true) not if (statement == 1)
Bool and integer's aren't the same, I'm afraid
Bool and integer are only different by size and even size depends on implentation. In PAWN they are same, bool: is just tag meaning variable must be set to true or false, otherwise compiler throws error/warning
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)