Game Development Community

dev|Pro Game Development Curriculum

Chat Commands

by John Smith \"ElectricSun\" · 12/02/2006 (3:41 pm) · 4 comments

Download Code File

in common/server/message.cs

add this to ChatMessageAll
if (getSubStr(%a2, 0, 1) $= "/")   // this here / is your conditional
	  chatcommands(%sender,%a2);
   if(getSubStr(%a2, 0, 1) !$= "/")
   {
    
    // the already existent loop for clients
  

   } // you just have to add this

make a file and call it ChatCommands.cs

function chatcommands(%sender, %message)
{
%cmd = getWord(%message, 0);    // get the first word
%cmd = stripChars(%cmd, "/");   // strip the conditional
%count = getWordCount(%message);// get all the arguments
%args = getwords(%message, 1);  // includes all the arguments
%cc = "cc" @ %cmd;              // all chatcommand functions have a cc
                      // conditional so someone can't call destroyserver().
call(%cc, %sender, %args, 0);   // then sends it all
}
                                // if someone says /kill PLAYERNAME
function cckill(%sender, %args) // this functions will murderize them
{
 if (!%sender.isadmin)                                                                       // if the sender isn't an Admin
 {          
     messageClient(%sender, 'MsgClient', 'You must be an Admin!');  // let them know so
     return;                                                                                          // then end this function
}
 
%target = plnametocid(%args);  // get the name out of the args sometimes names are more than one word so i didn't use getword()

 if (%target)                   // if PlnameToCid() returns the name as belonging to a client
 {
   %target.player.kill();       // KILL THE PLAYER
   messageAll('MsgAll', '%1 has been killed by the Admin', %target.name); // let everyone know so
 }
 else                          // if the name is wrong (mispelled/nonexistant)
 {
  messageClient(%sender, 'MsgClient', 'Invalid Target!'); // let the sender know so
 }
}

function plnametocid(%name)  // this function validates targeted players
{
    %count = ClientGroup.getCount(); // counts total clients
    for(%i = 0; %i < %count; %i++)  // loops till all clients are accounted for
        {
        %obj = ClientGroup.getObject(%i);  // gets the clientid based on the ordering hes in on the list
        %nametest=%obj.name;  // pointless step but i didnt feel like removing it....
        %nametest=strlwr(%nametest);  // make name lowercase
        %name=strlwr(%name);  // same as above, for the other name
        if(strstr(%nametest,%name) != -1)  // is all of name test used in name
            return %obj;  // if so return the clientid and stop the function
    }
    return false;  // if none fits return 0 and end function
}


- credit: whoever made it for T2's Construction Mod
(idk who, i just added it because, there was no Chat Command Tut)

Chatcommands.cs is at the top of the tut

About the author

Recent Blogs


#1
12/05/2006 (9:14 am)
(EDIT: fixed)
There will be problems with this block:

if (!%sender.isadmin)          // if the sender isn't an Admin
     messageClient(%sender, 'MsgClient', 'You must be an Admin!');// let them know so
     return;                    // then end this function

You need to add braces... otherwise it will always return, regardless of admin status.
#2
12/13/2006 (12:08 am)
thank you, i didn't even notice.
#3
12/10/2007 (9:15 am)
Will this stop for example /command being shown in the chat box and just do what the command is set to do?

If not, how can I stop any commands from being shown like normal chat?
#4
12/19/2007 (10:36 am)
having trouble with this but its probably because Im a noob at coding. You say:

in common/server/message.cs

add this to ChatMessageAll

if (getSubStr(%a2, 0, 1) $= "/") // this here / is your conditional chatcommands(%sender,%a2); if(getSubStr(%a2, 0, 1) !$= "/") { // the already existent loop for clients } // you just have to add this


But I dont know if I am adding it correctly... and the commands do not seem to do anything in game.
My chat chatMessageAll looks like the following:

function chatMessageAll( %sender, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 )
{
if ( ( %msgString $= "" ) || spamAlert( %sender ) )
return;

%count = ClientGroup.getCount();

for ( %i = 0; %i < %count; %i++ )
{
%obj = ClientGroup.getObject( %i );

if(%sender.team != 0)
chatMessageClient( %obj, %sender, %sender.voiceTag, %sender.voicePitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );
else
{
// message sender is an observer -- only send message to other observers
if(%obj.team == %sender.team)
chatMessageClient( %obj, %sender, %sender.voiceTag, %sender.voicePitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );
}
}
}


can you post an example of what this routine should look like after it is modified?
Any help appreciated. thanks