Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Abandoned Projects

This thread is locked; no one can reply to it. rss feed Print
Abandoned Projects
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Would delegation work for you? Or is that what you mean by the Mediator pattern? I don't have all the different design patterns memorized yet. It seems like you could sub divide the responsibility of GameScene into separate objects that handled different aspects of the game, which would help things stay modular, but would also alleviate the need for listeners. Every many to many relationship can be modeled as a one to one relationship that dispatches messages to others as necessary. I just don't see the value of listeners yet.

Dario ff
Member #10,065
August 2008
avatar

I have a more bloated version of Pong.

video

Game inside game. :o

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

james_lohr
Member #1,947
February 2002

jmasterx said:

I have not yet programmed professionally. I'm on my last year of college. Maybe writing code for a company will better enlighten me on the benefits of parents handling child logic, but as it stands, I find myself able to much more easily debug, test, and add features with listeners.

Actually what you are doing is much closer to what one would expect to see in professional/enterprise code than what others are suggesting. The difference is that professional code would likely be broken down into even smaller components, with even more emphasis on extensibility and maintainability.

Quote:

Maybe writing code for a company will better enlighten me on the benefits of parents handling child logic

Let's hope not. :P

don't see the value of listeners yet.

I'm not sure about C++, but in C# listeners (which are necessary for events) are wonderfully elegant and are the cleanest solution to a multitude of problems.

jmasterx
Member #11,410
October 2009

I could do it like this, yes. I think it would mean a big switch case since the parent would have:

case CARD_THROWN_ON_TABLE:
client->passMessage(msg);
playSound("throw");
...
...
break;

From the server to the client and client to the server, I pass messages and there is a switch case and it is not too big.

To do it like this, I still have to be bothered to look through big switch cases, where I might forget to write break;

Generally the way I use listeners is that I have 1 big listener class for all the events in the game. The parent just has to make the connections between the objects, who is listening to what. I think both come out to about the same, but I can see how listeners can be a pain without reference counting.

As far as letting the parent handle the logic of its children, I feel it would get out of hand, but I'm fine with delegation. I do use it when it is far simpler than a listener.

With listeners I can use a variety of enums to represent the message which helps self document the code for me. I also do not have to go digging through a gigantic case statement.

When it is between delegation and listeners, I think the developer should choose whichever they feel more comfortable with. They each present a solution to many to many relationships while staying modular and flexible. (At least for C++) As was mentioned, in a language like C#, listeners are absolutely the way to go, as they are practically built into the language.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I'm not sure about C++, but in C# listeners (which are necessary for events) are wonderfully elegant and are the cleanest solution to a multitude of problems.

This is a little too vague to be helpful.

Edit
@jmasterx
The way I do it is like this :

void InheritedClass::QueueUserMessage(Widgetmsg wmsg) {
   const Widgetmsg msg1(&some_widget , TOPIC_SOME_KIND_OF_WIDGET_TOPIC , ENUM_MESSAGE_VALUE);
   if (wmsg == msg1) {
      // blah, we got a message from some_widget about something blah blah deal with it here
   }
   if (notused) {
      parent->QueueUserMessage(wmsg);
   }
}

verthex
Member #11,340
September 2009
avatar

The difference is that professional code would likely be broken down into even smaller components, with even more emphasis on extensibility and maintainability.

I always thought that was used to maintain a working relationship with India for outsourcing source code. Being more professional these days implies being unemployed for less time but always switching companies.

jmasterx
Member #11,410
October 2009

@jmasterx
The way I do it is like this :
void InheritedClass::QueueUserMessage(Widgetmsg wmsg) {
const Widgetmsg msg1(&some_widget , TOPIC_SOME_KIND_OF_WIDGET_TOPIC , ENUM_MESSAGE_VALUE);
if (wmsg == msg1) {
// blah, we got a message from some_widget about something blah blah deal with it here
}
if (notused) {
parent->QueueUserMessage(wmsg);
}
}

That is Chain of Responsibility, you pass it up the parent chain until someone consumes it. That works pretty well for Gui components with children, or composed Widgets like a NumbericUpAndDown. In this case, that works much more effectively than a listener. It is also small though. In GUIs in general it is a good tool for the job. Java's Swing I read uses a Model View Controller which adds a fair amount of flexibility.

Quote:

Game inside game. :o

Pongception :o

OICW
Member #4,069
November 2003
avatar

