First words: This is my first tutorial on this forum, so judge me about its quality, because from wrong things we learn to do better.
First you need YSI 4.0 includes and sscanf plugin.
And add this as top of your script, after #incude <a_samp>
You all know the callback OnPlayerCommandText(playerid,cmdtext[]), also you know how it works (Player types a command , for example /heal and then the server passes it OnPlayerCommandText in cmdtext), but how commands processors works? They work in a way like this, but are faster and splitting up that parameter 'cmdtext' to commandname and params, but these are not that important. Is important how easy is to make a command:
Let's make a simple command:
Is'n it simple more simple that using strcmp?
What sscanf does? It is the oposite the format. (is also named unformat and can be used with this name).
A full tutorial of it made by Emmet_ can be found here.
Let's make a command to heal someone only usable by RCon Admin:
This command is just an example.
See how it works? This is the end of the tutorial, and i want to be judged for its quality :) (some people ask about reps at end of a tutorial, I find it like begging).
PS: Sorry for my bad english.
First you need YSI 4.0 includes and sscanf plugin.
And add this as top of your script, after #incude <a_samp>
PHP Code:
#include <YSI-Includes-YSI.tl/YSI/y_commands> //This applies only if you unarchived the folder from the YSI zip dirrectly to the includes folder.
#include <sscanf3> //This applies only if you have sscanf3 , else you should include sscanf2 (make sure you have it)
Introduction to Command Processors
You all know the callback OnPlayerCommandText(playerid,cmdtext[]), also you know how it works (Player types a command , for example /heal and then the server passes it OnPlayerCommandText in cmdtext), but how commands processors works? They work in a way like this, but are faster and splitting up that parameter 'cmdtext' to commandname and params, but these are not that important. Is important how easy is to make a command:
PHP Code:
YCMD:heal(playerid,params[],help)
{
//some code
return 1;
}
Creating a simple command
The structure of a command, is as seen in the example above, is this: YCMD:COMMAND_NAME(playerid,params[],help).Let's make a simple command:
PHP Code:
YCMD:healme(playerid,params[],help)
{
SetPlayerHealth(playerid,100.0);
SendClientMessage(playerid,-1,"You have been healed");
return 1;
}
PHP Code:
public OnPlayerCommandText(playerid,cmdtext[])
{
if(!strcmp(cmdtext,"healme"))
{
SetPlayerHealth(playerid,100.0);
SendClientMessage(playerid,-1,"You have been healed");
return 1;
}
return 0;
}
Using params and sscanf
Here the plugin mentioned above, sscanf, will be used.What sscanf does? It is the oposite the format. (is also named unformat and can be used with this name).
A full tutorial of it made by Emmet_ can be found here.
Let's make a command to heal someone only usable by RCon Admin:
PHP Code:
YCMD:sethealth(playerid,params[],help)
{
if(!IsPlayerAdmin(playerid)) //We use ! to check if playerid is NOT RCon Admin.
{
SendClientMessage(playerid,-1,"Sorry, but you are not a RCon Admin."); //Here we send a message to playerid
return 1; //Return ends the function, it takes out an else from the code
}
new targetid, Float:health; //We use Float:health because the function SetPlayerHealth takes a float as parameter (also works without the float tag, but it uses a different sscanf format parameter)
if(sscanf(params,"df",targetid,health)) //sscanf will return 1 if the parameters are not where they migh be.
{
SendClientMessage(playerid,-1,"Usage: /sethealth <TargetID> <Float:Health>");
return 1;
}
SetPlayerHealth(targetid, health); //Notice that we use here targetid and not playerid
new message[100], name[25];
GetPlayerName(playerid, name, sizeof(name));
format(message,sizeof(message),"Admin %s set your health to %f",name,health);
SendClientMessage(targetid,-1,message);
GetPlayerName(targetid, name, sizeof(name));
format(message,sizeof(message),"You have set the health of %s to %f",name, health);
SendClientMessage(playerid, -1, message);
return 1;
}
What is the help parameter?
It is a boolean parameter, and is set to true when a player types something like this: /command help. Let's use the help parameter in the command above:PHP Code:
YCMD:sethealth(playerid,params[],help)
{
if(!IsPlayerAdmin(playerid)) //We use ! to check if playerid is NOT RCon Admin.
{
SendClientMessage(playerid,-1,"Sorry, but you are not a RCon Admin."); //Here we send a message to playerid
return 1; //Return ends the command / function
}
if(help)
{
SendClientMessage(playerid,-1,"This command allows you to set the health of someone to a specified value");
return 1;
}
new targetid, Float:health; //We use Float:health because the function SetPlayerHealth takes a float as parameter (also works without the float tag, but it uses a different sscanf format parameter)
if(sscanf(params,"df",targetid,health)) //sscanf will return 1 if the parameters are not where they migh be.
{
SendClientMessage(playerid,-1,"Usage: /sethealth <TargetID> <Float:Health>");
return 1;
}
SetPlayerHealth(targetid, health); //Notice that we use here targetid and not playerid
new message[100], name[25];
GetPlayerName(playerid, name, sizeof(name));
format(message,sizeof(message),"Admin %s set your health to %f",name,health);
SendClientMessage(targetid,-1,message);
GetPlayerName(targetid, name, sizeof(name));
format(message,sizeof(message),"You have set the health of %s to %f",name, health);
SendClientMessage(playerid, -1, message);
return 1;
}
PS: Sorry for my bad english.