In this tutorial you will learn how to create commands, you are already familiar with it from tutorial №3, but you would have understood them fluently.
We need to import on_gamemode_init
and the module callbacks
. Since we will be working with commands, we import cmd
from commands
and the class Player()
.
from pysamp import on_gamemode_init, add_player_class
from pysamp.commands import cmd
from pysamp import callbacks
from pysamp.player import Player
I also suggest using add_player_class()
to keep your player from falling underground.
@on_gamemode_init
def your_function():
add_player_class(0, 134.0484, -67.3721, 1.5781, 265.4560, 0, 0, 0, 0, 0, 0)
We are done with the preparations, now let's start creating commands.
Let's start working with commands. They are very easy to create, we will create a couple of simple commands and you will fully understand how to create them.
To create a command, you have to write @cmd
and then create a function. I will create a simple command that will send a message to the player.
@cmd
def mycommand(player: Player):
player.send_client_message(-1, f"Hello, {player.get_name()}")
So this is what a simple command looks like, I think you have already guessed that you can write as many arguments as you want. Let's create a command /kickplayer
that will kick a player. You also need to import the kick()
and send_client_message()
functions.
from pysamp import kick, send_client_message
@cmd
def kickplayer(player: Player, playerid: int):
send_client_message(playerid, -1, "Kicked!")
kick(playerid)
It's also very simple. Now, what if you want to give a reason? To do that, we will write *reason
, after which it will be necessary to convert the tuple into a string.
@cmd
def kick(player: Player, playerid: int, *reason):
reason = " ".join(reason)
send_client_message(playerid, -1, f"You have been kicked. Reason: {reason}")
kick(playerid)
Now let's learn how to work with aliases
. To do this, let's write a simple command that will give health to the player.
@cmd(aliases=("health", "restoreHealth"))
def heal(player: Player):
player.set_health(100)
player.set_armour(100)
player.send_client_message(-1, "You have been successfully healed.")
As you can see, almost nothing changes, but now the command will be called not only on /heal
, but also on /health
and so on.
Now let's deal with use_function_name
, it's probably all clear here, but I'll just say that if use_function_name=False
then the command will not be called by the function name. You must also use aliases
if use_function_name=False
. Let's create a simple command that will issue weapons to the player.
@cmd(aliases=("guns", "weapon"), use_function_name=False)
def weapon_function(player: Player):
player.give_weapon(24, 500)
player.give_weapon(26, 500)
player.give_weapon(27, 500)
player.give_weapon(34, 500)
player.give_weapon(46, 1)
player.send_client_message(-1, "You have successfully obtained weapons.")
As you can see, the command will not be called if the player enters /weapon_function
in the chat.
In this tutorial you learned how to work with commands. Now you know the specifics of creating commands, and you also know the different ways to create them. In the next tutorial you will learn how to work with dialogs.