Sample Source Code

ChatBlade ships as C++ Source Code and includes full HTML API Documentation as well as an assortment of How To pages explaining how to implement ChatBlade in a game.

CBCommandMap is a class which encapsulates a single OnCommand() function pointer as well as the text Slash Commands which invoke it. CBCommandMap also contains a description of the contained Slash Command and an optional help string.
One instance of CBCommandMap is created for each OnCommandXXX() function, though each function can have multiple typed Slash Commands which map to it.

A CBCommandMap instance for the OnCommandStuck() function would contain a pointer to the static function CBSlashCommands::OnCommandStuck(), a description of the function, and the 4 typed commands which invoke the function.
    CBSlashCommands::OnCommandStuck
    Command Invoked when a user wants to free themselves from being Stuck on the terrain
    /STUCK
    /UNSTICK
    /UNSTUCK
    /EJECT

The API help for the CBCommandMap class can be found here.

A How To describing how to load up various Slash Commands via CBCommandMaps can be found here.

  
//  CBCOMMANDMAP.H

//  Header file for the CBCommandMap class which maps various / Commands to a single function
//  For example both /Send and /Tell may map to the OnCommandTell() function

//  The CBCommandMap class provides the functionality of allowing multiple user entered commands
//     to map to the same function
//  This allows for customization of the Slash Command user interface

//  When the user enters in a Slash Command ChatBlade shall cycle through all the CBCommandMaps
//  It shall look for a match for the / Command entered
//  If a match is found it shall call the pOnCommandFunction in that CBCommandMap to Invoke the
//     associated OnCommand() Function

#include <vector>
#include "CBCONSTANTS.H"

#ifndef  __CBCOMMANDMAP_H__
#define  __CBCOMMANDMAP_H__



//  Prototype for an OnCommand() function taking 2 parameters (the / Command, and the rest of
//    the line entered)
//  Functions matching this prototype are stored statically in the CBSlashCommands Class
typedef int ( *ONCOMMANDFUNCTION)(CBSTRING sSlashCommand, CBSTRING sCommandParameters);


//  Class that maps one OnCommand() function to various / Commands
class CBCommandMap
  {
  public:

  //  Constructor
  CBCommandMap();

  //  Clears out all data in this Command Map instance
  //  Sets pOnCommandFunction to NULL, and erases all Aliases
  int Clear();

  //  Set the function pointer of the OnCommand() function for this Command Map
  int SetPointer(ONCOMMANDFUNCTION pOnCommandFunctionPointer);

  //  Add in an Alias that shall map to this Command
  int AddAlias(CBSTRING sAlias);

  //  Function to tell if a string is an Alias for invoking the stored OnCommand() function
  //  Returns CB_TRUE if the string is in the m_vAliasArray and CB_FALSE if not
  //  Command is Case Insensitive
  int IsCommand(CBSTRING sCommand);

  //  Invokes the associated Command of this CommandMap
  int CallCommand(CBSTRING sCommand, CBSTRING sParameters);

  //  Sets the Description of the OnCommandXXX() function stored in this CBCommandMap instance
  int SetDescription(CBSTRING sDescription);

  //  Gets the Description of the Slash Command Function stored in this instance
  CBSTRING GetDescription();

  //  Sets the Help String of the OnCommandXXX() function stored in this CBCommandMap instance
  //  Used to describe to Players how to format and use the Slash Command
  int SetHelpString(CBSTRING sHelpString);

  //  Gets the Help String of the Slash Command Function stored in this instance
  CBSTRING GetHelpString();


  //  Data

  //  An optional discription of the OnCommandXXX() function stored in this CBCommandMap instance
  CBSTRING m_sDescription;

  //  An optional Help string showing usage information for the OnCommandXXX() function contained
  //    in this CBCommandMap instance
  CBSTRING m_sHelpString;

  //  A vector of all the Command Aliases for this Function
  std::vector <CBSTRING> m_vAliasArray;

  //  Pointer to the OnCommand() function that the Command Aliases in this CommandMap map to
  //  Typically shall be / Commands such as OnCommandTell() or OnCommandQuit()
  ONCOMMANDFUNCTION pOnCommandFunction;

  };



# endif  //  __CBCOMMANDMAP_H__


//  COPYRIGHT FLEX DOLPHYNN
//  ChatBlade License required.  Permission to use granted as per licensing agreement.
  
  
//  CBCOMMANDMAP.CPP

//  Source file for the CBCommandMap class which maps various / Commands to a single function
//  For example both /Send and /Tell may map to the OnCommandTell() function

//  The CBCommandMap class provides the functionality of allowing multiple user entered commands
//     to map to the same function
//  This allows for customization of the Slash Command user interface

//  When the user enters in a Slash Command ChatBlade shall cycle through all the CBCommandMaps
//  It shall look for a match for the / Command entered
//  If a match is found it shall call the pOnCommandFunction in that CBCommandMap to Invoke 
//    the associated OnCommand() Function
  
#pragma warning(disable:4786)

#include "CBCOMMANDMAP.H"
#include "CBUTILS.H"


//  Constructor
CBCommandMap::CBCommandMap()
  {
  pOnCommandFunction = NULL;
  }




//  Clears out all data in this Command Map instance
//  Sets pOnCommandFunction to NULL,  and erases all Aliases and Help strings
int CBCommandMap::Clear()
  {
  pOnCommandFunction = NULL;
  m_vAliasArray.clear();
  m_sDescription.clear();
  m_sHelpString.clear();

  return(CB_SUCCESS);
  }




//  Function setting our stored pointer to an OnCommand() function
int CBCommandMap::SetPointer(ONCOMMANDFUNCTION pOnCommandFunctionPointer)
  {
  int nReturnValue = CB_SUCCESS;

  //  Make sure the pointer is not NULL
  if(pOnCommandFunctionPointer == NULL)
    {
    nReturnValue = CB_ERROR_NULL_POINTER;
    }
  else  //  Pointer not NULL
    {
    pOnCommandFunction = pOnCommandFunctionPointer;
    }

  return(nReturnValue);
  }




//  This function should be called to add in a / Command string which shall map to an
//    OnCommand() type function
//  Multiple Commands can all map to a single function
//  Care should be taken that the same / Command string does not map to more than 1 OnCommand()
//    type function
//  Commands are stored in UPPERCASE and are case insensitive
//  This Function is typically called from CBClientImplementation::LoadSlashCommands() and other
//    Locations
int CBCommandMap::AddAlias(CBSTRING sAlias)
  {
  //  Make sure there is text
  if(sAlias.size() < 1)
    {
    return(CB_ERROR_EMPTY_STRING);
    }

  //  Make sure that the Command starts with a Slash character (CB_SLASHCOMMAND_CHARACTER)
  //    adds it if it does not
  CBUtils::CBStringEnsureInitialSlash(sAlias);

  //  UPPERCASE the string
  CBUtils::CBStringToUpper(sAlias);

  //  Add it to our vector
  m_vAliasArray.push_back(sAlias);

  return(CB_SUCCESS);
  }




//  Function to tell if a string is an Alias for invoking the stored OnCommand() function
//  Returns CB_TRUE if the string is in the vAliasArray and CB_FALSE if not
//  Command is Case Insensitive
int CBCommandMap::IsCommand(CBSTRING sCommand)
  {
  int la;  //  Loop variable

  //  UPPERCASE the string
  CBUtils::CBStringToUpper(sCommand);

  //  Search through the list of Aliases
  for(la = 0; la < (int)(m_vAliasArray.size()); la++)
    {
    //  Compare the strings
    if(m_vAliasArray[la].compare(sCommand) == 0)
      {
      //  Found a match
      return(CB_TRUE);
      }
    }

  //  No match if we got here
  return(CB_FALSE);
  }




//  Invokes the associated Command of this CommandMap
int CBCommandMap::CallCommand(CBSTRING sCommand, CBSTRING sParameters)
  {
  //  Make sure the function pointer is not NULL
  if(pOnCommandFunction == NULL)
    {
    return(CB_ERROR_NULL_FUNCTION_POINTER);
    }

  return(pOnCommandFunction(sCommand, sParameters));
  }




//  Sets the Description of the OnCommandXXX() function stored in this CBCommandMap instance
int CBCommandMap::SetDescription(CBSTRING sDescription)
  {
  m_sDescription = sDescription;

  return(CB_SUCCESS);
  }




//  Gets the Description of the Slash Command Function stored in this instance
CBSTRING CBCommandMap::GetDescription()
  {
  return(m_sDescription);
  }




//  Sets the Help String of the OnCommandXXX() function stored in this CBCommandMap instance
//  Used to describe to Players how to format and use the Slash Command
int CBCommandMap::SetHelpString(CBSTRING sHelpString)
  {
  m_sHelpString = sHelpString;

  return(CB_SUCCESS);
  }




//  Gets the Help String of the Slash Command Function stored in this instance
CBSTRING CBCommandMap::GetHelpString()
  {
  return(m_sHelpString);
  }








//  COPYRIGHT FLEX DOLPHYNN
//  ChatBlade License required.  Permission to use granted as per licensing agreement.
  

Home
Features
FAQ

ChatBlade Copyright © Flex Dolphynn