Followers 0

# Event/Action Management

## 8 posts in this topic

I'm currently considering how i might approach a parameter based event management system.

My aim is to build it such that the actions that are executed on a particular event are able to be dynamic. This would essentially extend the event system that is used for input control, however my aim is to use a lot less case statements.

My initial thought was to build up a couple of string based maps so that parameters may be allocated against events, for example:

map<string,string> actionMap;

map<string,function> FunctionMap;

actionMap["onCollision"] = "doDestroy"

FunctionMap["doDestroy"] = <pointer reference to function to execute for destroying an object>

allowing for other parts of the code to call,

FunctionMap[ActionMap["onCollision"]](<object reference>);

I've already encountered some pitfalls with this approach, particularly the catches with using function pointers, and void* variables, and was curious to see what other people were doing in this kind of case?

Is there any real advantage in doing this compared to say, case statements against variables? Noting I'm hoping to have a few thousand objects at a time.

0

##### Share on other sites

In my projects I usually write event subclass for every different case with base properties in base class and event specific properties in subclasses. This is the typical OOP that can save you from hours of debugging looking for error in your dynamic events, however you have to spend more time implementing different classes. You can easily go with only one dynamic class with maps etc.. but that in future can create a messy code with lots of constants for parameter names and huge potential for bugs. I guess this is a personal choice, both options are valid.

2

##### Share on other sites

Thanks for the responses so far... attempting to digest them.

I'm liking the look of std::function and was surprised that I've not seen that before... is this new with c++11?

1

##### Share on other sites

I'm not exactly a veteran coder, so take what I say with a grain of salt, but it sounds like you might be interested in the Command pattern: http://en.wikipedia.org/wiki/Command_pattern

Essentially, instead of passing around function pointers that take in void* parameters, you encapsulate the function calls in an object with a common interface. For example, in an RTS-like game:

Disclaimer: didn't test this code

class Command
{
public:
virtual ~Command()
{}

virtual void execute() = 0;
}

class MoveCommand : public Command
{
public:
MoveCommand( GameActor& actor, double goalX, double goalY )
:   _actor( actor ),
_goalX( goalX ),
_goalY( goalY )
{}

~MoveCommand()
{}

void execute()
{
// do whatever sophisticated move logic you need here
// probably something better than this.
_actor.moveBy( _goalX - _actor.getX(), _goalY - _actor.getY() );
}

private:

// store whatever data you need for this type of command
GameActor& _actor;
double _goalX;
double _goalY;
}


Then, you can create commands somewhere and pass them into a CommandHandler class that executes them whenever you need.

class CommandHandler
{
...
public:
{
_commands.push_back( command );
}
void executeCommands()
{
for( auto command : _commands )
{
command->execute();
}
}

private:
std::vector<Command*> _commands;
}

Command* command = new MoveCommand( someActor, someX, someY );

// later
commandHandler.executeCommands();



Also, it would probably be better if instead of using strings in your actionMap and functionMap, you used enums instead. That way the compiler will detect invalid keys so you don't get burned by typing "onDestory" somewhere in your code, which could end up being a difficult bug to track down.

Edited by Kovaz
0

##### Share on other sites
The use of String at the moment is more of a 'first cut' than anything else. I've done some 'safety testing' and it appears the way this has been implemented is that simply putting in a line that says "onDestroy" without parenthesis will simply do nothing (will be undefined variable, or invalid call of function) and to call it from the funcmap you have to actively call the funcmap to do the call, there's no magic pointer at a global level. I take the point about the invalid key though, there is no error checking at the moment to ensure that the key being used actually exists in the maps, other than the map security itself, and if a non-mapped key is used it does error out. That is my next step (people say i work backwards), the aim at this point was to create the ability to reference a function from a map and pass dynamic variables.
0

##### Share on other sites

Little off-topic but is very rare the use of events in a well structured game. They are just IDs that you pass to objects instead of calling the object function. My recommendation is that you try to solve your problem without the use of events. If you're thinking that input are events they aren't. Search on the forums and you will see. Using strings to declare an event is the worst approach I think. Since all objects can know about the event, why give it a name instead of a constant ID? Games aren't that Java Application that use events for everything, they're real time simulations.

0

## Create an account

Register a new account