What are best practices to add gameplay, to specific level? I have some event types, and my game logic now looks like. OnEnterSensor is virtual and is different for each level(01,02 ...). Game type platformer and what about other types of games and their logic description.

    virtual bool OnEnterSensor( Entity* sensor, EventArgs* args )
      if (str_equal(sensor->Name(), "button_sensor"))
        door_closed = false;//door_closed internal level variable
      else if (str_equal(sensor->Name(), "door_sensor")) ........ and so on.

Is it good practice to use such kind of code. I'm not sure about this long IF construction when level logic is complex then it becomes large, and hard to read. Will be glad to hear any recommendations (maybe some in-level events) and other methods to describe level gameplay.


The data you loaded and parsed can be accessed just as you've indicated, using standard conditional logic. If you're looking for conciseness, there are other ways to do conditional logic:

  • switch keyword is one (found in most imperative-style languages), while the ?: operators are another. Neither is much more elegant than if, except for small sets of conditionals.
  • There is a more elegant way that takes two forms: function pointers (also known as "functors" or in C#, "delegates"), or the structural equivalent frequently used in OO languages, the Strategy Pattern.

Function pointers are simple:

   //do something

   //do something totally different

run(bool a)
   function myFunc;
   if (a)
      myFunc = boo;
      myFunc = foo;

   myFunc(); //we use this in place of an if statement + resultant logic. It's a "strategy" that's been assigned based on the value of something else.

The Strategy Pattern is really just a way to package up the same mechanism into classes, where one or another problem with the language makes it hard or impossible to do the assignment and/or direct function call on myFunc, above. It's purpose is "to allow an algorithm to be selected at runtime". The "algorithm", in this case, is exactly what you have between the braces of your if statements. As for nesting, yes you can use function pointers within calls to other pointed-to-functions. Same goes for Strategy Pattern, it can operate at many different, nested levels.

As an aside...

You have two choices at the higher level as to when to handle changes to the game world:

  • What you're doing above, which is to respond to events
  • Polling: On every game update, you simply re-run all of the logic that looks at the level data (as you're doing above) and makes changes to your game's data model as a result.
  • \$\begingroup\$ Could you show me some articles, examples, please? \$\endgroup\$ – Yevhen Dec 21 '11 at 16:46
  • \$\begingroup\$ @bobenko Examples given above. Here's an article outlining pretty much exactly what I've just explained. Also see this answer. \$\endgroup\$ – Engineer Dec 21 '11 at 17:15
  • \$\begingroup\$ All my game objects are parsed from XML(I don't have problem with it), and xml is generated in MapEditor. Thanks for reply, but maybe you have misunderstood my question or I your answer. You said "and now you use that to perform conditional logic at runtime" - how to do it? the way I wrote in code sample, or some other better way. \$\endgroup\$ – Yevhen Dec 21 '11 at 17:33
  • \$\begingroup\$ @bobenko Amended. \$\endgroup\$ – Engineer Dec 21 '11 at 18:18
  • \$\begingroup\$ Answer is rather general, but usage of strategy pattern is good idea, I'll mark it as answered. \$\endgroup\$ – Yevhen Dec 23 '11 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.