Help me better understand Zenject and Dependency Injection.
My situation: I'm trying to get a job as a junior dev and I was given a task of reworking code based on a singleton to one based on Dependency Injection (using Zenject) and explain how it improves the code. Never used Zenject or anything similar before, so now I'm trying to understand it. I learned a bit about the framework, but honestly, I don't have problems with understanding HOW to work with it so far, I don't understand WHY use it. What problem are they really solving in the context of Unity games? Also, I have questions about DI itself.
1. They provided an example of a singleton-based code with a simple singleton Player class. But I don't understand why would you need a Player singleton in the first place — it can simplify some things, of course, but if you have enemies, you'll probably want to share a lot of code with them, so you don't want to write a singleton anyway. I feel like this is solving a non-existent problem. Can you guys give me a case where using a singleton in a first place (and replacing it with Zenject later) would be a reasonable idea? I usually use singletons only for a few managers in a project and feel like installing a whole framework just to get rid of 2-3 singletons isn't the best approach.
2. Another reason to use DIF I've seen is: "If you have a scene with 50 NPCs that all require 20 dependencies each, you will realize quickly that populating them in the inspector is an absolute nightmare. To create an NPC factory that creates them from a prefab is much better. But now you have to either supply all the reference they need manually, which means a ton of code that just assigns stuff, and you are forced to supply all references to all of them, even if they do not need them. This is just an example. With a DI framework, this becomes trivial and you can solve this with like 10 lines of code.". Again, I don't understand it. I totally agree on making a factory, but how much dependencies you need to change that a base prefab + prefab variants + Scriptable Objects + a bit of custom logic wouldn't be enough? How DIF make things easier in this case, you'll still need to manually select all needed dependencies and to write code for DIF, right?
3. What is wrong with methods like Object.FindObjectsByType so you need to replace them with DIF? Yes, it can't search by interface, but if it's something you want to search in scene, you probably should make it as a component with a single responsibility anyway?
4. Does sending a reference of a class via Event count as DI? What about accessing a class via collision or Object.FindObjectsByType?
5. Let say I have multiple object in a scene, enemies, for example, and I need to access a specific one of them. Does Zenject help in this situation? If yes, how?
6. I heard that DI framework is far more important for larger projects, but why?
7. Another line of reasoning for DIF is that "Monobehavior is bad and outdated, don't use it; with DIF you can use GameObjects and Monobehaviors only when you really need it". What exactly is so bad about the Unity approach (not arguing one way or another, just trying to better understand things)? What are pros and cons of moving your game logic out of Unity classes?
8. How would you approach to designing, for example, a Character (not one class, but everything, including colliders, animations, logic, etc) moving most of logic in pure C#?
https://redd.it/1pzauer
@r_Unity3D
My situation: I'm trying to get a job as a junior dev and I was given a task of reworking code based on a singleton to one based on Dependency Injection (using Zenject) and explain how it improves the code. Never used Zenject or anything similar before, so now I'm trying to understand it. I learned a bit about the framework, but honestly, I don't have problems with understanding HOW to work with it so far, I don't understand WHY use it. What problem are they really solving in the context of Unity games? Also, I have questions about DI itself.
1. They provided an example of a singleton-based code with a simple singleton Player class. But I don't understand why would you need a Player singleton in the first place — it can simplify some things, of course, but if you have enemies, you'll probably want to share a lot of code with them, so you don't want to write a singleton anyway. I feel like this is solving a non-existent problem. Can you guys give me a case where using a singleton in a first place (and replacing it with Zenject later) would be a reasonable idea? I usually use singletons only for a few managers in a project and feel like installing a whole framework just to get rid of 2-3 singletons isn't the best approach.
2. Another reason to use DIF I've seen is: "If you have a scene with 50 NPCs that all require 20 dependencies each, you will realize quickly that populating them in the inspector is an absolute nightmare. To create an NPC factory that creates them from a prefab is much better. But now you have to either supply all the reference they need manually, which means a ton of code that just assigns stuff, and you are forced to supply all references to all of them, even if they do not need them. This is just an example. With a DI framework, this becomes trivial and you can solve this with like 10 lines of code.". Again, I don't understand it. I totally agree on making a factory, but how much dependencies you need to change that a base prefab + prefab variants + Scriptable Objects + a bit of custom logic wouldn't be enough? How DIF make things easier in this case, you'll still need to manually select all needed dependencies and to write code for DIF, right?
3. What is wrong with methods like Object.FindObjectsByType so you need to replace them with DIF? Yes, it can't search by interface, but if it's something you want to search in scene, you probably should make it as a component with a single responsibility anyway?
4. Does sending a reference of a class via Event count as DI? What about accessing a class via collision or Object.FindObjectsByType?
5. Let say I have multiple object in a scene, enemies, for example, and I need to access a specific one of them. Does Zenject help in this situation? If yes, how?
6. I heard that DI framework is far more important for larger projects, but why?
7. Another line of reasoning for DIF is that "Monobehavior is bad and outdated, don't use it; with DIF you can use GameObjects and Monobehaviors only when you really need it". What exactly is so bad about the Unity approach (not arguing one way or another, just trying to better understand things)? What are pros and cons of moving your game logic out of Unity classes?
8. How would you approach to designing, for example, a Character (not one class, but everything, including colliders, animations, logic, etc) moving most of logic in pure C#?
https://redd.it/1pzauer
@r_Unity3D
Reddit
From the Unity3D community on Reddit
Explore this post and more from the Unity3D community
Help out a newby dev with visual noscripting!
Hey, i recently started working on my first 2d project and wanted to make it using the visual noscripting system since c# is a bit much to wrap my head around rn. Ive been putting stuff together with the help of Chat gpt but it's getting really frustrating with him. If anybody would be interested in helping me get a clear basic understanding of the system it would be amazing. If youre interested in helping me out send me a dm!!!!!!
https://redd.it/1pzf9eb
@r_Unity3D
Hey, i recently started working on my first 2d project and wanted to make it using the visual noscripting system since c# is a bit much to wrap my head around rn. Ive been putting stuff together with the help of Chat gpt but it's getting really frustrating with him. If anybody would be interested in helping me get a clear basic understanding of the system it would be amazing. If youre interested in helping me out send me a dm!!!!!!
https://redd.it/1pzf9eb
@r_Unity3D
Reddit
From the Unity2D community on Reddit
Explore this post and more from the Unity2D community
I can make beautiful designs in Photoshop, Illustrator and web dev. But game development with Unity shaders, materials, textures and lightning is a whole other beast.
https://redd.it/1pzhru8
@r_Unity3D
https://redd.it/1pzhru8
@r_Unity3D