Either help to reformat for the wiki ;) ;) ;), or zip them all up and attach them to the Create page. That way anyone can download them and help to add the rest to the wiki.

Oh, well. I've thought about that myself. Didn't have much time though. I'll see what I can do. Anyway I've wanted to get to wiki editting sooner or later :)

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Specter Phoenix
Member #1,425
July 2001
avatar

jmasterx said:

With C++0x, more references is not really a big deal for me.

Psst. It is no longer C++0x it is now C++11 :P.

OICW said:

Oh, well. I've thought about that myself. Didn't have much time though. I'll see what I can do. Anyway I've wanted to get to wiki editting sooner or later

Yeah I've thought the same thing, but don't have anything that would be beneficial to be put up. Sometimes I wish we had Pixelate still going from the users but I think almost all the writers have moved onto other things.

jmasterx
Member #11,410
October 2009

Quote:

Psst. It is no longer C++0x it is now C++11 :P.

Yes, I know, but I use Visual Studio 2010 which came out before the official C++11 standard.

OICW
Member #4,069
November 2003
avatar

Yeah I've thought the same thing, but don't have anything that would be beneficial to be put up

Meanwhile as we speak - ok, about an hour ago :) - I've put online chapter 3, more coming tomorow/today depending on your timezone.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Specter Phoenix
Member #1,425
July 2001
avatar

jmasterx said:

Yes, I know, but I use Visual Studio 2010 which came out before the official C++11 standard.

Change compilers immediately! VS doesn't support the standards that well. Last I knew it doesn't even support the C99 standard yet.

gnolam
Member #2,030
March 2002
avatar

You're really determined to crap on this thread, aren't you? :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Specter Phoenix
Member #1,425
July 2001
avatar

gnolam said:

You're really determined to crap on this thread, aren't you?

No, I have VS but every time I am on IRC they point out that VS doesn't support quite a bit of the standards which is why I quit messing with them. Now I use gcc in Ubuntu and Ming in windows. I can think of better ways to 'crap on this thread' other than pointing out that VS is outdated (knowing MS it is by choice).

james_lohr
Member #1,947
February 2002

This is a little too vague to be helpful.

In C#, events/listeners look like this:

#SelectExpand
1 2public class Keyboard 3{ 4 public Action<Key> KeyPress; // <- this is an action that other classes can subscribe to using any method with a matching signature of (Key) 5 6 private InternalCrap() 7 { 8 //lots of encapsulated code 9 KeyPress(key); 10 //lots more encapsulated code 11 } 12} 13 14 15class Program 16{ 17 private Keyboard keyboard = new Keyboard(); 18 static void Main() 19 { 20 //register a "listener" 21 keyboard.KeyPress += KeyPressMethod; 22 //register another listener 23 keyboard.KeyPress += KeyPressMethod2; 24 //deregister one of the listeners 25 keyboard.KeyPress -= KeyPressMethod; 26 } 27 28 private void KeyPressMethod(Key key) // <- note: this method as a signature matching the action 29 { 30 System.out.println("Key " + key + " was pressed!"); 31 } 32 33 private void KeyPressMethod2(Key key) // <- as does this one 34 { 35 System.out.println("Just printing this line to illustrate that multiple method references (delegates) can be added to the action."); 36 } 37 38}

The point is that "listeners" are so cleanly supported by the language that it tends only to be inexperienced programmers that don't use them and instead opt for polling.

Specter Phoenix
Member #1,425
July 2001
avatar

The point is that "listeners" are so cleanly supported by the language that it tends only to be inexperienced programmers that don't use them and instead opt for polling.

I'm inexperienced so guess that means I should opt out of listeners until I become experienced ;).

Paul whoknows
Member #5,081
September 2004
avatar

Can we use the spoiler tags for off-topic comments? wouldn't be that a great idea?

I mean, it bothers me to skip off-topic comments, it would be great if it were some way to easily see off-topic comments and then just ignore them.

By the way, I really like Death Note デスノート Desu Nōto , take a look at it: http://en.wikipedia.org/wiki/Death_Note

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Dario ff
Member #10,065
August 2008
avatar

I've watched the anime, and it was awesome. :D People told me I should read the manga tho.

EDIT:

I was rooting for L

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Paul whoknows
Member #5,081
September 2004
avatar

L Lawliet? hahaha. I'm reading the manga right now (chapter 45/108) it's amazing! I think I'm going to watch the anime after reading all the chapters! :)

I wish I had a Death Note, real names and pictures of some of the members in this forum ;D off-topic comments would be dramatically reduced in my threads.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Dario ff
Member #10,065
August 2008
avatar

I think I'm going to watch the anime after reading all the chapters!

The anime is quite short as well. Only 37 episodes, but I loved it(except for the last few because of some new characters...)

Quote:

I wish I had a Death Note, real names and pictures of some of the members in this forum ;D off-topic comments would be dramatically reduced in my threads.

I'm safe. :P

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

23yrold3yrold
Member #1,134
March 2001
avatar

I heard the anime was sub-par compared to the manga. Haven't seen it, but the manga was fantastic. Until about the half-way point (JUST AS PLANNED). Then it dropped a bit to pretty good.

Fun fact: The same artist/writer team who did Death Note (writer Tsugumi Ohba and artist Takeshi Obata) now write and draw Bakuman, which is one of my current favorite mangas. It's kind of a meta-comic about a couple of teenagers trying to make it as a manga writer and artist. :)

(... how did we get off on this topic ...)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Specter Phoenix
Member #1,425
July 2001
avatar

(... how did we get off on this topic ...)

Same way we always do. We just talk about what is on our minds and boom we are off topic and on a topic that is completely left field.

Trent Gamblin
Member #261
April 2000
avatar

Talking about cartoons/animes here is fine, I can't control it. But if you're ever in my house, remember this rule: NO ANIME.

Specter Phoenix
Member #1,425
July 2001
avatar

The abandoned projects thread has now been abandoned. :o

Tobias Dammers
Member #2,604
August 2002
avatar

jmasterx said:

Maybe writing code for a company will better enlighten me on the benefits of parents handling child logic

Textbook OOP modelling is often inconvenient, inefficient, or plain old wrong.

Consider the typical moving ball problem. Many OOP textbooks model a moving ball like this:

class Ball {
    public:
    virtual void move() { x += dx; y += dy; }
};

Very nice, the ball moves itself. Awesome. We can even implement a bunch of moving objects, each with a different implementation of the polymorphic move() method, and they will all move differently.

But then we want to do collisions, and the thing breaks. The ball moves, but in order to check if it can actually move, it needs to know a lot about its environment: the position and type of all the other objects, the walls, and maybe a lot more. We can, of course, change the move method's signature to:

virtual void move(World& world);

...but this means we have to open up a whole bunch of internals of both the world and its objects so that individual objects can read and manipulate them. The benefits of encapsulation are mostly moot this way. Another ugly spot is that if two object collide, the design mandates that one of them is the 'bouncer' and one is the 'bouncee': it's ball1->bounceWith(ball2);, even though they are really equal partners in the collision.

If we take a step back, we might observe, though, that in the real world, a ball does not in fact move itself. It is a passive object, subject to physical effects such as elasticity, inertia, in short, mechanics. The ball does not move itself: the ball has a bunch of properties, and the laws of nature act upon it.

Taking this back into the OOP domain, we end up with a design where the ball is just a data object with little functionality; it has a position and a velocity, and a few immutable properties such as mass, elasticity, etc. Movement is not handled by the ball itself, but by the Physics class: this class reads the object's parameters, and performs suitable physics calculations, something like this:

class Physics {
    void updatePositions(Object[] objects) {
        foreach (objects as object) {
             object->position += object->velocity * deltat;
             foreach (collidingObjects(object, objects) as cobject) {
                 handleCollision(object, cobject);
             }
        }
    }
};

The Physics class can act upon all the objects, without knowing too much about them (e.g., it doesn't have to know the color of a ball to calculate how it bounces, so the ball can keep that information to itself), but it can naturally operate on two objects at a time - the bouncer/bouncee problem goes away, as it is now physics->bounce(ball1, ball2);.

If you're a keen observer, you may notice something: the ball is a plain data object with little or no functionality; the physics class is pure functionality with little or no state. This means that we have separated state from behavior, exactly the opposite of what OOP tries to do (bundle state and behavior into classes). We could just as well have written this in a procedural paradigm or, in fact, if we ignore the OOP boilerplate, we actually have: class Physics is basically just a function (or a set of functions), and class Ball is just a record type (polymorphic, if you have different object types, but still just a record type).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén



Go to: