How to Create Basic Commands - Printable Version + open.mp forum (https://forum.open.mp) -- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3) --- Forum: Tutorials (https://forum.open.mp/forumdisplay.php?fid=37) --- Thread: How to Create Basic Commands (/showthread.php?tid=341) |
How to Create Basic Commands - DTV - 2019-04-18 Note: I'm by no means a professional at scripting but what I'm going to tell is what I've learned through the years. I'm going to explain it how I understand it so apologizes if how I describe something isn't correct. This also won't be using sampctl for obtaining the proper include/plugin files, I've simply haven't learned it yet so please don't haze me for not using it here. How to Create Basic Commands
Last Updated: 17/04/19
This tutorial is aimed at beginners who have little experience with coding. It will go over what you'll need, how to create a command, what you can do within a command, etc.
I. Requirements - Improved ZCMD - sscanf II. Getting started First thing you'll need to do is get your script ready. If you use the default IDE that comes with the SA-MP server package, you can press ?New? and it will create this for you. However, the following will work as well: PHP Code: #include <a_samp> The first thing we'll need to do is add I-ZCMD and sscanf to our script so that we can access their functions. We do this by typing #include <include_name_here>?at the top of your script. It should look something like this: PHP Code: #include <a_samp> //any native samp includes should be above all user-made includes Now you should compile the script to make sure no errors occur. If done right, you should have no errors or warnings. Next step is creating the commands. III. Creating a Command Creating a command in the early days of SA-MP required use of public OnPlayerCommandText(playerid, cmdtext[]) along with using strcmp and strtok to understand what commands were being typed and to gather command parameters that the player typed alongside the command. This is no longer used by most scripters as its been shown time and time again to be very inefficient compared to other command processors made today. We'll be using izcmd as our command processor as its (or the original zcmd) the one that most scripters use today. We create a command by doing the following: PHP Code: CMD:examplecmd(playerid, params[]) The example above is one of the common ways scripters will create a command, however izcmd allows you to create a command a few more ways such as: PHP Code: COMMAND:examplecmd(playerid, params[]) For the rest of the tutorial, we'll be using the first example. Commands can be placed anywhere in the script below the #include?lines. Now when a player types in /examplecmd, it won't do anything as there's no code to go along with it however the script will recognize it as a command. Let's made this command send out a simple message: PHP Code: CMD:examplecmd(playerid, params[]) Now when a player types /examplecmd, it will show them the message ?Hello world, I made a command!?. This is a very simple command however, so let's make it give the player full health and armour: PHP Code: CMD:examplecmd(playerid, params[]) Now when the player types /examplecmd, it will set the player's health and armour values to 100 along with sending them a message. Now, making commands like this will work fine but what if a player needs to type in additional information that the command requires from the player? IV: Dealing with parameters Parameters are what the player will type after the command so that the script can use them. Not all commands will require parameters but when they do, you'll need something to discern if the player's typed any parameters after the command, what those parameters are, how many there are, etc. This is what sscanf will help us do. Here's an example command where the player needs to type in a number to set their health to a specific value: PHP Code: CMD:sethealth(playerid, params[]) When the player types /sethealth, the script will see that no parameters have been typed in and will give a message to the player on how to use the command. The same will happen if the player was to type something like `/sethealth f` as its looking for a decimal number. Once the player types in a number, the script will see that and (assuming there's no other parameters to deal with) continue with the command with the information given by the player. If the player typed `/sethealth 100`, it will set their health to 100, if they typed `/sethealth 1`, it will set their health to 1 and so on. V: Further Examples For the final section, I'm going to leave a few basic commands that do various things to give an idea of how creating a command goes. If there's something I'm doing that I haven't covered earlier, it will be mentioned in the examples. a. /freezeplayer PHP Code: CMD:freezeplayer(playerid, params[]) b. /givedeagle PHP Code: CMD:givedeagle(playerid, params[]) c. /setserverhour PHP Code: CMD:setserverhour(playerid, params[]) d. /setmoney PHP Code: CMD:setmoney(playerid, params[]) This concludes the tutorial. I've never really written anything like this so feedback is appreciated, I hope one of you learns something from this! RE: How to Create Basic Commands - Gravityfalls - 2019-04-18 The code looks worse the way you write it because you use brackets just for a single "return" statement and leave absolutely no spacing in between. PHP Code: CMD:freezeplayer(playerid, params[]) { RE: How to Create Basic Commands - kristo - 2019-04-18 (2019-04-18, 08:05 AM)Alaska Wrote: The code looks worse the way you write it because you use brackets just for a single "return" statement and leave absolutely no spacing in between. From a post I wrote a while back: Quote:Not using braces around single line statements is a bad habit that can make your code more prone to human errors. You might be interested in reading about Apple's gotofail bug. The PAWN style guide by Southclaws states the following:? However you are correct about having no spacing, I'm a sucker for whitespace myself. The tutorial itself... tl;dr for now, but it seems okay at the first glance. You might want to use [php] tags instead of [code] tags and use Courier New font for inline?code?instead of surrounding them with backticks (``) though. RE: How to Create Basic Commands - DTV - 2019-04-18 I updated the thread, thanks for the feedback :) |