Hi everyone! I hope you find this dive into the Service Locator pattern interesting! There will be a follow up video to this one to explore Dependency Injection and further explore the topic of Inversion of Control in a few weeks - suggestions, comments and questions welcome! I plan to add a bit more functionality to this Service Locator implementation later this week, which might not make it into a video - keep an eye on the repository if you are interested in that!
I appreciate the last comment on Service Locator might be considered as an antipattern because of some of its caveats. It is still very valuable to try this approach and see for yourself. Very nice tutorial!
Learned a lot here, and this is my second time watching this after following the previous videos in the channel, its starting to make more sense. Ill end up watching this a couple of more times to walk through it until it clicks completely...awesome content! Can't say that enough.
Thanks for the suggestion - I've received a few similar comments lately, and I'll definitely be considering that as a future topic that would likely span several videos, or possibly some kind of project. Cheers!
Although I really enjoy these complex topics with great examples, I'd like to see some more projects being build using all these best practices. Something like the 3D platformer, but more extended. I totally understand if this is a big task/project and you would rather focus on these kind of videos, it's just a suggestion :) Great work!
Thanks for the comment - this is something I've been thinking about more now that the channel has grown to be much than when I did the 3D platformer series. Extended projects appeal to less viewers, but have the benefit that you just described in your comment. Stay tuned!
Thanks a lot for this video. I think this'll solve my dependency issues for the most part, especially configuring locators for scenes or even just gameobjects sounds exactly what I've been looking for. You did mention that it's not "feature rich" yet, so I wonder what else you would add to it?
You're welcome! Probably the next feature I would add would be lifecycle management, in case you registered some services and wanted them to automatically do something every Update / FixedUpdate etc. Maybe we can cover a few things like that in a future video, but I might add to the repository before that as time permits.
Great! My advice would be to start small with just one Service or Resource that you reference from multiple places. One thing I didn't mention in the video is that Factory or Abstract Factory classes are often used together with Inversion of Control paradigms. So, for example, if you use a unique ParticleEffectFactory for each scene, your various components that use the factory never change - they just retrieve the registered IParticleEffectFactory for that scene from the ServiceLocator.
I was thinking I'd make 2 videos - in the first one we can make a small DI system from scratch since it's a good learning exercise, and the second one could be a compare / contrast / explore video about several different frameworks.
I dont prefer to use a 3rd party asset to put it a top of my code structure. One small update or a bug on the asset would cause a lot of problem. Even though i like DI, i dont like using it with Unity that makes me feel i am fightining with the engine itself.
thank you for covering these topics! i'd love it if you would spend a bit more time on the overview, as i often get lost, not sure what class you're writing in and what the variables or functions do. still, awesome content!
I downloaded the zip of the files. Got a missing script reference error when creating the Service Locators through the context menu. Moving the ServiceLocatorGlobal and ServiceLocatorScene classes to their own files fixed the error. Is their a project setting in Unity that allows for creating components in files that don't have the same name?
That's interesting, I've not had that problem or heard of anyone who did. But this could happen for any number of reasons. There isn't a special setting to enable this. At any rate, I'm glad you were able to resolve the issue, maybe I should update the repository.
Mostly use that when the scene is loading, in the Start method. Then you can cache the reference and only need to call for it one time right at the beginning.
Thanks! I think the black menus were introduced in one of the recent beta versions of Unity 2023. I’m using 2023.2.0b13 right now. Took a while to get used to them tbh.
Do you think Service Locator pattern and Dependency Injection can be used together? The two seem like opposites of each other to me. I even read somewhere that Service Locator was called anti-pattern, just like Singleton. What do you think?
I do think you could use them together, but I don't know that I would choose to do that - the reason being that the DI solution shown on this channel only runs at startup, but the Service Locator could be used even for new Game Objects created after the game starts. For that reason I would either continue to develop the DI solution to be able to handle more situations, or just use the Service Locator. It really depends on what you need to accomplish.
@@git-amend As I understand it, Dependency Injection has a more open structure, because the dependencies are explicitly specified in the code and these dependencies are provided by the injection container. During the addition phase, dependencies are usually defined through a configuration file or records specified during program startup. In Service Locator, the resolving has a more centralized structure in that the components get the dependencies they need through the Service Locator. During the runtime phase, Service Locator usually register records and resolves them dynamically. This provides more centralized control and a flexible structure, but can reduce the transparency of the code about where dependencies come from.
Thanks for this, great implementation, however,I have a question : I'm using your implementation here, I want to reference 2 objects with the same type inside an object from Scene1 into another object in Scene2. How to do this with service locator ? am I supposed to create a c# script that I name ServiceGlobalObjectContainer and add them to the service global and get the service in the other side on a script and affect them ?
Interesting question. The first and probably easiest thing that comes to mind to to create a Factory that creates, caches and supplies the 2 services you want that are the same type. Then you can supply the factory class instead of the services and ask the factory for the one you want. This way the ServiceLocator knows about the Factory and not the 2 services that are the same type.
By registering the services inside the Hero classes, aren't you still tightly coupling the Hero to those services? Doesn't that defeat the purpose of the service locator? I'd have expected the services themselves and consumers to just request them.
@@git-amend Lets face it, almost every game needs a inventory system. It would be super awesome to have someone walk through a good system that we can apply to almost any game.
I really want to understand this advanced work. But my level is not there yet, I am having a hard time understanding this. 😢 Where should I start to learn advance coding?
Big Bro I want to become a game developer and develop games like gta , rdr , Resident evil , uncharted etc etc which language hsould I start with I am in 10 grade I mastered MIT 6.042J and CS50 should I learn DSA or should do something else
Your background in MIT 6.042J and CS50 provides a strong foundation in computational thinking, which is great. Focusing on DSA will significantly enhance your problem-solving skills, crucial for complex game development tasks like those in GTA or Uncharted. The key to success in engineering or game dev is your ability to solve REAL problems. Start small - build a lot of small games that you can go start to finish in one day, build them and then do a retrospective - ask yourself: How can I improve? What am I getting good at? What are my weaknesses? Then repeat, maybe trying something a little different, a little bigger each time. You'll be amazed how fast your skills grow when you do this on a daily basis.
Thanks for asking about a Discord server! It's something I'm considering. To better understand everyone's interest, I might set up a poll or some kind of survey soon to find out if/how people would use that. Stay tuned; your input will be invaluable in making a decision!
@@git-amend Could we do our Bootstrapper abstract class in Generic T where T:Monobehaviour so the base class will be reusable even more or is it ridicilous?
Just don't understand about this method. Isn't that Unity already had a lot tools that can be use, why adding a new method to it when we can use the Unity code?
Unity does not offer inversion of control systems (Dependency Injection or Service Locator) out of the box. You have to build your own or use a 3rd party tool or Asset.
Hi everyone! I hope you find this dive into the Service Locator pattern interesting! There will be a follow up video to this one to explore Dependency Injection and further explore the topic of Inversion of Control in a few weeks - suggestions, comments and questions welcome!
I plan to add a bit more functionality to this Service Locator implementation later this week, which might not make it into a video - keep an eye on the repository if you are interested in that!
Man.... it's like you're reading my mind with the timing of this one : )
@@stevehamilton321 Haha! Awesome!
Man you are pumping out quality Unity videos like crazy!
Thanks for all your hard work 🙏🏽
Thank you! Glad you like the vids! Cheers!
I appreciate the last comment on Service Locator might be considered as an antipattern because of some of its caveats. It is still very valuable to try this approach and see for yourself. Very nice tutorial!
Thanks!
If I had the amount of knowledge you have, I'd walk down the street feeling god incarnate
Just found your channel this past week and it's quickly becoming my favorite for game dev content. Thank you!
Right on, glad you like it! Cheers!
Hey great video! I love this kind of more "complex" game development content. Looking forward to the DI and frameworks video.
Right on, thanks!
Learned a lot here, and this is my second time watching this after following the previous videos in the channel, its starting to make more sense. Ill end up watching this a couple of more times to walk through it until it clicks completely...awesome content! Can't say that enough.
Thanks again!
Outstanding content. I am loving these videos.
Glad to hear it! Thanks!
You are doing great stuff and very advenced C#. Thanks a lot for your content! :)
My pleasure! Glad you enjoy it!
Found your channel recently and i love your videos. Helped me alot in understanding patterns and using them in my games. Keep up the great work
Great to hear! Thanks!
You are my best teacher :))), i like your content
Thank you so much!
I would love to see you do a series on DOTS / ECS / Job System
Thanks for the suggestion - I've received a few similar comments lately, and I'll definitely be considering that as a future topic that would likely span several videos, or possibly some kind of project. Cheers!
I'm happy to join your fans croud. Great content with precise explanation.
Awesome, thank you!
Thanks for all these videos. I studied software engineering and its refreshing to see some unity videos, that are on a industry standard level! :)
You're very welcome! Glad to hear that!
@@git-amendWow, that was a fast reply xD
Although I really enjoy these complex topics with great examples, I'd like to see some more projects being build using all these best practices. Something like the 3D platformer, but more extended. I totally understand if this is a big task/project and you would rather focus on these kind of videos, it's just a suggestion :) Great work!
Thanks for the comment - this is something I've been thinking about more now that the channel has grown to be much than when I did the 3D platformer series. Extended projects appeal to less viewers, but have the benefit that you just described in your comment. Stay tuned!
Thanks a lot for this video. I think this'll solve my dependency issues for the most part, especially configuring locators for scenes or even just gameobjects sounds exactly what I've been looking for.
You did mention that it's not "feature rich" yet, so I wonder what else you would add to it?
You're welcome! Probably the next feature I would add would be lifecycle management, in case you registered some services and wanted them to automatically do something every Update / FixedUpdate etc. Maybe we can cover a few things like that in a future video, but I might add to the repository before that as time permits.
Another fantastic video, though its going to take me some time to figure out how I use this one in my game
Great! My advice would be to start small with just one Service or Resource that you reference from multiple places. One thing I didn't mention in the video is that Factory or Abstract Factory classes are often used together with Inversion of Control paradigms. So, for example, if you use a unique ParticleEffectFactory for each scene, your various components that use the factory never change - they just retrieve the registered IParticleEffectFactory for that scene from the ServiceLocator.
I had my own clumsy implementation of this, now stealing yours :)
Awesome!
Now I can wait for dependency injection magic, would you use framework such as VContainer or Exenject or writing your own?
I was thinking I'd make 2 videos - in the first one we can make a small DI system from scratch since it's a good learning exercise, and the second one could be a compare / contrast / explore video about several different frameworks.
I dont prefer to use a 3rd party asset to put it a top of my code structure. One small update or a bug on the asset would cause a lot of problem. Even though i like DI, i dont like using it with Unity that makes me feel i am fightining with the engine itself.
@@TheKr0ckeR C# natively contains a dependency injection framework. Most people have no idea but it's been in there since version 1.0.
thank you for covering these topics! i'd love it if you would spend a bit more time on the overview, as i often get lost, not sure what class you're writing in and what the variables or functions do. still, awesome content!
Thanks, and I'll keep that in mind! I will probably add some comments to the code in the repository this week as well.
I downloaded the zip of the files. Got a missing script reference error when creating the Service Locators through the context menu. Moving the ServiceLocatorGlobal and ServiceLocatorScene classes to their own files fixed the error.
Is their a project setting in Unity that allows for creating components in files that don't have the same name?
That's interesting, I've not had that problem or heard of anyone who did. But this could happen for any number of reasons. There isn't a special setting to enable this. At any rate, I'm glad you were able to resolve the issue, maybe I should update the repository.
ForSceneOf method looked dangerous a little bit because of many loops, shall we consider of doing that method async so it may not cause a stutter.
Mostly use that when the scene is loading, in the Start method. Then you can cache the reference and only need to call for it one time right at the beginning.
Great guide! Do you use custom theme for Unity? I see black dropdown menu's and etc. Is that an extension or smth? :)
Thanks! I think the black menus were introduced in one of the recent beta versions of Unity 2023. I’m using 2023.2.0b13 right now. Took a while to get used to them tbh.
Do you think Service Locator pattern and Dependency Injection can be used together? The two seem like opposites of each other to me. I even read somewhere that Service Locator was called anti-pattern, just like Singleton. What do you think?
I do think you could use them together, but I don't know that I would choose to do that - the reason being that the DI solution shown on this channel only runs at startup, but the Service Locator could be used even for new Game Objects created after the game starts. For that reason I would either continue to develop the DI solution to be able to handle more situations, or just use the Service Locator. It really depends on what you need to accomplish.
@@git-amend As I understand it, Dependency Injection has a more open structure, because the dependencies are explicitly specified in the code and these dependencies are provided by the injection container. During the addition phase, dependencies are usually defined through a configuration file or records specified during program startup. In Service Locator, the resolving has a more centralized structure in that the components get the dependencies they need through the Service Locator. During the runtime phase, Service Locator usually register records and resolves them dynamically. This provides more centralized control and a flexible structure, but can reduce the transparency of the code about where dependencies come from.
Thanks for this, great implementation, however,I have a question : I'm using your implementation here, I want to reference 2 objects with the same type inside an object from Scene1 into another object in Scene2. How to do this with service locator ?
am I supposed to create a c# script that I name ServiceGlobalObjectContainer and add them to the service global and get the service in the other side on a script and affect them ?
Interesting question. The first and probably easiest thing that comes to mind to to create a Factory that creates, caches and supplies the 2 services you want that are the same type. Then you can supply the factory class instead of the services and ask the factory for the one you want. This way the ServiceLocator knows about the Factory and not the 2 services that are the same type.
Very interesting, and excellent code as always. Did I notice some norske words in that list? ;-)
Takk! Og ja, du har et skarpt øye - Copilot foreslo tysk, men vi vet jo alle at norsk er veien å gå! 😉
By registering the services inside the Hero classes, aren't you still tightly coupling the Hero to those services? Doesn't that defeat the purpose of the service locator? I'd have expected the services themselves and consumers to just request them.
It's just an example for the video. Of course in a real scenario you would not register a service and then request it in the same class.
Didn't know Matthew Mcconaughey did programming tutorials. Love you game programming pattern videos! They are very helpful and well explained!
Haha, do I sound like an actor from Texas? Thanks for the comment!
please create us a inventory system that is modular and robust!
Great suggestion! I'm actually planning something along those lines in the new year. Stay tuned!
@@git-amend Lets face it, almost every game needs a inventory system. It would be super awesome to have someone walk through a good system that we can apply to almost any game.
I really want to understand this advanced work. But my level is not there yet, I am having a hard time understanding this. 😢 Where should I start to learn advance coding?
Little bit at a time I suppose! Keep watching, there will be more intermediate content this year as well.
Big Bro I want to become a game developer and develop games like gta , rdr , Resident evil , uncharted etc etc which language hsould I start with I am in 10 grade I mastered MIT 6.042J and CS50 should I learn DSA or should do something else
Your background in MIT 6.042J and CS50 provides a strong foundation in computational thinking, which is great. Focusing on DSA will significantly enhance your problem-solving skills, crucial for complex game development tasks like those in GTA or Uncharted. The key to success in engineering or game dev is your ability to solve REAL problems. Start small - build a lot of small games that you can go start to finish in one day, build them and then do a retrospective - ask yourself: How can I improve? What am I getting good at? What are my weaknesses? Then repeat, maybe trying something a little different, a little bigger each time. You'll be amazed how fast your skills grow when you do this on a daily basis.
@@git-amend Thank you bro for your advice , definitely follow your words👍👍
Which AI-voice tool do you use to create your videos? Or is this your actual voice?
That is my real voice
Crazy, I would have lost a bet on that. :D
Anyways, thank you for your very informative videos!
Cool video but a bit advanced for my mind at the moment
There's always tomorrow 😊
will you be making a Discord server?
Thanks for asking about a Discord server! It's something I'm considering. To better understand everyone's interest, I might set up a poll or some kind of survey soon to find out if/how people would use that. Stay tuned; your input will be invaluable in making a decision!
Have my comment been deleted? 🤔
its the only tutorials can not be watched at 2x
I'll take that as a compliment!
@@git-amend Exactly
@@git-amend Could we do our Bootstrapper abstract class in Generic T where T:Monobehaviour so the base class will be reusable even more or is it ridicilous?
Just don't understand about this method. Isn't that Unity already had a lot tools that can be use, why adding a new method to it when we can use the Unity code?
Unity does not offer inversion of control systems (Dependency Injection or Service Locator) out of the box. You have to build your own or use a 3rd party tool or Asset.