Server, Client, and Say Commands

Post Python examples to help other users.
User avatar
satoon101
Project Leader
Posts: 2295
Joined: Sat Jul 07, 2012 1:59 am

Server, Client, and Say Commands

Postby satoon101 » Tue Jan 15, 2013 6:05 am

I just added a new update to the command structure. Previously, the Python-side API only allowed the used of decorators to register/unregister the commands. With this update, you can now use a registration class (which the decorators now tie into) to register your commands:

  • ServerCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [description]
      4. [flags]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.server import ServerCommandRegistry

      def server_command_callback(CCommand):
      print('Test command just called')

      ServerCommandRegistry.register_command('test_command', server_command_callback, 'Test command')

      def unload():
      ServerCommandRegistry.unregister_command('test_command', server_command)
  • ClientCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import ClientCommandRegistry

      def client_command_callback(edict, CCommand):
      print('Test client command just called')

      ClientCommandRegistry.register_command('client_test', client_command_callback)

      def unload():
      ClientCommandRegistry.unregister_command('client_test', client_command_callback)
  • SayCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import SayCommandRegistry

      def say_command_callback(index, teamonly, CCommand):
      print('Test say command just called')

      SayCommandRegistry.register_command('say_test', say_command_callback)

      def unload():
      SayCommandRegistry.unregister_command('say_test', say_command_callback)

With ClientCommandRegistry and SayCommandRegistry, you can also register filters (though client command filters has not been added, as of yet):

Syntax: Select all

from commands.client import ClientCommandRegistry
from commands.say import SayCommandRegistry

def load():
ClientCommandRegistry.register_filter(client_command_filter)
SayCommandRegistry.register_filter(say_filter)

def client_command_filter(edict, CCommand):
print('Client Command Filter')

def say_filter(index, teamonly, CCommand):
print('Say Filter')

def unload():
ClientCommandRegistry.unregister_filter(client_command_filter)
SayCommandRegistry.unregister_filter(say_filter)


You can also, of course, use the decorators to register your commands/filters, and they will automatically be unregistered when you unload your script:

Syntax: Select all

from commands.client import ClientCommand
from commands.client import ClientCommandFilter
from commands.say import SayCommand
from commands.say import SayFilter
from commands.server import ServerCommand

@ClientCommand('client_test')
def client_command_test(edict, CCommand):
print('Client Command Test')

@ClientCommandFilter
def client_command_filter_test(edict, CCommand):
print('Client Command Filter Test')

@SayCommand('say_test')
def say_command_test(index, teamonly, CCommand):
print('Say Command Test')

@SayFilter
def say_filter_test(index, teamonly, CCommand):
print('Say Filter Test')

@ServerCommand('server_test')
def server_command_test(CCommand):
print('Server Command Test')
Again, client command filters are not implemented, so that portion of the code above will probably raise an error. All of the rest should work perfectly fine.

If you notice any bugs, have any questions/comments, please feel free to post them.

Satoon

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest