# Unity Question about how to achieve loose coupling

## 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627718
• Total Posts
2978792
• ### Similar Content

• Your home planet was attacked. Now you have to use your spaceship to battle the invaders. Powerful 3D arcade with outer space background. Very addictive. Good luck!

• Attack Of Mutants is an adrenaline - powered and bloody shooter in with lots of horror and action! Beat back the waves of opponents. The game features a lot of weapons and types of enemies. Show them what you are capable of. Prove your power and strength!

• By sprotz
I like unity3d but the installation size is too big. the package itself is 1GB and the installation is 5GB. What are some good, easy 3d game engines similar to Unity that have smaller file sizes? Excluding engines that lack editors like level editors, world editors etc, with an easy enough scripting language, physics and good documentation.

•
I'm looking for a team to help me out with a 3D platformer. Basically, you jump between platforms with parkour elements and you fight enemies with a mix of melee and ranged attacks. This is purely a hobby project. I'm not promising any payment, ever. You can do it for experience, to learn, for fun, whatever, as long as you don't expect to get paid. Right now I need a 3D modeler and animator. Reply or email me at jordestoj@yahoo.com if you're interested. Thanks.

• Hi all!
We are looking for a C# programmer for our 2D Action RPG titled Adavia, made in Unity.
The game itself is akin to Legend of Zelda: Link to the Past, though we're also adding in traditional RPG elements such as Character Creation.
This is more of a hobby than anything commercial, if it somehow does manage to go commercially, all revenue will be split equally among the team.
If you're interested, we ask that you be comfortable with:
Unity Coding A.I's for enemies and NPCs. Working with GUI's. Communicating regularly with the team via Skype (text only). If you have any questions or would like to apply, please contact me at nathan.jenkins1012@gmail.com

• 9
• 9
• 21
• 14
• 12