r/unity • • 2d ago

Coding Help Game Architecture

Hi, I'm a software dev with experience in web, mobile and backend, not much in game dev though. I also have ADHD which is kinda contributing to my dilemma.

I'll keep it short, I can't architect a game. I've been spending the past few days trying to come up with a good architecture and build a proper game layer for later use, I can't come up with a good enough one, and this is the type of stuff where YouTube doesn't help.

For example, someone said I should keep my first scene empty and instead load stuff by code after the scene has loaded through a GameInitiator or something. No idea how to do this properly.

Different issues like creating a very good, extensible game layer on top of Unity, where I have "entities" and "person" or "creature" or "NPC" or whatever and I can easily supply mesh and animation data and it would just work. similar to how GTA works as well as you being able to control any "skin' in the game and it works naturally rather than PlayerController is stuck to one person.

But not each character has the same ability, some of them have magic abilities, some of them are melees, etc, so a data-driven approach where each creature is just different data won't work, some of them require actual code and timed animations.

Other things are communicating between stuff, like doing something should update the UI and I know dragging the UI in the inspector isn't scalable, so I have no idea. Other people suggested a GameContext or a GameManager that holds references to all of them and it becomes a source of truth, others suggested an global event-driven/broadcast architecture.

So, what are your thoughts?

3 Upvotes

19 comments sorted by

4

u/sisus_co 2d ago

When thinking about flexible game architecture, I think dependency injection should be at the top of your mind.

Breaking functionality into smaller modular classes and then piecing them together using dependency injection makes it possible to achieve the ability to swap things like skins out at runtime. As a nice side bonus it can also make your code easily unit-testable and help bring clarity about dependencies, which can have a lot of benefits from readability to avoiding some common bugs.

So e.g. instead of a single-use PlayerController component, you could have a more reusable CharacterController component which could receive inputs from various different components. Then you could plug a PlayerInput component into it for the player character, or an CharacterAI component for your NPCs.

And for the ability to easily swapping the visuals of your entities, you can separate the visual layer of your entities (Renderer etc.) into separate prefabs, and then instantiate them underneath your entity root GameObject at runtime based on the active skin. And then you'll just have to write all your components at the root of the entity in such a way that they can function with any visuals.

Dragging references to UI-related components in the Inspector isn't something to be afraid of IMO, however it's good practice to keep have your gameplay-related components not know anything about your UI-related components. So if you have a HealthBarUI component, it can know about the Health component, but not the other way around. And the HealthBarUI can contain serialized field references to whatever UI components it needs and it's not a problem, but the Health components shouldn't contain any.

1

u/Aggressive_Damage_75 2d ago

I understand but won't you face an issue where everything is tightly coupled? Assume you create a new scene, you gotta wire everything back up. And assume you have an actual mission scene, and another testing ground scene, whatever you create in that testing ground won't affect the other scene, you gotta remember where you gotta wire everything, go to the other scene(s), wire it back up.

1

u/sisus_co 1d ago

Using dependency injection doesn't affect the number of dependencies, it just makes them more transparent and easily swappable.

So if you use [SerializeField] UIManager uiManager instead of UIManager.Instance, both components would fail to work the same if you don't have the UIManager component that they depend on added to the scene in question. The serialized field version, however, is actually more loosely coupled, because it supports drag-and-dropping any UIManager instance into the slot, instead of being tightly coupled to one particular singleton instance. And if you use a dependency injection solution that supports interfaces, then you can make things even more loosely coupled and easily testable when needed.

What's really cool about using dependency injection is that it makes the dependencies knowable at compile time, which unlocks the ability to do things like automatically log warnings in the Console if any dependencies are missing on any component or display warning boxes in the Inspector about missing dependencies. If you use things like singletons, then doing that automatically is impossible, because the dependencies are hidden and scattered inside various methods in your code.

