r/unity • u/Aggressive_Damage_75 • 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?
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.
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:
and another:
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
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
2
u/frey89 2d ago
You can read through this as a starting point https://github.com/UnityTechnologies/open-project-1/tree/main/UOP1_Project/Assets/Scripts
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:
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.
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.