Thread: Event programming in games

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Event programming in games

    This is not problem, just a gerneral question. I was wondering if event programming is handled in the game engine?

    If this is so, does it call a certain function to create the event when a condition is met? Or is it all stored in a serpertate file away from the game engine then all linked together

    My thinking is possibly way off, but Im really intreeged to know how a game program handles events

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This is a complex question and there are a million ways to handle events. We are doing it via messages sent from the script class via a C structure to a message router class which then sends the message to ScriptMsgProc in either the engine or the object manager class based on the numeric range the message ID falls into.

    ScriptMsgProc then attempts to find a handler in a vector of handlers. Once it determines a handler does exist, it calls OnScriptMsg to then convert the handler from void (void) to the correct return type and param list for the function. Each type of handler function has a function signature that is stored in an enum. The function prototypes are stored in a union and we use the enum with the union to arrive at the final function prototype. If the message does not have a handler we then call the base class Default() which will attempt to handle the message, but we also flag an error condition stating that a message was sent from the script to an object that did not have a handler for the message. If Default() cannot perform the script command to script handler function resolution then an error message is returned to the script indicating the message was not handled.

    To respond to script messages you simply use DECLARE_SCRIPT_MSGMAP() in the class declaration. The class must be derived from CBaseObject in order to work correctly. Then in the implementation file you do this:

    Code:
    #include "CThisObject.h"
    
    DEFINE_SCRIPT_MSGMAP(CThisObject,CBaseObject)
      ON_SCRIPT_CMD0(ID_ISALIVE,IsAlive)
      ON_SCRIPT_CMD1(ID_GOTOWP,GoToWaypoint)
      ON_SCRIPT_CMD2(ID_ATTACKUSING,AttackUsing)
    END_SCRIPT_MSGMAP()
    
    ....
    bool CThisObject::IsAlive(void)
    {
      if (m_iHealth>0) 
      {
         return true;
      } else return false;
    }
    
    UINT CThisObject::GoToWaypoint(UINT nWPListID)
    {
       //Resolve id to list ptr
       CWaypointList *pList=m_pWPListMgr->GetList(nWPListID);
    
       if (!pList) return WAYPOINTS_NO_EXIST;
    
       CWaypoint pt;
       pList->GetWaypoint(0,&pt);
    
       float diffx=pt.x-m_vecPos.x;
       float diffy=pt.y-m_vecPos.y;
    
       float length=D3DXVec2Length(m_vecPos-pt);
    
       diffx/=length;
       diffy/=length;
    
       m_vecMotionVector.x=diffx;
       m_vecMotionVector.y=diffy;
      
    }
    
    ...
    The actual internal workings of CBaseObject and ScriptCmn.h are quite complex but that is the jist of it. We did not invent this system but modified the message system that MFC employs.
    Last edited by VirtualAce; 09-27-2006 at 06:42 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    1
    The simple answer is yes.
    How you do that is a complicated ordeal and Bubba's is a good example of this.

    If you have thought you might handle this with threads I have done a lot of research into that and it really is a bad idea for most games. Multi-Threading is powerful but it requires a lot of excess code just to keep the threads synchronized and that tends to cost more in processing time and speed than just handling the events inside one single thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Violent video games?
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 04-26-2006, 01:43 PM
  3. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM