Service Locator: Inversion of Control in Unity C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น • 84

  • @git-amend
    @git-amend  ปีที่แล้ว +5

    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!

    • @stevehamilton321
      @stevehamilton321 ปีที่แล้ว +1

      Man.... it's like you're reading my mind with the timing of this one : )

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      @@stevehamilton321 Haha! Awesome!

  • @guyginat
    @guyginat ปีที่แล้ว +18

    Man you are pumping out quality Unity videos like crazy!
    Thanks for all your hard work 🙏🏽

    • @git-amend
      @git-amend  ปีที่แล้ว +5

      Thank you! Glad you like the vids! Cheers!

  • @adamprokop
    @adamprokop 10 หลายเดือนก่อน +2

    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!

    • @git-amend
      @git-amend  10 หลายเดือนก่อน

      Thanks!

  • @MelMacaluso
    @MelMacaluso 24 วันที่ผ่านมา

    If I had the amount of knowledge you have, I'd walk down the street feeling god incarnate

  • @HunterSchramm
    @HunterSchramm ปีที่แล้ว +2

    Just found your channel this past week and it's quickly becoming my favorite for game dev content. Thank you!

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      Right on, glad you like it! Cheers!

  • @cheesecakenl1980
    @cheesecakenl1980 ปีที่แล้ว +10

    Hey great video! I love this kind of more "complex" game development content. Looking forward to the DI and frameworks video.

    • @git-amend
      @git-amend  ปีที่แล้ว +2

      Right on, thanks!

  • @Cloud-Yo
    @Cloud-Yo 8 หลายเดือนก่อน

    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.

    • @git-amend
      @git-amend  8 หลายเดือนก่อน +1

      Thanks again!

  • @xlilcasper
    @xlilcasper ปีที่แล้ว +3

    Outstanding content. I am loving these videos.

    • @git-amend
      @git-amend  ปีที่แล้ว +2

      Glad to hear it! Thanks!

  • @PrzygodazUnity
    @PrzygodazUnity ปีที่แล้ว +2

    You are doing great stuff and very advenced C#. Thanks a lot for your content! :)

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      My pleasure! Glad you enjoy it!

  • @PascalR5255
    @PascalR5255 ปีที่แล้ว +1

    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

    • @git-amend
      @git-amend  ปีที่แล้ว

      Great to hear! Thanks!

  • @termite1737
    @termite1737 ปีที่แล้ว +2

    You are my best teacher :))), i like your content

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      Thank you so much!

  • @PaulRobello
    @PaulRobello ปีที่แล้ว +2

    I would love to see you do a series on DOTS / ECS / Job System

    • @git-amend
      @git-amend  ปีที่แล้ว +3

      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!

  • @anton.m.korshunov
    @anton.m.korshunov 8 หลายเดือนก่อน

    I'm happy to join your fans croud. Great content with precise explanation.

    • @git-amend
      @git-amend  8 หลายเดือนก่อน

      Awesome, thank you!

  • @mhouney
    @mhouney 5 หลายเดือนก่อน

    Thanks for all these videos. I studied software engineering and its refreshing to see some unity videos, that are on a industry standard level! :)

    • @git-amend
      @git-amend  5 หลายเดือนก่อน +1

      You're very welcome! Glad to hear that!

    • @mhouney
      @mhouney 5 หลายเดือนก่อน

      ​@@git-amendWow, that was a fast reply xD

  • @ekekw930
    @ekekw930 ปีที่แล้ว +1

    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!

    • @git-amend
      @git-amend  ปีที่แล้ว +4

      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!

  • @devphil0
    @devphil0 11 หลายเดือนก่อน +1

    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?

    • @git-amend
      @git-amend  11 หลายเดือนก่อน +2

      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.

  • @Broudy001
    @Broudy001 ปีที่แล้ว

    Another fantastic video, though its going to take me some time to figure out how I use this one in my game

    • @git-amend
      @git-amend  ปีที่แล้ว +3

      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.

  • @ribicb
    @ribicb ปีที่แล้ว

    I had my own clumsy implementation of this, now stealing yours :)

  • @nguyenhoangminhtrung2779
    @nguyenhoangminhtrung2779 ปีที่แล้ว +1

    Now I can wait for dependency injection magic, would you use framework such as VContainer or Exenject or writing your own?

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      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.

    • @TheKr0ckeR
      @TheKr0ckeR ปีที่แล้ว +1

      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.

    • @WalterGordyCanada
      @WalterGordyCanada 9 หลายเดือนก่อน

      @@TheKr0ckeR C# natively contains a dependency injection framework. Most people have no idea but it's been in there since version 1.0.

  • @uhhmir
    @uhhmir ปีที่แล้ว

    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!

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      Thanks, and I'll keep that in mind! I will probably add some comments to the code in the repository this week as well.

  • @OdysseyHome-Gaming
    @OdysseyHome-Gaming 7 หลายเดือนก่อน

    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?

    • @git-amend
      @git-amend  7 หลายเดือนก่อน

      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.

  • @omeryilmaz1021
    @omeryilmaz1021 6 หลายเดือนก่อน

    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.

    • @git-amend
      @git-amend  6 หลายเดือนก่อน

      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.

  • @TheKr0ckeR
    @TheKr0ckeR 11 หลายเดือนก่อน

    Great guide! Do you use custom theme for Unity? I see black dropdown menu's and etc. Is that an extension or smth? :)

    • @git-amend
      @git-amend  11 หลายเดือนก่อน +1

      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.

  • @endihidir2087
    @endihidir2087 10 หลายเดือนก่อน

    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?

    • @git-amend
      @git-amend  10 หลายเดือนก่อน

      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.

    • @endihidir2087
      @endihidir2087 10 หลายเดือนก่อน

      @@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.

  • @Yogs01180
    @Yogs01180 7 หลายเดือนก่อน

    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 ?

    • @git-amend
      @git-amend  7 หลายเดือนก่อน +1

      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.

  • @TheArghnono
    @TheArghnono ปีที่แล้ว

    Very interesting, and excellent code as always. Did I notice some norske words in that list? ;-)

    • @git-amend
      @git-amend  ปีที่แล้ว +2

      Takk! Og ja, du har et skarpt øye - Copilot foreslo tysk, men vi vet jo alle at norsk er veien å gå! 😉

  • @gregbradburn
    @gregbradburn 5 หลายเดือนก่อน

    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
      @git-amend  5 หลายเดือนก่อน +1

      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.

  • @hainkiwanki
    @hainkiwanki 11 หลายเดือนก่อน

    Didn't know Matthew Mcconaughey did programming tutorials. Love you game programming pattern videos! They are very helpful and well explained!

    • @git-amend
      @git-amend  11 หลายเดือนก่อน

      Haha, do I sound like an actor from Texas? Thanks for the comment!

  • @damonfedorick
    @damonfedorick 11 หลายเดือนก่อน +1

    please create us a inventory system that is modular and robust!

    • @git-amend
      @git-amend  11 หลายเดือนก่อน +2

      Great suggestion! I'm actually planning something along those lines in the new year. Stay tuned!

    • @damonfedorick
      @damonfedorick 11 หลายเดือนก่อน +1

      @@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.

  • @MarioSantoso
    @MarioSantoso 10 หลายเดือนก่อน

    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?

    • @git-amend
      @git-amend  10 หลายเดือนก่อน +1

      Little bit at a time I suppose! Keep watching, there will be more intermediate content this year as well.

  • @Normie4lyf
    @Normie4lyf ปีที่แล้ว

    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

    • @git-amend
      @git-amend  ปีที่แล้ว +1

      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.

    • @Normie4lyf
      @Normie4lyf 11 หลายเดือนก่อน

      @@git-amend Thank you bro for your advice , definitely follow your words👍👍

  • @martin.m.kloeckener
    @martin.m.kloeckener 10 หลายเดือนก่อน

    Which AI-voice tool do you use to create your videos? Or is this your actual voice?

    • @git-amend
      @git-amend  10 หลายเดือนก่อน

      That is my real voice

    • @martin.m.kloeckener
      @martin.m.kloeckener 10 หลายเดือนก่อน

      Crazy, I would have lost a bet on that. :D
      Anyways, thank you for your very informative videos!

  • @5thBabbitt
    @5thBabbitt 6 หลายเดือนก่อน

    Cool video but a bit advanced for my mind at the moment

    • @git-amend
      @git-amend  6 หลายเดือนก่อน

      There's always tomorrow 😊

  • @manofapocalypse
    @manofapocalypse ปีที่แล้ว

    will you be making a Discord server?

    • @git-amend
      @git-amend  ปีที่แล้ว +2

      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!

    • @manofapocalypse
      @manofapocalypse 11 หลายเดือนก่อน

      Have my comment been deleted? 🤔

  • @omeryilmaz1021
    @omeryilmaz1021 6 หลายเดือนก่อน +1

    its the only tutorials can not be watched at 2x

    • @git-amend
      @git-amend  6 หลายเดือนก่อน +1

      I'll take that as a compliment!

    • @omeryilmaz1021
      @omeryilmaz1021 6 หลายเดือนก่อน

      ​@@git-amend Exactly

    • @omeryilmaz1021
      @omeryilmaz1021 6 หลายเดือนก่อน

      @@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?

  • @actk1
    @actk1 4 หลายเดือนก่อน

    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?

    • @git-amend
      @git-amend  4 หลายเดือนก่อน

      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.