Creating Commands

From SA-MP Wiki

Jump to: navigation, search

Contents

Introduction

This tutorial will first teach how to make basic commands using the strcmp(string compare) function, then get onto more advanced command structures (e.g. parameters, dcmd).

Commands are possibly the most used feature for user interaction in PAWN scripts, so being able to create your own command is really a nice asset when you create your own script. They can be created using the OnPlayerCommandText callback, which is triggered when someone enters chat that has the forward slash ('/') character first.

Tutorial

Basic Commands

An example of a basic command is shown below. This command will display the text "Hello Player!" in the chatbox to the player who typed the command.

public OnPlayerCommandText(playerid, cmdtext[])
{
     if(!strcmp(cmdtext, "/mycommand")) 
     {
        SendClientMessage(playerid, 0xFFFFFFFF, "Hello Player!");
        return 1;
     }
     return 0;
}

This command works when OnPlayerCommandText is called. When it's called the 'playerid' and 'cmdtext' are given as 'parameters', playerid is the 'player' who typed the command, and 'cmdtext' is a string with the command they entered, including the forward slash ('/').

Usage of the 'if' statement

Then an 'if' statement will use the function strcmp to check if the command entered by the player ('cmdtext') matches the text '/mycommand', if it does then the code in between the braces ('{' and '}') will be used/ran/executed.

Usage of 'return'

After the 'SendClientMessage' is 'return 1;', this stops the script after everything has been executed. 'return's always go at the end of a block of code (in most cases, but that's more advanced).

Basic Teleport Command

An example of a basic command is shown below. This command will Teleport the player who typed the command.

public OnPlayerCommandText(playerid, cmdtext[])
{
     if(strcmp(cmdtext, "/myteleport") == 0) 
     {
         SetPlayerPos(playerid, -1967.8365, 2956.9823, 12.9375);
         return 1;
     }
     return 0;
}

To get your X, Y, Z co-ordinates for teleports, use /save in game, for more info have a look on the Debug Guide

Parameter Commands

Commands can have parameters, this means data entered by the player who typed the command, such a playerid or string

Take for instance a '/kick' command, you have to enter the id of the player you want to kick like: '/kick 23'

There are several ways of doing this, the most common is DCMD combined with sscanf, another is Strtok (but this is a very slow and inefficient way) If you just simply want to make a parameter command in OnPlayerCommandText using Sscanf then try this:

Simple code

Put this code at the top of OnPlayerCommandText

public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[30], params[30];
	sscanf(cmdtext, "ss", cmd, params);
... your command checks ...
}

What this does is create two variables at a suitable size [256 is NOT a suitable size!] then uses Sscanf to split the entered command into two separate strings, the command ['cmd'] and the parameters ['params']. You then just use 'cmd' in your if statement to check the command:

Single Parameters

if(!strcmp(cmd, "/mycommand"))
{
	//Code
	return 1;
}

Then you can use the 'param' variable to get data such as playerids, text and more.

if(!strcmp(cmd, "/mynameis"))
{
	new str[];
	format(str, 3, "Welcome to SA:MP %s", params);
	SendClientMessage(playerid, 0xFFFFFFFF, str);
	return 1;
}

That example uses Format to send a message with the text the player entered after the command. So if you typed '/mynameis bob' you would see a message saying: "Welcome to SA:MP bob"


Multiple Parameters

You can have more than one parameter in a command by using Sscanf in the command check statement This second usage of sscanf will split the 'params' variable into it's separate data. So you can have a command with enterable numbers and text

Example:

if(!strcmp(cmd, "/myinfo"))
{
	new n[24], a, c[40]; // n=name, a=age, c=favourite colour :D
	sscanf(params, "sds", n, a, c);
	//Now that the data in the command has been stored in the above variables, it can be used.
	return 1;
}

sscanf takes the params string, splits it into the three types of data it should be ["sds" s=string, d=integer]

But the user won't know how many parameters are after the command, so if they get it wrong [wrong type of parameter, wrong amount] Send a message to tell them how to use it.

This can be done with sscanf, if the variables in the function don't match the types of data in the string then sscanf will return 1 So using a little if statement with the sscanf function we can get the job of applying the variables and checking if they are right all in the same line:

if(!strcmp(cmd, "/myinfo"))
{
	new n[24], a, c[40];
	if(sscanf(params, "sds", n, a, c))return SendClientMessage(playerid, 0xFFFFFFFF, "How to use: '/myinfo [name] [age] [favourite colour]'");
	else
	{
		//Now that the data in the command has been stored in the above variables, it can be used.
	}
	return 1;
}

Under the if statement you can have other checks that relate to the entered data, for instance a kick command can't kick a player that isn't connected, so warn the player who typed it:

new id;
if(sscanf(params, "d", id))return SendClientMessage(playerid, 0xFFFFFFFF, "How to use: '/kick [playerid]'");
else if(!IsPlayerConnected(id))return SendClientMessage(playerid, 0xFF0000FF, "Invalid Player ID");
else
{
	Kick(id);
}
The line
else if(!IsPlayerConnected(id))
if the parameters are right [after the 'if(sscanf...' check] then this next line will make it's IsPlayerConnected check, if it returns 0 [hence the '!' which means 'opposite' if used in an if statement] then warn the user.

If the player is connected then there are no more checks, and the player is kicked.

Personal tools
Navigation
Toolbox