What is the best way for module communication?

To the original poster: what type of game or application are you trying to make? More often than not, knowing this will often result in seeing what architecture is a) possible and b) optimal for your project, so feel free to share.

In a single user environment “children accessing other children” is not so bad but in a multi-user environment where other developers could add what they consider a feature can cause major headaches for large code bases as this brings in unexpected behavior.  A classic example is a child destroying another child and the parent ending up with invalid pointers.

A common use-case is for the parent to spawn children and keep control of where the children are and what they are doing.  Now if a child has manually updated state for other children (for example, it’s position) then this becomes out of sync to the parent and this can cause functions and look-ups  against the parent to provide inaccurate data or worse.

IMHO, if a child needs to alter state in another then that change should be propagated via the controller so the controller is aware of this change.

Hi SGS,

Good point about the the multi-developer environment. I develop solo, and always

Fully agree, in that changes should be propagated via the controller. But for me this is another argument pro hierarchical tree architecture, and contra event dispatching.

Of course, as with any architecture, some coding discipline is required, and indeed, as per your example a child destroying another child is something to be avoided at all cost. If really needed, you could require passing the “source of the destruction command” as a parameter, and then refuse to be destroyed if the source is not an accepted controller object.

But for my single developer environment that’s a bit of overkill, and just maintaining some proper coding hygiene takes care of things.

At the risk of sound like a broken record on this forum: check out the PDF and sample code I uploaded in this thread. This outlines what in my view is the “best” way to structure your code in modules.

Disclaimer: I know there is no single best way for every project or developer. But I do know that if there was, it would be my way :smiley:

Just kidding! But do check it out - working this way has made my coding skills soooooo much better.

Some more detailed info: what I like about the structure I use is this:

  • Code is split into logical blocks that are as small as possible

  • Any element within my app can talk to any other element

  • The code structure and construction and cleanup routines are the same for every module

The above are the 3 main reasons for structuring my modules the way I do, and they speed up my development tremendously.