event horizon - lua/c++ event system
- an event system useful for games (especially with a lua scripting component)
- event notification mechanism compatible with both lua and c/c++ method callbacks
- each event notification can deliver a packet of information (a data payload)
that may be used as contextual data for the event.
- this payload mechanism brings much variability to each individual event type
- event types are defined as fast memory-aligned short-strings (128 bit), and is up to the user to uniqify
the eventsystem operates with a command metaphor.
for each event, there is a data packet payload that travels alongside.
we can think of the event as the "command", and the payload as the
"arguments" to the command.
For our examples here, our payload is called "TestStruct", which is simply
a c++ struct containing the arguments for the command:
// we'll fill in the details of this struct later...
arg1, arg2, my3rdArg;
in c++, events are triggered like this:
trigger( "fire", TestStruct( 112358, 3.14159265f, "pi is good to taste, please use enough cornstarch" ) );
in lua, events are triggered like this:
trigger( "fire", 987654321, 0.112358, "cake is for feasting, 33 times this morning" )
To recieve event notification upon an event trigger, we register a callback:
c++ callbacks have this signature, and may be global or member functions:
void trig( const void* arg )
// NOTE: inside, you then cast the "arg" to the payload
// struct containing the list of args:
const TestStruct* args = (const TestStruct*)arg;
lua callbacks have this signature, and deals with real arguments directly:
function trig( arg1, arg2, my3rdArg )
Registration of a callback is supplied with:
1.) a function
2.) a description of the arguments.
c++ callbacks are registered like this:
reg( "fire", EventCallback( &trig, TestStruct::getdef() ) );
lua callbacks are registered like this:
reg( "fire", trig ) --description supplied by TestStruct in c++
we can also register [obj,member] callbacks in both c++ and lua:
reg( "fire", EventCallback( &obj, &MyClass::trig, TestStruct::getdef() ) );
reg( "fire", trig, obj )
finally... you're probably wondering about getdef()
getdef() is what enables the magic to happen to translate arguments from lua->c++ and c++->lua
Only basic types (int, bool, float, string) are supported as members.
TestStruct( int b, float f, const char* o )
beans = b;
frys = f;
safeStrCpy( oompas, o, sizeof( oompas ) );
/// describe the data packet
static packet& getdef()
static packet o =
packet_( "TestStruct" )
.add( "beans", &TestStruct::beans )
.add( "frys", &TestStruct::frys )
.add( "oompas", &TestStruct::oompas );
Thanks to Don Clugston's awesome FastDelegate code for genericizing
function pointers of any kind.
event horizon - lua/c++ event system demonstration
Copyright (c) 2006 kevin meinert all rights reserved
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA