SA-MP Forums Archive
[Tutorial] Creating commands with y_commands and sscanf - 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Creating commands with y_commands and sscanf (/showthread.php?tid=290132)



Creating commands with y_commands and sscanf - SmiT - 14.10.2011

Introduction
y_commands & sscanf
Downloading
Windows
Код:
plugins sscanf
Linux
Код:
plugins sscanf.so
Including

We need to load sscanf and y_commands features. At the top of our script we have to include them.

pawn Код:
#include <YSI\y_commands>
#include <sscanf2>
This will load the codes from "..pawno/include/YSI/y_commands.inc" and "..pawno/include/sscanf2.inc" into your script.

Important Note
Callbacks
pawn Код:
public OnPlayerConnect(playerid)
{
    return 1;
}
Adding a command
pawn Код:
YCMD:my_command(playerid, params[], help)
{
    return 1;
}
pawn Код:
YCMD:my_command(playerid, o[], help)
{
    return 1;
}
y_commands help system
pawn Код:
YCMD:help(playerid, params[], help)
{
    if (help) return SendClientMessage(playerid, -1, "Displays help about commands");
    // If help system occured, "/help help" the player will get the message above.
    if (isnull(params)) return SendClientMessage(playerid, -1, "Syntax Error: /help [Command]");
    // If the parameters are null "/help" the player will get the syntax message above ( COLOR = -1 wich is some sort of black )
    Command_ReProcess(playerid, params, true);
    // Call to put text through the command processor, with the help functions enabled or disabled.
    return 1;
}
Creating commands without sscanf
pawn Код:
YCMD:information(playerid, params[], help)
{
    SendClientMessage(playerid, -1, "Tutorial by SmiT !");
    return 1;
}
Код:
warning 203: symbol is never used "params"
warning 203: symbol is never used "help"
1.We can tell our script that "params" and "help" are unused with the "#pragma unused symbol" directive:

pawn Код:
YCMD:information(playerid, params[], help)
{
    #pragma unused params, help
    SendClientMessage(playerid, -1, "Tutorial by SmiT !");
    return 1;
}
2.We use the "help" parameter and make a check if the parameters of the command are null:

pawn Код:
YCMD:information(playerid, params[], help)
{
    if( help ) return SendClientMessage(playerid, -1, "This command displays some information");
    // if parameter HELP occured, (player types "/help information") it will return the message above.
    if( isnull( params ) ) // We check if the parameters are NULL. So it won't work something like "/information word".
    {
        SendClientMessage(playerid, -1, "Tutorial by SmiT !");
        // So, if the  parameters are null the player will get the message above.
    }
    return 1;
}
Another example
pawn Код:
YCMD:ooc(playerid, params[], help)
{
    new string[ 128 ];
    new PlayerName[ MAX_PLAYER_NAME ];
    if( help ) return SendClientMessage(playerid, -1, "With this command you can talk in OOC chat!");
    if( isnull( params ) ) return SendClientMessage(playerid, -1, "Syntax Error: /ooc [Your Text]");
    GetPlayerName( playerid, PlayerName, sizeof( PlayerName ) );
    format( string, sizeof( string ),"(OOC CHAT) %s: %s", PlayerName, params);
    SendClientMessageToAll(-1, string);
    return 1;
}

StringPlayerNameNoteHelp systemIsnullArraysGetting the player nameFormatPlaceholdersSendClientMessageToAll Example result
Код:
(OOC CHAT) SmiT: Hello
Placeholders

Код:
%b Inserts a number at this position in binary radix.
%c Inserts a single character.
%d Inserts an integer (whole) number
%f Inserts a floating point number.
%i Inserts an integer.
%s Inserts a string. ( We used this )
%x Inserts a number in hexadecimal notation.
%% Inserts the literal '%'
Adding an alternate spelling

Gamemode

pawn Код:
public OnGameModeInit()
{
    Command_AddAltNamed("information", "info");
    return 1;
}
Filterscript

