Aspect Oriented Programming

Discussion in 'Game Development (Technical)' started by Jesse Aldridge, Jun 21, 2007.

  1. HairyTroll

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    582
    Likes Received:
    0
    Using Context oriented programming, multiple inheritance would be used to add all 9 behaviours (mixins) to a class. At runtime the class is instantiated as an object. Then these behaviour mixins are activated and de-activated for that specific object as needed (The multiple inheritance for the object is modified at runtime). So for example, at one point in the game perhaps 6 of the 9 mixins are active. At another point in the game perhaps only a single mixin is active. In addition the programmer is able to specify method combinations using before, after and around specifiers. Thus telling the OO system to call certain behaviours before, after or around other behaviours.

    The reason I said that it becomes as efficient as "normal OO programming" is that the hoops that you describe above are not required. Everything is handled by the existing OO machinery of the environment meaning the programmer doesn't have to code all these additional patterns.
     
    #21 HairyTroll, Jun 24, 2007
    Last edited: Jun 24, 2007
  2. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    Then how do you code a "behaviour group" that combines two behaviours that react to the same event? For example a behaviour that once the entity collides with some other entity (like a bullet to a monster) gives the player some score points and a behaviour that explodes the entity? Both of these behaviours need to react at the same event (collision with entity). How do you inform both for the event?

    In C++ you cannot have a class that inherits two base classes with the same function (actually you can, but when you call that function, the compiler will produce an error because it doesn't know which class' function to call - and you want to call both). And in C++'s case things are somewhat easy because with some voodoo coding (having these classes derive from another class that provides a callback mechanism and using a registry that accept this base class with an event mask for the events you're interested) you can slip through these. But what about languages, such as Java, which do not support multiple inheritance?

    With simple inheritance, things are easy. I've done it in Square Shooter - the red bullets have the same "logic" (role) as the black bullets - their role class is derived from the black bullet role class and they just change the collision reaction code a bit to allow them to go through the enemies.
     
  3. Jesse Aldridge

    Original Member

    Joined:
    Apr 19, 2005
    Messages:
    221
    Likes Received:
    0
  4. HairyTroll

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    582
    Likes Received:
    0
    That is one of the problems that Context orientated programming solves. That paper (< 10 pages) describes an implementation of Context orientated programming in Lisp, but don't let that put you off. It is definitely worth a read especially as you are one to create your own languages.
     
  5. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    Thanks for the PDF, i'll read it at some point later. Although to be honest, i never liked the lisp family of languages (even if i wrote an interpreter for a list-like language myself :D).
     
  6. MedievalElks

    MedievalElks New Member

    Joined:
    Jul 25, 2006
    Messages:
    81
    Likes Received:
    0
    Classic mixin-inheritance is different than classic IS-A inheritance. Mixin classes are those that generally don't stand on their own, but rather provide functionality when added to others. The canonical example is a Lockable class that provides locking semantics to say, a Semaphore, or Thread, or FileHandle, etc.

    Mixins in C++ are often implemented as so-called "abstract subclasses":

    template<class Base> class Lockable: public Base { ... };

    Lockable<Thread> lockable_thread; // Lockable semantics added to Thread

    You could do the same with composition, but then you'd have to add a Lockable to every class that needed the functionality instead of adding it dynamically. Same thing if you inherit directly from Lockable - it's done statically in the class definition.
     
  7. mot

    mot
    Indie Author

    Joined:
    Oct 21, 2004
    Messages:
    439
    Likes Received:
    0
    I'd use signals and slots in this case...

    But sooner or later, the exploding behaviour mentioned above will have to add different colors of sparks depending on the enemy's skin and the player will only be awarded points when the enemy has no armor or is underwater or is standing very close to the barrel of the gun.

    At that point, you need to change things to award points depending on the origin and target of the shot, the size of the explosion, splash damage, maybe add points when a piece of debris from the explosion caused by the shot hits the enemy etc. And most of the elegance quickly disappears.

    It is easier to hard-code it than build an overblown support infrastructure that would allow an elegant solution one layer above.

    But this all depends, of course. If you have a fixed design and know it's not going to get crazily complicated later on, it's definitely possible to find a way to do it pretty nicely using one of the methods mentioned...

    In Java I have found it quite powerful to use anonymous inner classes.

    Like this:

    Code:
    Bullet buildTimedExplodingBullet() {
      return new AbstractBulletBase() {
        public void getLifetime() {
          return 3; // -1 for infinite
        }
        public void onTimeout() {
          explodeSmall();
        }
        public void onHit(Actor a, Point pos) {
          explodeBig();
        };
    }
    and use buildTimedExplodingBullet() (or a Factory class if needed) in all guns that shoot this kind of bullets; use this method recursively to create new kinds of explosions...
     
  8. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,858
    Likes Received:
    2
    But when you read a novel, you know it's not going to crash in someone elses code!

    I think all this talk of which paradigms are best etc is pretty irrelevant tbh. I have no clue how anyone would classify the AI engine in my latest game, and yet it still seems to work. I classify it as "finished AI Code"
     
  9. mot

    mot
    Indie Author

    Joined:
    Oct 21, 2004
    Messages:
    439
    Likes Received:
    0
    And to comment on the original topic, the AspectJ toolkit specifically, while nicely integrated into Eclipse and all, is more a burden than help.

    The problem is that it HIDES things from me. While I'm looking at a piece of code and wondering why it's not working, it hides the bits that get injected into different parts of the code, here and there, and there's no clear connection.

    Yeah, my thoughts exactly.

    With modern languages and tools it's possible to rapidly restructure your code to fit the problem quite naturally. No need to try to fit into academic paradigms.
     
  10. Jesse Aldridge

    Original Member

    Joined:
    Apr 19, 2005
    Messages:
    221
    Likes Received:
    0
    Since posting about this I've decided that the problems that AOP attempts to solve are better solved by using a more dynamic language, such as Python.
     
  11. HairyTroll

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    582
    Likes Received:
    0
    A lot of design patterns are like that too. Invented to get around limitations imposed by the C++ and Java object models.
     
  12. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    In a little 2D flash-based engine i'm making i decided to try the whole behavioral idea. Here is an old version of the wiki page i use for this project (and the game about it) which describes briefly how it is used. The little pink sprite is controlled by two behaviors ("player" and "keyboard controlled"). It could do more, but at this point i was concerned more about making the art for the game than the code :)
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer