Let’s say I have a button which is created within a layer, which is constructed by another layer and that by an over-arching module of my game. I want that button to call a function on a particular object (or objects!) which was created by a layer within a layer within a user interface module which was, itself, constructed by a composer scene. That’s quite a distance between the caller and the receiver.
Currently, I have three options open to me to allow that function call to take place:
- Pass a reference to the thing I might want to call a function on around the system. The penalty here is that tidying up references can cause memory leaks after garbage collection and is just unsightly.
- Have the thing with the function to be called listen for an event on the current scene or Runtime object and simply remove the listener when it is finalized. This approach is handy, but lends itself to losing the connection between the items making the call and receiving the call, which makes future maintenance harder.
- Using a single table, perhaps returned from a dedicated module, to store a list of named functions, which can be called in a ‘global’ style but is more easily managed. This, too, has the flaw that tidying references up is a risk at garbage collection but that connections between caller and receiver are more obvious during maintenance and expansion.
What do you do to make distant or complex connections between components easier to manage, maintain and simply easier to see when returning to old code?