pawn Код:
public OnFilterScriptInit()
{
    Command_AddAltNamed("information", "info");
    return 1;
}
Creating commands with sscanf
pawn Код:
YCMD:heal(playerid, params[], help)
{
    new PlayerID;
    if( help ) return SendClientMessage(playerid, -1, "With this command you can heal a player!");
    if( sscanf( params, "u", PlayerID ) ) return SendClientMessage(playerid, -1, "Syntax Error: /heal [PlayerName / ID]");
    if( PlayerID == INVALID_PLAYER_ID ) return SendClientMessage(playerid, -1, "[ERROR] This player is OFFLINE");
    SetPlayerHealth(PlayerID, 100);
    return 1;
}
Declaring
Help system
Sscanf part
INVALID_PLAYER_ID
SetPlayerHealth
Sscanf formula in a command
pawn Код:
if( !sscanf( params, "specifiers_here", your_variables ) )  // If the player entered the CORRECT parameters ( NOTE the "!" )
{
    // Do your code
}
pawn Код:
if( sscanf ( params, "specifiers_here", your_variables ) ) // If the player entered the WRONG parameters
{
    // Do your code
}
Specifiers

Quote:
Originally Posted by ******
Посмотреть сообщение
Код:
Format					Use
L(true/false)				Optional logical truthity
l					Logical truthity
B(binary)				Optional binary number
b					Binary number
N(any format number)			Optional number
n					Number
C(character)				Optional character
c					Character
I(integer)				Optional integer
i					Integer
D(integer)				Optional integer
d					Integer
H(hex value)				Optional hex number
h					Hex number
O(octal value)				Optional octal value
o					Octal value
F(float)				Optional floating point number
f					Floating point number
G(float/INFINITY/-INFINITY/NAN/NAN_E)	Optional float with IEEE definitions
g					Float with IEEE definitions
{					Open quiet section
}					Close quiet section
P<delimiter>				Invalid delimiter change
p<delimiter>				Delimiter change
Z(string)[length]			Invalid optional string
z(string)[length]			Deprecated optional string
S(string)[length]			Optional string
s[length]				String
U(name/id)				Optional user (bot/player)
u					User (bot/player)
Q(name/id)				Optional bot (bot)
q					Bot (bot)
R(name/id)				Optional player (player)
r					Player (player)
A<type>(default)[length]		Optional array of given type
a<type>[length]				Array of given type
E<specification>(default)		Optional enumeration of given layout
e<specification>			Enumeration of given layout
'string'				Search string
%					Deprecated optional specifier prefix
A simple setskin command
pawn Код:
YCMD:setskin(playerid, params[], help)
{
    return 1;
}
pawn Код:
YCMD:setskin(playerid, params[], help)
{
    new PlayerID, SkinID, PlayerName[ MAX_PLAYER_NAME ], TargetName[ MAX_PLAYER_NAME ], string[ 128 ];
    if( help ) return SendClientMessage(playerid, -1, "With this command you can change a player skin!");
    if( sscanf( params, "ui", PlayerID, SkinID)) return SendClientMessage(playerid, -1, "Syntax Error: /setskin [PlayerName / ID] [SkinID]");
    if( PlayerID == INVALID_PLAYER_ID ) return SendClientMessage(playerid, -1, "[ERROR] This player is OFFLINE");
    if( SkinID > 299 || SkinID < 1 ) return SendClientMessage(playerid, -1, "Available SA:MP Skins: 1-299");
    GetPlayerName( playerid, PlayerName, sizeof ( PlayerName ) );
    GetPlayerName( PlayerID, TargetName, sizeof ( TargetName ) );
    format( string, sizeof ( string ), "Your Skin has been set to ID: %d by %s", SkinID, PlayerName);
    SendClientMessage(PlayerID, -1, string);
    format( string, sizeof ( string ), "You have changed %s Skin to ID: %s", TargetName, SkinID);
    SendClientMessage(playerid, -1, string);
    SetPlayerSkin(PlayerID, SkinID);
    return 1;
}
Declaring
Help system
Sscanf part
INVALID_PLAYER_ID
SkinID
PlayerName
TargetName
FormatSendClientMessageFormatSendClientMessageSetPlayerSkin A simple /eat command
pawn Код:
YCMD:eat(playerid, params[], help)
{
    new option[ 20 ];
    if( help ) return SendClientMessage(playerid, -1,"With this command you can eat a Cookie or a Cake");
    if( sscanf( params ,"s[20]", option) ) return SendClientMessage(playerid, -1,"Syntax Error: /eat [Cookie,Cake]");
    if( !strcmp( option,"Cookie") )
    {
        SendClientMessage(playerid, -1, "You have ate a cookie! You have been healed to 100.");
        SetPlayerHealth(playerid, 100);
    }
    else if( !strcmp( option,"Cake"))
    {
        SendClientMessage(playerid, -1, "You have ate a cake! You have been healed to 100.");
        SetPlayerHealth(playerid, 100);
    }
    else return SendClientMessage(playerid, -1,"[ERROR] The option can only be Cookie or Cake.");
    return 1;
}
DeclaringHelp systemSscanf partStrcmp CookieStrcmp CakeElse Permissions
pawn Код:
public OnPlayerLogin(playerid)
{
    if (IsPlayerAdmin ( playerid )) // If the player is logged into RCON
    {
        Command_SetPlayerNamed("setskin", playerid, true); // Enable the command for him
    }
    else // If the player is NOT logged into rcon
    {
        Command_SetPlayerNamed("help", playerid, false); // Disable  the command for him
    }
}
Commands
UNKNOWN COMMAND
Custom message
References
Result
pawn Код:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>
#include <YSI\y_commands>
#include <sscanf2>

#if defined FILTERSCRIPT

public OnFilterScriptInit()
{
    print("\n--------------------------------------");
    print(" Blank Filterscript by your name here");
    print("--------------------------------------\n");
    return 1;
}

public OnFilterScriptExit()
{
    return 1;
}

#else

main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
}

#endif

public OnGameModeInit()
{
    // Don't use these lines if it's a filterscript
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    Command_AddAltNamed("information", "info");
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid)
{
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    return 1;
}

public OnPlayerSpawn(playerid)
{
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    return 1;
}

public OnVehicleSpawn(vehicleid)
{
    return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/mycommand", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
    return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    return 1;
}

public OnPlayerEnterCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveCheckpoint(playerid)
{
    return 1;
}

public OnPlayerEnterRaceCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveRaceCheckpoint(playerid)
{
    return 1;
}

public OnRconCommand(cmd[])
{
    return 1;
}

public OnPlayerRequestSpawn(playerid)
{
    return 1;
}

public OnObjectMoved(objectid)
{
    return 1;
}

public OnPlayerObjectMoved(playerid, objectid)
{
    return 1;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
    return 1;
}

public OnVehicleMod(playerid, vehicleid, componentid)
{
    return 1;
}

public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
    return 1;
}

public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
    return 1;
}

public OnPlayerSelectedMenuRow(playerid, row)
{
    return 1;
}

public OnPlayerExitedMenu(playerid)
{
    return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    return 1;
}

public OnRconLoginAttempt(ip[], password[], success)
{
    return 1;
}

public OnPlayerUpdate(playerid)
{
    return 1;
}

public OnPlayerStreamIn(playerid, forplayerid)
{
    return 1;
}

public OnPlayerStreamOut(playerid, forplayerid)
{
    return 1;
}

public OnVehicleStreamIn(vehicleid, forplayerid)
{
    return 1;
}

