# Unity Question about how to achieve loose coupling

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
What's wrong with just passing references to the sound/font/whatever system into the object classes so they can use them like that? You don't need a big over engineered framework, just pass things into the bits that need them as appropriate. Or is that not "sexy" enough? [rolleyes]

The obvious (and flawed) rebuttal is "but I'd need to pass the sound system everywhere!". Except that actually when you get down to it you really don't. Reliance on a global has made you and your code depend on it being used everywhere, but thats not how it should be.

However things aren't as bad as they seem. If you actually get over the initial fear and start making the changes you'll find it surprisingly easy. Even though it feels like certain objects are being used everywhere, often it's much narrower than you think. And those uses that are remaining can probably be either refactored out or are acceptable dependancies.

##### Share on other sites
I see your point. The function that would need the references passed would be, in this example, object loading. So the game would call the OBJECTS::LoadSceneFromFile(string filename) function, but it'd have to pass the SOUND class, PHYSICS class, MESHES class, TEXTURES class, etc. A bit messy, but it would be restricted to only one area. Hmm.

##### Share on other sites
Quote:
 Original post by venzonI see your point. The function that would need the references passed would be, in this example, object loading. So the game would call the OBJECTS::LoadSceneFromFile(string filename) function, but it'd have to pass the SOUND class, PHYSICS class, MESHES class, TEXTURES class, etc. A bit messy, but it would be restricted to only one area. Hmm.

First, you should get your vocabulary straight.

You don't pass classes. Unless, of course, you're using a language with reflection, such as Java or C# or (shudder) PHP, and you're passing around the type itself. In most applications, you pass around class instances. Of which there can, of course, be more than one.

So, you'd have to pass one of the SOUND class instances to that function. Which sounds pretty normal to me, since you need to specify how the scene will be able to play sounds.

##### Share on other sites
Quote:
 Original post by venzonI see your point. The function that would need the references passed would be, in this example, object loading. So the game would call the OBJECTS::LoadSceneFromFile(string filename) function, but it'd have to pass the SOUND class, PHYSICS class, MESHES class, TEXTURES class, etc. A bit messy, but it would be restricted to only one area. Hmm.

What about passing a mediating structure between these class instances, something like:

void GAME::loadSceneFromFile(string fileName);{    SceneInfo sceneInfo = ObjectManager.getSceneInfoFromFile(fileName);    SoundManager.create ( sceneInfo.getSoundData() );     //SoundManager is an instance of SOUND    PhysicsManager.create ( sceneInfo.getPhysicsData() ); //PhysicsManager is an instance of PHYSICS    MeshManager.create ( sceneInfo.getMeshData() );       //MeshManager is an instance of MESHES    TextureManager.create ( sceneInfo.getTextureData() ); //TextureManager is an instance of TEXTURES}

This way none of the other subsystems know about anything else other than a structure detailing what it is they're supposed to be creating.

1. 1
2. 2
3. 3
Rutin
20
4. 4
frob
18
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632562
• Total Posts
3007091

×