Page 1 of 1

Client Commands

Posted: Wed Nov 28, 2012 9:12 pm
by ashbash1987
Since you've all been waiting for it, here's the first pass for Client Commands! It's now in the latest source code revision. Here's a quick sample I whipped up:

Syntax: Select all

# ../commands/commands.py

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
from Source import ClientCmd

# =============================================================================
# >> COMMAND CALLBACKS
# =============================================================================
def command_callback(entity, command):
print("I am command callback 1. I have %d arguments: %s" % (command.ArgC(), command.ArgS()))
return ClientCmd.ClientCommandReturn.BLOCK

def command_callback2(entity, command):
print("I am command callback 2. I have %d arguments: %s" % (command.ArgC(), command.ArgS()))
return ClientCmd.ClientCommandReturn.CONTINUE

def command_callback3(entity, command):
print("I am command callback 3. I have %d arguments: %s" % (command.ArgC(), command.ArgS()))
return ClientCmd.ClientCommandReturn.CONTINUE

def jointeam_callback(entity, command):
print("I am a jointeam callback. I have %d arguments: %s" % (command.ArgC(), command.ArgS()))
return ClientCmd.ClientCommandReturn.CONTINUE

my_cmd = ClientCmd.GetClientCommand("my_command")
jointeam_cmd = ClientCmd.GetClientCommand("jointeam")

# =============================================================================
# >> ADDON LOAD/UNLOAD
# =============================================================================
def load():
"""
Called upon addon load.
"""
my_cmd.AddToEnd(command_callback)
my_cmd.AddToEnd(command_callback2)
my_cmd.AddToStart(command_callback3)
jointeam_cmd.AddToEnd(jointeam_callback)

def unload():
"""
Called upon addon unload.
"""
my_cmd.Remove(command_callback)
my_cmd.Remove(command_callback2)
my_cmd.Remove(command_callback3)
jointeam_cmd.Remove(jointeam_callback)

As you can see, the structure is very much like ConCommands, except the callbacks are called with 2 parameters: an entity and a command argument structure. Note here that I've also hooked into pre-existing client commands ('jointeam' in this example), and that I can block execution of the client command if I wish to with .BLOCK (as a quick example off the top of my head, think a class limiter in TF2 which would .BLOCK execution of 'joinclass' if there are too many of a certain class, or .CONTINUE if ok). Blocking a client command will stop execution of that command further down the queue and since this executes before being passed further down into the engine, it would prevent the final server-based action arising from the client command to execute.

Have a play with it, see what you can come up with :)

As an aside, 'say'/'say_team' doesn't work with this, as this is not client command that can be hooked this way, so say commands are currently not ready.

Posted: Thu Nov 29, 2012 1:04 am
by satoon101
Awesome, Ash!!! Can't wait to test this out and get the Python API written!!

Satoon

Posted: Thu Nov 29, 2012 3:10 pm
by Omega_K2
Nice :D

For now at the very least you can do "public" say commands though player_say event, so that's not much of an issue.

On another note, was the problem with crashing fixed for server commands when you passed object methods (ex. myobj.mymethod) instead of normal methods (or classes with __call__) ? The ticket I submitted on SP was never closed nor I can renember seeing this in the changelog being fixed, but I might have overlooked it.

Posted: Thu Nov 29, 2012 5:53 pm
by Tuck
does this mean we can start getting menu selection from clients? :)