0
\$\begingroup\$

I know a thing or two about Unity at this point, but I've still lots of subjective, best practice questions, Such as: How do you decide what values are obtained via Start/Awake initialization, and which values are explicitly serialized into the scene? In my current project, game objects and behaviors reference each other in a complex graph. Initially, I did this on Start(), programmatically, with lots of raycasts and GetComponent<>() and such. Now though, I just declared stuff public (violating encapsulation?), to simply serialize this information to the scene once generated by an Editor script... Which is more correct? Should my methodology be that anything that can be serialized should be serialized? Or is it better to write more initialization code which conveys some explanation why the values are what they are?

\$\endgroup\$

closed as too broad by Vaillancourt, bummzack, Josh Jan 8 '16 at 22:19

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ To use this site properly, you'll want to break this into individual questions that have solid possible answers. As it stands, it's just too broad for a single answer. Come talk with us here: chat.stackexchange.com/rooms/19/game-development \$\endgroup\$ – Almo Jan 8 '16 at 18:48
  • 2
    \$\begingroup\$ The end goal is to make a game. It doesn't matter how you get there. The player does not care; the player just wants to have fun. Whatever methodology you choose to implement to make your life easiest to turn an empty project into a completed project is up to you. No methodology is perfect. \$\endgroup\$ – Chris McFarland Jan 8 '16 at 19:07
  • 2
    \$\begingroup\$ As Almo said, we don't deal in subjective questions, and best practice, really, is different from organization to organization. \$\endgroup\$ – Vaillancourt Jan 8 '16 at 19:07
1
\$\begingroup\$

You're right that this is subjective. You can load your initialization data from literally anywhere. So in my mind, asking about what method you should use is not answerable by anyone other than yourself.

So regarding how you might make that decision, there are a few things to consider.

  1. You mentioned encapsulation. I think this is not a concept that should be applied to initialization. The thing about configuring the system is that you are doing the part of programming that can't be abstracted in the academically ideal, OO-software way of doing things. Whether you're embedding it inside a dedicated unity script, reading an external text file, or designing some sort of convoluted dependency injection container, it's important to remember what you're trying to accomplish. That something is: the ugly part of the system. The coupling, the dependencies, the code that ought not be within a well-designed class. You need some of that code to make a working system of any type, especially games. But it probably won't be pristine under any circumstances.

  2. Serializability: should you be able to move this data outside the code. I would contend that yes, you should. Many reasons. However, as a stop-gap, implementing the data inside a single unity script is not a bad tactic, as long as you can easily update it and/or transition away from it.

  3. Explanation: should you have to describe the configuration as you write it. Ideally, no, because configuration data ought not be too complicated to speak for itself. Is that the case? Perhaps you should reconsider what is configurable and what is not. If the configuration is susceptible to error, then those errors ought to be caught by the thing loading the configuration. So that might be a better place to explain the cause of the errors. Or to efficiently report errors back to the configuration author, to inform them of their mistake.

  4. What unity callback should run it. I am not a unity expert, but I would doubt that there is an objectively correct answer to this. My intuition is that it does not matter. You should design your unity app to run some sort of startup logic that loads the data, and from there bootstrap itself into a system that runs the game using that data without worrying where it came from. In other words, show a main loading screen that figures it out. Then transition out of that loading screen into your actual game.
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.