If you use a dependency injection framework, then you can also automate the delivery of dependencies to your components. So, in this case you'd only need to configure UIManager as a service once, and then your mission scene and your testing ground scene would both automatically receive it. This way you can get the best of both worlds: the flexibility and transparency of dependency injection and the convenience of a singleton.

Personally I really value the ability to get immediate feedback about missing dependencies the moment I attach a component to some GameObject in a scene, rather than having to manually play-test it to find out that something was missing at runtime, so I put a lot of effort into getting instant feedback like that in my DI solution.

3

u/ledniv 2d ago

I think you are overcomplicating this by trying to invent a perfect reusable game layer before you have the game.

A data driven architecture does not mean every creature is only data and no code. The creature can have data that says which abilities it has, and those abilities can call completely different logic functions. The same goes for animation. Keep the actual attack state and timing in game data, then let the Animator visually show that state. The gameplay does not need to depend on an animation event firing at exactly the right frame.

For example, one creature might have:

AbilityIds[creatureIndex] = MAGIC_FIREBALL;

and another:

AbilityIds[creatureIndex] = MELEE_SLASH;

Then the logic for those abilities can be completely different. The data tells you what the creature can do. It does not replace the code that implements it.

I would also avoid building a global event system just so UI can find out what happened. Keep one authoritative game state. Gameplay logic modifies that state and the UI reads it. Health changes in GameData, then the health bar displays GameData. The player and UI do not need to know about each other.

And I would not make an empty bootstrap scene unless you actually have a problem that requires one. Start simple. One game loop, one place for the current state, one place for the rules, simple logic functions, and GameObjects for input and presentation.

Small plug: this is basically the architecture I build in High Performance Unity Game Development with Data Oriented Design. The book separates Balance, GameData, Logic, presentation, and the game loop, and specifically shows how to use that architecture inside Unity without needing ECS or a huge framework.

https://www.manning.com/books/high-performance-unity-game-development

1

u/Aggressive_Damage_75 2d ago

How would you make the UI read the player state without a direct reference by dragging it in the editor.

2

u/AltusLudus 2d ago

You are overthinking it, always build a system that makes the most sense to you, and if it's not working at some point refactor

1

u/Aggressive_Damage_75 2d ago

The point is by the time I realize how bad it is, it's too late to refactor. You gotta rebuild systems

2

u/AltusLudus 2d ago

It's never too late to refactor, why are you so negative lol are you building gta6 here

Go with lightweight architecture where you just build composable components and it'll never be hard to refactor

1

u/Aggressive_Damage_75 2d ago

It's a bit of a problem I have, I compare myself and my architecture to Rockstar and even attempt to replicate their internal tooling knowing very well that Unity is different. It's really a problem

5

u/AltusLudus 2d ago

But their architecture is made for a team of thousands of people, and a massive scale of the world, it's just a huge overhead for a solo dev building a smaller game

As they say, premature optimization is the root of all evil, don't over engineer things, focus on building the game

1

u/Aggressive_Damage_75 2d ago

I understand but yk, its a mental thing I'm trying to overcome but at least I'd be satisfied with some sort of layer that makes it easy to add new monsters or creatures without having to do lots of work.

1

u/Antypodish 2d ago

Now you are trolling.

1

u/Aggressive_Damage_75 2d ago

Why is it that when people ask for help there's always someone who assumes they're trolling? Grow up.

2

u/Antypodish 1d ago

Because you attempt doing something AAA multimillion dolar company does, withouth doing something simple and functional first, yet as a hobbiest. You pick an unrealistic goal, and you seems fixed on the idea.

Studying games is good. Replicating something far out of scope is bad.

As others said, you are overengineering things from the begin with.

Certain technical and architectural choices, extends the development time exponentially. So need to be aware, which design choice will strain the development. Time and skill set may the be bottleneck. Inexperienced dev will not know that yet.

Make prototype then small project work. Then learn things and the architecture over the time. Improve, iterate. Make next thing with improved architecture.

There is no such thing as "perfect design". Fit the design to your project's need.

Pick smaller games to start with and learn their architectures and technical choices. It will be easier to start with. Not trying to be a perfect game, because it won't be.

2

u/eliormc 2d ago

You're thinking too much, but I understand, there are several ways to do the same and it is confusing. So, my recommendation is:

  • the first scene is a loading screen, while loading the menu scene. This is your starting point. Read everything you need here from the user preferences or from a saving file and then start your game with this information in memory.
  • all global variables you can put in an object with the "singleton paternt" and don't destroy on load to keep it available all the game. Or, you can use scriptable objects as "comunication chanel" and you put the reference of the chanel (the scriptable object asset) to every object you want to share the variables values, and suscribe to events. This way I liked because you can create events in your scriptable objects and every game object who need to listen the event or trigger this event and send information to the others is independent, so, doesn't matter if the object who trigger the event is destroyed or there are multiple objects who trigger the event, they work as someone with a radio and the listeners can be or not in scene and always works. Also, when you read or write these variables from the scriptable object, they keep their values during all game.
  • in every scene, you can put a gameManager (just another GameObject with this big script) that handle the main logic of the game and keep all sync. This can be a singleton to make it available for all the other game object in scene without creating manual reference for each one. In this scrip you can handle the game progress.
  • About the skins, you can have a inputController script to handle your inputs independently from the characters and in every character you can use a player Controller scrip that will handle the characters, using other scripts for handle the animations, sounds, movements of the character. So, in your game manager you can have a list of reference characters, enable and disable the character (when you want to change), also, each one should have a Cinemachine camera (in the past it was a virtual camera), so, when you disable one character and enable the other, the camara travel to the active character. And because every character is feeding with the input manager, the player can use different skins seamless.

So, everything I said is too complex for you?, start with a simple scene, add a terrain, add 1 character, configure the animator, decide if you want to use the "character controller component" or use physics to move your character adding a rigidBody and colliders, (but DO NOT MIX characterController with RigidBody), then create your input manager with the new input system, create your player controller script, create your moving script, create a Cinemachine camera (you can use the simple camera but at the end it is more unnecessary work), and when you feel confident enough which these script, start thinking in the game logic, menus and other scenes.

I hope this help you.

1

u/dariusbiggs 1d ago

You are over architecting it and likely getting overwhelmed. Start small, no that's too big, smaller.

You will get more out of the process through appropriate use of Dependency Injection and Object composition.

Always start small, graphics are not really relevant early in the game development, boxes, spheres, generic shapes are more than enough. Some games can be fully playable without a gui.

Start with a single screen game to build your skills and learn how to structure things. Then develop from there.

Try these ones, either as a project or a thought exercise. Pong, Snake, Frogger, Go, Checkers, Othello, Solitaire, Tower of Hanoi, Nine men's Morris, The Royal game if Ur.

What components are you going to need to compose the objects in the game. What components are you going to need to manage the game state.

Take Pong.

  • You need a game screen (the base scene)
  • You need a paddle object, of which you need two instances
  • You need a ball object
  • You need a game state object to track the game scores. You can use a Singleton hete.
  • You need an input system for the paddle, this should be an interface. That way you can swap the paddle control from the user input to a computer input.
  • How are you going to detect a ball leaving the field and score for a player.

Are there any missing? Start implementing them one at a time, and figure out how you are going to test them. At some point you will get to the collision detection between the paddle and the ball, the ball and the game field, thd ball scoring for player 1, the ball scoring for player 2, these are great things to test. They ensure you don't break the game play with further development.

Hope that helps, good luck.

1

u/Singhaniia_Labs 1d ago

I've ADHD too so I can relate. What helped me was starting with one playable character and building around actual needs. I use components for behaviours, data assets for configuration, and interfaces for abilities that need custom logic. I also let systems communicate through specific events rather than one giant GameManager. Expand it to your real needs when it is required.