SA-MP Forums Archive
[Tutorial] Making a simple command to set a player's data - 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] Making a simple command to set a player's data (/showthread.php?tid=615374)



Making a simple command to set a player's data - NealPeteros - 21.08.2016

SETTING A PLAYER'S DATA WITH A COMMAND
INFORMATION
Hi, guys! I'm actually so bored right now, and I'm home alone. So, I decided to create my ever first tutorial here on SAMP Forums. As this will be my first tutorial, I'd like to apologize to all who are offended especially to advanced C Language scripters, who take this tutorial for newbies. Yes, I can accept those kinds of replies, since I'm only a 13 year-old beginner-medium scripter. This tutorial is for beginners who are confused on how to make commands like these.

Learning Objectives
In this tutorial, we are able to learn how to create a simple command to set a player's data. In this example, we'll choose
pawn Code:
/setlevel
as our example, but actually, there are alot of commands that can be used. Here's some:So, what are we waiting for? Let's go straight to the tutorial!


In the beginning, we'll create a #define to define the max players. Put this in below include <a_samp> and ontop of the next includes
Sample
PHP Code:
#include <a_samp> 
#undef MAX_PLAYERS 
#define MAX_PLAYERS 100 
#include <sscanf2> 
// #include ... 
PHP Code:
#undef MAX_PLAYERS 
#define MAX_PLAYERS 100 
Then, we will create an enumerator to store player's data.
PHP Code:
enum pInfo //This will create an enumerator named pInfo to store data about your player. 
PHP Code:
//Opening tag 
PHP Code:
    pLevel//You can add more here like pKill pDeath, etc. 
PHP Code:
}; // Closing tag 
Here, we'll create a variable named PlayerInfo
PHP Code:
new PlayerInfo[MAX_PLAYERS][pInfo]; //This will create a PlayerInfo variable 
So, in the end, our enumerator with the variable will look like this
PHP Code:
enum pInfo
{
    
pLevel
};
new 
PlayerInfo[MAX_PLAYERS][pInfo]; 
Once we have that, we can now create the command. I will be using ZCMD as my command processor.
PHP Code:
CMD:setlevel(playeridparams[]) //This will create the command /setlevel 
PHP Code:
//Opening tag 
PHP Code:
    new giveplayeridamount//This creates 2 variables namely giveplayerid and amount 
PHP Code:
    if(sscanf(params"ud"giveplayeridamount)) //If the player doesn't enter any other word after the command /setlevel 
PHP Code:
       if (giveplayerid == INVALID_PLAYER_ID// Checks if the ID entered is offline/invalid
       
return SendClientMessage(playerid, -1"Player is not connected."); 
PHP Code:
    //Then, 
PHP Code:
        SendClientMessage(playeridCOLOR_WHITE"USAGE: /setlevel [playerid] [amount]"); //It will send the player a message to show him/her how to use the command 
PHP Code:
        return 1//Stops a function and goes back to the point in code which called the function in the first place 
PHP Code:
    //Closing tag 
If the player added the correct use,
PHP Code:
    PlayerInfo[giveplayerid][pLevel] = amount//This will set the player's level to the amount given. You can add messages here to notify the player that his level was set to the amount 
PHP Code:
    return 1//Stops the command, and continues processing. 
And last, but not the least, our closing tag.
PHP Code:
//Closing tag 
In the end, our command will look like this
PHP Code:
CMD:setlevel(playeridparams[])
{
    new 
giveplayeridamount;
    if(
sscanf(params"ud"giveplayeridamount))
       if (
giveplayerid == INVALID_PLAYER_ID// Checks if the ID entered is offline/invalid 
       
return SendClientMessage(playerid, -1"Player is not connected.");  
    {
        
SendClientMessage(playeridCOLOR_WHITE"USAGE: /setlevel [playerid] [amount]");
        return 
1;
    }
    
PlayerInfo[giveplayerid][pLevel] = amount;
    return 
1;

So, this is my first tutorial in SAMP. Thanks for reading, and enjoy!


Re: Making a simple command to set a player's data - Stinged - 21.08.2016

Quote:
Originally Posted by NealPeteros
View Post
In the beginning, we'll create a #define to define the max players. Put this on top of your script, but below the includes.
PHP Code:
#define MAX_PLAYERS 100 
You have to #undef MAX_PLAYERS first or else it'll cause problems.
PHP Code:
#undef MAX_PLAYERS
#define MAX_PLAYERS 100 
Quote:
Originally Posted by NealPeteros
View Post
Put this on top of your script, but below the includes.
No, that's a huge mistake a lot of scripters make.
All the includes above the re-definition will use the default MAX_PLAYERS, which is 500.

You should always re-define MAX_PLAYERS under #include <a_samp>
PHP Code:
#include <a_samp>
#undef MAX_PLAYERS
#define MAX_PLAYERS 100
#include <sscanf2>
// #include ... 
Quote:
Originally Posted by NealPeteros
View Post
PHP Code:
if(sscanf(params"udd"giveplayeridamount)) 
There are only 2 parameters to enter (player's id/name and the level), so what's the use for the second "d"?
It's supposed to be "ud"


You should check if the player is connected or else the user can set INVALID_PLAYER_ID's level which would cause errors.
Because INVALID_PLAYER_ID > MAX_PLAYERS-1.

So, add this after the sscanf check:
PHP Code:
if (giveplayerid == INVALID_PLAYER_ID// We use that instead of IsPlayerConnected because sscanf2 automatically does that with "u"
    
return SendClientMessage(playerid, -1"Player is not connected."); 
By the way, you should mention the fact that this isn't a saving system.
A lot of new scripters that are looking for tutorials that teach how to make a registration system might get confused and think this is the same thing.


Re: Making a simple command to set a player's data - NealPeteros - 21.08.2016

Oops. Didn't see that mistake. And yeah. Nice suggestion about the invalid player id. Gonna edit it. Also, yeah. I didn't add the saving system. This is an add-on tutorial for the command. For scripters who have a script, but they still want to add the /setlevel, so this is for them. Sorry for not adding it though. Also, since this is my first TUT, please give me shots of your ratings(_/10) and suggestions, with additional comments to help me improve.


Re: Making a simple command to set a player's data - Logic_ - 22.08.2016

4/10, still needs improvement and explain how the sscanf works and how to use it as well as why should we use zcmd and sscanf instead of OPCommandText callback and strtok. Also, you didn't included zcmd in the script.


Re: Making a simple command to set a player's data - NealPeteros - 22.08.2016

Thanks for the rate. I'll try to improve the next time I'll make a tutorial.


Re: Making a simple command to set a player's data - Cerax101 - 29.08.2016

4/10 ---

Its Easy to make an CMD -- for Player Data Base

But your Is Good