3
\$\begingroup\$

I know the basics of C#(Syntax, 4 pillars, SOLID, Designs Patterns, etc...) and Unity. I can do basic stuff like Flappy bird or Fruit Ninja without any help. But problems arises when I want to create more advanced projects, I don't know how to structure projects and I can't find any guides on how you are supposed to do it. Like where are things supposed to go.

I know that you are supposed to create different Managers that handle the flow of your game like a "Game Manager" or a "Sound Manager" or a "Spawn Manager" or a "Score Managers" but I never know what logic I am supposed to put into it. I don't even know what managers you are supposed to create for easy games like flappy bird. Should you create a Score Managers that controls the score? What about a Game Manager, what should it control? What states should it have? Maybe "isRunning" and "gameOver", maybe.

But what about more complex games where you have an inventory, currency systems, etc... Lets say you have a "fish game where you can walk or hunt fish with a submarine and sell it to upgrade your submarine to dive deeper into the depths of the ocean." what states should the game manager than have? Should its responsibilities only be if the player is dead or not? What about dialogs, UI's, etc...

What about a 2D Platformer where your goal is to go from point A to point B with the help of the "Past you"

I have 20+ Unity games I would like to create, but I never learned how to use the knowledge I have to create things.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ "I know that you are supposed to create different Managers that handle the flow of your game" -> No, you are not supposed to do that. It's one way to do things, but it's not always the only way or even a good way. \$\endgroup\$
    – Philipp
    Commented Jul 9 at 8:23
  • 2
    \$\begingroup\$ Let me give you some advice from a grumpy old games programmer who has been doing this for forty years: just do it. Don't worry about the best way, just write something that does something. In time you'll learn what works and what doesn't, but you'll never understand that from me or anyone else telling you. \$\endgroup\$ Commented Jul 9 at 17:34
  • \$\begingroup\$ You might try reading some discussions/critiques of game code. maybe try "quake 3 source code review" etc. I thought of quake because the code is typically open source and people have a chance to see how Carmack constructed in the ways you are struggling with. Not saying Quake is/isn't programmed well or that there aren't more modern ways to manage the difficulties, but might give some insight about code roles. \$\endgroup\$
    – Yorik
    Commented Jul 9 at 21:18
  • \$\begingroup\$ One big difference between Unity C# and oldschool SOLID C# is that in Unity you don't think as much in "classes" but more in components. A component has only one simple purpose. A Unity gameobject will have several components to create a behaviour. \$\endgroup\$
    – Kokodoko
    Commented Jul 11 at 12:31

1 Answer 1

19
\$\begingroup\$

There's no rule that says that you have to have a bunch of component classes named "SomethingManager". It's common for projects to have a bunch of Manager components, but that's often because naming things is hard and people get lazy. Having many Manager components can even be a sign of poor architecture, especially if all of these managers are large and complex classes that put too much responsibility in one place.

Putting it another way, if you're thinking "a game has to have a Game Manager, so I need to figure out what to put in my Game Manager" then you're approaching your architecture from the wrong direction. Write the code that your game needs in a way that's suited to your game. Don't try to shoehorn your code into a particular design philosophy just because you think that's what everyone else does.

Using a "Spawn Manager" as an example; I never create a single SpawnManager for my games. Instead, I create different spawners for different types of objects using a reusable object pooling system, and reference those spawners where needed. So instead of doing something like this...

public class Bomb : MonoBehavior {
    // [...]

    public void Explode() {
        SpawnManager.Instance.SpawnBombExplosion(transform.position);
    }
}

...there will be something more like this:

public class Bomb : MonoBehavior {
    [SerializeField] private ExplosionSpawnPool explosionSpawnPool;
    // [...]

    public void Explode() {
        explosionSpawnPool.SpawnAt(transform.position);
    }
}

There's even a good reason to avoid the WhateverManager approach to naming: as can be seen in your question, a name like "GameManager" is not very useful because it doesn't really help you understand what the component does. A component name like "DeckShuffler" or "PlayerSpawner" gives you a very good idea of what the component does and when it should be used. "GameManager" tells you practically nothing about what specific functionality is implemented in the component. Some would argue that if you can't come up with a good simple-and-descriptive name for a class, that means that the class is too complicated.


There are so many kinds of games, and so many ways you can design and build even a particular type of game, that it's hard to find general wisdom about how to structure a project that is applicable to many projects. It's sometimes helpful to read general guides on software architecture, but some principles that make a lot of sense in other types of software don't fit well in a video game. Experience goes a long way here; the more projects that you develop, the more you'll understand what does and doesn't work and have better ideas about how to approach your architecture.

Don't settle into a standard pattern and use it over and over even if it isn't the right fit for a particular game. I'm always thinking about lessons I've learned from what I've built, and how I might improve the architecture for both the current project I'm working on and for future projects.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ Really good answer, its surprisingly rare that I create 'managers' when I'm just coding in general. They always end up being bloated because they're so convenient to throw whatever new functions I need into them. The one exception is simulations, creating specific managers for specific properties I'm trying to simulate makes things far more straightforward in my experience. \$\endgroup\$
    – ChellCPlus
    Commented Jul 9 at 11:33
  • \$\begingroup\$ @Kevin But how do I learn about "architecture" and game design and all that? I am like a broken calculator right now. I have learned the basics of various stuff in Unity and C#(its my first programming language and game engine) but I don't know how to create advanced stuff with it. I don't know how to make an inventory system or currency to buy things even do I know all things that is used to create it. How do you learn all that stuff? \$\endgroup\$
    – MrV
    Commented Jul 9 at 13:02
  • 2
    \$\begingroup\$ @MrV practise, practise and once again practise. Try to make a lot of smaller things or games and just focus on actually making them. You can also watch some tutorial if that is helping you for better understanding but the most is gained by actually trying and doing it yourself \$\endgroup\$
    – Zibelas
    Commented Jul 9 at 13:12
  • \$\begingroup\$ @MrV think about what an inventory system does. What are the actions you can perform on it? (Perhaps: put an item in, take an item out, list items). What data do you have to store to accomplish that? (A list of objects, probably). What invariants do you need to maintain? (No negative quantities, maybe a limit on # of slots, # of a given item, or total weight). Now start writing some code to make it happen. For sure you'll make mistakes and have to rewrite. Maybe you'll run into things you don't know how to do — but then you'll have a specific problem to solve and question to ask. \$\endgroup\$
    – hobbs
    Commented Jul 9 at 14:51
  • 1
    \$\begingroup\$ @MrV for me it is breaking it down into smaller and smaller parts until you can't really divide it anymore and code those one by one. What is an inventory? Something that holds many things. Means we need to have something that can store that, some function to add to the storage and removing it. Some flare for displaying, maybe a sorting, keeping track of how many items. There are many things you could add, but you can keep it as well really simple. \$\endgroup\$
    – Zibelas
    Commented Jul 9 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .