• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Stragen

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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:
    void addCommand( Command* command )
    {
        _commands.push_back( command );
    }
    void executeCommands()
    {
        for( auto command : _commands )
        {
            command->execute();
        }
    }

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

// somewhere in your code
Command* command = new MoveCommand( someActor, someX, someY );
commandHandler.addCommand( command );

// 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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0