PDA

View Full Version : different ways to trigger !chat command


shanapu
03-24-2016, 23:28
hi,
i'm not familar with sourcepawn, i couldnd write a plugin from scratch. but i know how to read & edit sourcecodes. i read many sources recently and found 3 different ways to trigger/read a chat command.
What are the pros and contras of these ways to handle a chat command?

thanks in advance

shanapu


public OnPluginStart()
{

RegConsoleCmd("sm_test1", TestOn);

}

public Action:TestOn(client, args)
{
PrintToChatAll("Hello world")
}



public OnPluginStart()
{

HookEvent("player_say", PlayerSay);

}


public PlayerSay(Handle:event, String:name[], bool:dontBroadcast)
{

decl String:text[256];
GetEventString(event, "text", text, sizeof(text));
if (StrEqual(text, "!test2"))
{
PrintToChatAll("Hello world")
}
}



public OnPluginStart()
{

AddCommandListener(Event_Say, "say");
AddCommandListener(Event_Say, "say_team");

}

public Action:Event_Say(clientIndex, const String:command[], arg)
{
static String:menuTriggers[][] = { "!test3" };

decl String:text[24];
GetCmdArgString(text, sizeof(text));
StripQuotes(text);
TrimString(text);

for(new i = 0; i < sizeof(menuTriggers); i++)
{
if (StrEqual(text, menuTriggers[i], false))
{
PrintToChatAll("Hello world")
}
}
}

ddhoward
03-25-2016, 00:47
Unless you have a special reason not to (like the command already exists, and you're trying to block it with another plugin), you pretty much always want to use the Reg*Cmd natives to create commands. In addition to being simple and pretty much handled by Sourcemod itself, it also allows use of simple overrides.

Another advantage is that it supports the changing of the triggers without needing to edit the plugin. For example, some servers might set their silent trigger, which is / on most servers, to be something weird like \ or even $. Similarly, the non-silent trigger can also be changed.

shanapu
03-25-2016, 16:09
Unless you have a special reason not to (like the command already exists, and you're trying to block it with another plugin), ...

What are other special reasons? and what the advantage of using Method 2 & 3?

I understand the advantage of RegConsoleCmd but why i see "so many" plugins doesnt use regconsolecmd? cant explain it with cmd exist.

ddhoward
03-25-2016, 16:15
There is pretty much no reason to use method 2, ever.

Method 3 is only useful if the command already exists and you're trying to hook it, or you, for some reason, don't want to "register" an actual command.

Method 1 is the only sane option.

blaacky
03-25-2016, 17:32
I'd say only ever use method 2 or 3 if you ever have a reason to unregister a command. Because you can't remove commands that have been registered via RegConsoleCmd

shanapu
03-25-2016, 18:37
... or you, for some reason, don't want to "register" an actual command...

... if you ever have a reason to unregister a command. Because you can't remove commands that have been registered via RegConsoleCmd

What could be a reasons you want a unregistered cmd? im not fully through it.

blaacky
03-25-2016, 19:12
I've had to unregister commands before because in a large plugin I made some commands are created through a config file. If the server owner changes the words in the config file then it needs different commands then before, so I use something like method 3

Neuro Toxin
03-25-2016, 19:40
Ive hit this deregistering issue for similar reasons.

I worked around this by registering all my commands to one callback for processing.

When a command is no longer used i just ignore it.

ddhoward
03-25-2016, 20:20
http://ddhoward.bitbucket.org/scripting/include/validClient.inc
http://ddhoward.bitbucket.org/scripting/include/ddhoward_updater.inc

In these includes, which I made, exist "commands" which must be shared between all plugins that utilize the include. AddCommandListener seems more appropriate in this case. Further, the use of AddCommandListener causes the "command" to not appear in sm_searchcmd or sm_help, preventing cluttering up these lists.

shanapu
03-26-2016, 21:31
thanks for your feedback guys!