public OnVehicleStreamOut(vehicleid, forplayerid)
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    return 1;
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
    return 1;
}
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
    if( !success ) return false;
    return true;
}
/*                                          COMMANDS                                            */
YCMD:help(playerid, params[], help)
{
    if (help) return SendClientMessage(playerid, -1, "Displays help about commands");
    if (isnull(params)) return SendClientMessage(playerid, -1, "Syntax Error: /help [Command]");
    Command_ReProcess(playerid, params, true);
    return 1;
}
YCMD:commands(playerid, params[], help)
{
    if ( help ) return SendClientMessage(playerid, -1, "Lists all the commands a player can use.");
    new
        count = Command_GetPlayerCommandCount( playerid );
    for ( new i = 0; i != count; ++i) SendClientMessage( playerid, -1, Command_GetNext ( i, playerid ) );
    return 1;
}
YCMD:information(playerid, params[], help)
{
    if( help ) return SendClientMessage(playerid, -1, "This command displays some information");
    if( isnull( params ) )
    {
        SendClientMessage(playerid, -1, "Tutorial by SmiT !");
    }
    return 1;
}
YCMD:ooc(playerid, params[], help)
{
    new string[ 128 ];
    new PlayerName[ MAX_PLAYER_NAME ];
    if( help ) return SendClientMessage(playerid, -1, "With this command you can talk in OOC chat!");
    if( isnull( params ) ) return SendClientMessage(playerid, -1, "Syntax Error: /ooc [Your Text]");
    GetPlayerName( playerid, PlayerName, sizeof( PlayerName ) );
    format( string, sizeof( string ),"(OOC CHAT) %s: %s", PlayerName, params);
    SendClientMessageToAll(-1, string);
    return 1;
}
YCMD:heal(playerid, params[], help)
{
    new PlayerID;
    if( help ) return SendClientMessage(playerid, -1, "With this command you can heal a player!");
    if( sscanf( params, "u", PlayerID ) ) return SendClientMessage(playerid, -1, "Syntax Error: /heal [PlayerName / ID]");
    if( PlayerID == INVALID_PLAYER_ID ) return SendClientMessage(playerid, -1, "[ERROR] This player is OFFLINE");
    SetPlayerHealth(PlayerID, 100);
    return 1;
}
YCMD:setskin(playerid, params[], help)
{
    new PlayerID, SkinID, PlayerName[ MAX_PLAYER_NAME ], TargetName[ MAX_PLAYER_NAME ], string[ 128 ];
    if( help ) return SendClientMessage(playerid, -1, "With this command you can change a player skin!");
    if( sscanf( params, "ui", PlayerID, SkinID)) return SendClientMessage(playerid, -1, "Syntax Error: /setskin [PlayerName / ID] [SkinID]");
    if( PlayerID == INVALID_PLAYER_ID ) return SendClientMessage(playerid, -1, "[ERROR] This player is OFFLINE");
    if( SkinID > 299 || SkinID < 1 ) return SendClientMessage(playerid, -1, "Available SA:MP Skins: 1-299");
    GetPlayerName( playerid, PlayerName, sizeof ( PlayerName ) );
    GetPlayerName( PlayerID, TargetName, sizeof ( TargetName ) );
    format( string, sizeof ( string ), "Your Skin has been set to ID: %d by %s", SkinID, PlayerName);
    SendClientMessage(PlayerID, -1, string);
    format( string, sizeof ( string ), "You have changed %s Skin to ID: %s", TargetName, SkinID);
    SendClientMessage(playerid, -1, string);
    SetPlayerSkin(PlayerID, SkinID);
    return 1;
}
YCMD:eat(playerid, params[], help)
{
    new option[ 20 ];
    if( help ) return SendClientMessage(playerid, -1,"With this command you can eat a Cookie or a Cake");
    if( sscanf( params ,"s[20]", option) ) return SendClientMessage(playerid, -1,"Syntax Error: /eat [Cookie,Cake]");
    if( !strcmp( option,"Cookie") )
    {
        SendClientMessage(playerid, -1, "You have ate a cookie! You have been healed to 100.");
        SetPlayerHealth(playerid, 100);
    }
    else if( !strcmp( option,"Cookie"))
    {
        SendClientMessage(playerid, -1, "You have ate a cake! You have been healed to 100.");
        SetPlayerHealth(playerid, 100);
    }
    else return SendClientMessage(playerid, -1,"[ERROR] The option can only be Cookie or Cake.");
    return 1;
}
Special thanks to:



Re: Creating commands with y_commands and sscanf - TheBluec0de - 14.10.2011

Nice


Re: Creating commands with y_commands and sscanf - Yamoo - 14.10.2011

Nice tutorial man, keep it up.


Re: Creating commands with y_commands and sscanf - Mr_Scripter - 14.10.2011

Good Work, Nicely Explained Thanks


Re: Creating commands with y_commands and sscanf - Zh3r0 - 14.10.2011

I hate the fact that you copied ******'s way of making threads! His are unique!


Re: Creating commands with y_commands and sscanf - Yamoo - 14.10.2011

Zh3ro I so want to give youz rep but I can't due to yourself taking so many from me, anyhow I noticed that but it's detailed well.


Re: Creating commands with y_commands and sscanf - System64 - 14.10.2011

Good tutorial, good explained.... rep+


Re: Creating commands with y_commands and sscanf - alpha500delta - 14.10.2011

Nice, but perhaps you should remove the "example result", because some noobs will just copy, paste and compile it without even looking at the tutorial itsself >.<


Re: Creating commands with y_commands and sscanf - Kush - 14.10.2011

Quote:
Originally Posted by Yamoo
Посмотреть сообщение
Zh3ro I so want to give youz rep but I can't due to yourself taking so many from me, anyhow I noticed that but it's detailed well.
Sucking up?

OT: Lengthy tutorial, horrifically explained.


Re: Creating commands with y_commands and sscanf - Yamoo - 15.10.2011

Quote:
Originally Posted by Siin
Посмотреть сообщение
Sucking up?

OT: Lengthy tutorial, horrifically explained.
Hell no, I lick up no one's arse.. He just told the truth, and I reply with the truth.

I would give a reputation for any point which is giving to be fair and if most people who recieved a rep point from me will see it's mostly because of posts.

Also lengthy tutorial? You expect a tutorial with one bloody line? Pathetic. Also it ain't really long.