Quantcast
Channel: SA-MP Forums
Viewing all articles
Browse latest Browse all 18226

[Tutorial] How to add commands to your server like a PRO.

$
0
0
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>
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;


Is'n it simple more simple that using strcmp?
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 
targetidFloat: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(targetidhealth); //Notice that we use here targetid and not playerid
    
new message[100], name[25];
    
GetPlayerName(playeridnamesizeof(name));
    
format(message,sizeof(message),"Admin %s set your health to %f",name,health);
    
SendClientMessage(targetid,-1,message);
    
GetPlayerName(targetidnamesizeof(name));
    
format(message,sizeof(message),"You have set the health of %s to %f",namehealth);
    
SendClientMessage(playerid, -1message);
    return 
1;


This command is just an example.

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 
targetidFloat: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(targetidhealth); //Notice that we use here targetid and not playerid
    
new message[100], name[25];
    
GetPlayerName(playeridnamesizeof(name));
    
format(message,sizeof(message),"Admin %s set your health to %f",name,health);
    
SendClientMessage(targetid,-1,message);
    
GetPlayerName(targetidnamesizeof(name));
    
format(message,sizeof(message),"You have set the health of %s to %f",namehealth);
    
SendClientMessage(playerid, -1message);
    return 
1;


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.

Viewing all articles
Browse latest Browse all 18226

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>