Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cr0wn3r · Feb 16, 2012 at 02:52 PM · c#classesbestpractices

Best practice for passing data between classes in C#

I had a large PlayerController class in my game which was getting unwieldy, so Ive split it in to multiple classes to handle the various bits. Much easier to work with and now I can re-use certain classes for other game objects without cluttering them with the whole lot.

So now I have:

MouseTarget Class - all this does is looks for the coordintates of a mouse click if it is on navigable terrain, does a few tricks, but basically makes those co-ordinates available for other scripts if / when the mouse is clicked on the terrain.

PlayerPath Class - gets the click coordinates for the overall destination from the MouseTarget Class (e.g. MouseTarget.clickPosition) , talks to the pathfinding stuff, and then based on the suggested path makes the target movement required for the next path segment available publically.

PlayerMover Class - looks for the co-ordinates PlayerPath.nextPathSegment and moves the player to it

PlayerAnimator Class - reads loads of properties from PlayerMover and executes animations based on how fast the player is moving, and their direction and orientation.

My question is, as you can see above, I now have a whole load of public properties which my classes use to work together. But it seems really messy just accessing them as Class.property. It means there's a spiders web of links between these classes that is going to get really hard to manage as the game grows.

Can anyone shed any light on a "best practice" to achieve things like this, in a more manageable way? I suspect it might be as much to do with C# class design techniques as Unity specifically, but even so, it would be great to get some pointers.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by sebas77 · Feb 16, 2012 at 03:40 PM

I would split the data from the logic. Encapsulate the data into mini classes (they are called Value Object) and inject them to all the classes that need to use the data in a given context.

Inject them by constructor or by setter. Using static class is bad, using public members is bad :)

You can decide to let your controller class hold the Value Object and then have a getter for the value object OR inject the value objects through an initialization phase (if you have any).

Pseudo code for the second case:

MouseCoordVO mouseCoord = new MouseCoordVO();

MouseTarget mouseTarget = new MouseTarget(mouseCoord); PlayerPath playerPath = new PlayerPath(mouseCoord);

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr0wn3r · Feb 16, 2012 at 07:54 PM 0
Share

Good advice. Funny - I normally develop in php and use dependency injection as a matter of course - but never occured to me to implement it here. I think without the control over the underlying framework it feels a little more abstracted than if I were writing an app from the ground up, or on top of a framework that already provides a DI container.

avatar image sebas77 · Feb 17, 2012 at 10:22 AM 0
Share

Indeed, the nature of Unity Framework does not allow straightforward dependency injection, but there are solutions. I would like to write an article about it once I have the time.

avatar image
2

Answer by Christian Stewart · Feb 16, 2012 at 03:00 PM

Hi,

What I recommend (from someone who is dealing with hundreds of dlls and classes) - make a static "instance" of each item (public static CharacterAnimator Instance) so you can access it from anywhere without bothering to use getComponent all the time.

Next I recommend using properties. You mention them above, however I'm unsure wether or not you're using this format:

public Vector3 movement { get; set; }

If you use that format you can easily write in methods to be run when the variable is called later on.

In terms of the "web" of references, this is "just the way it goes". Just be sure to remember what each method does with good commenting, and use a program like Visual Studio 2011 with the ReSharper plugin to ensure that all your code is in good form.

Feel free to edit with more suggestions!

~Christian

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr0wn3r · Feb 16, 2012 at 07:36 PM 0
Share

Thanks for all the tips. Yeah, commenting is going to be important I can see. I havent been using get; set;... I should be though I can see.

avatar image
2

Answer by tingham · Feb 16, 2012 at 03:03 PM

We have implemented both events, as well as key-value coding ( KNKVC http://www.kennettnet.co.uk/code/ ) to solve this problem. Key value change observation is really nice but requires a lot of boilerplate code. Events are a simple way to pass data between objects.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr0wn3r · Feb 16, 2012 at 07:35 PM 0
Share

Thanks - I'll check out unity events more thoroughly. That links handy. I have so much reading to do...!

avatar image tingham · Feb 16, 2012 at 07:43 PM 0
Share

If you have any implementation questions wrt $$anonymous$$N$$anonymous$$VC just hit me up on Skype @tingham

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Best Practice for Vector Nulls 2 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Unity C# Start function without extending Mono 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges