How to split up your scripts in Unity (the easy way)

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 54

  • @GameDevBeginner
    @GameDevBeginner  Місяць тому +4

    One thing to add to this, in this example I used an interface to make the connection abstract from both sides in code, but that's not the only way to do it. For example, in my course, which this example is from, I ended up using a Unity Event. The end result is similar, except that the fire display doesn't need to actively do anything.

  • @AzazelezazA
    @AzazelezazA Місяць тому +8

    This is exactly what I'm dealing with right now for my school project. I'm either abstracting everything to eternity or can't seperate a class properly fearing of communication between parts will be pain in the ass in future. I guess practice will be the answer to this problem. I will pick a way and it will cause problems when I decide to change something in future and I will learn from it and improve my decision making in future projects. Not doing anything because of fear of failure is always worse than doing something and fail.

  • @bane9109
    @bane9109 Місяць тому +13

    Been doing gamedev for around 2.5 years and this is still something that I sometimes struggle with. It has improved the longer I’ve been coding, but sometimes still difficult to decide. Thanks for the video!

    • @MyPing0
      @MyPing0 Місяць тому

      Yeah same here. I've learned to take advantage of events as much as possible wherever reasonable. Sometimes I would have a hierarchy of specific scripts being controlled by things a bit more general which are controlled by a manager script. The manager script would use the events thrown out by the scripts it manages to.. well.. manage them. lol

  • @Marc142000
    @Marc142000 Місяць тому +2

    I'm surprised you didn't make any mention to the methods explained on your 'Connecting scripts on the same object in Unity' video where you had a class that stored the actions an object could execute and other components subscribe to them. I think that way of doing things is perfectly explained in that video, but I would have liked you to talk more in depth about when to implement that style of method over the ones described on this video.
    Either way, brilliant information and lovely explaination. Thank you so much and keep them coming!!

  • @patrickbielau6919
    @patrickbielau6919 Місяць тому +5

    the text display in your video is aesthetically pleasing. And the you do a great job in giving options, not doctrines. That's rare in the coding scene :)

  • @mattallenclosedforum9505
    @mattallenclosedforum9505 Місяць тому +10

    I always think, if the scripts are small are modular I can use them for multiple projects. And then I start from scratch every time 😂

    • @PhysicsPhun_Ameya
      @PhysicsPhun_Ameya Місяць тому

      Yeah, me too. I start to feel like I am gonna forget the script if I just use the previous one

  • @black_squall
    @black_squall Місяць тому +6

    My favorite unity tutorial guy.

  • @dogemcdogeface7901
    @dogemcdogeface7901 Місяць тому +3

    Hey man, this is a really great video that touches a topic that we struggle at work too!
    Just a quick question, at 4:04 you mentioned that Ground Check could be implemented via Inheritance, would it be better to make Ground Check as an Interface instead? Just figured that an interface is more modular. Thanks in advance ! :D

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      Thank you! Personally, I would want to use an interface when I want to create compatibility (i.e. two different classes can be treated as if they're the same, even though they're not) and inheritance to share functionality (avoid writing the same code twice in both scripts) so for me, it's inheritance. But, this only works if the hierarchy of the scripts is a good fit, and it may still be possible to do something like this with interfaces, now that default implementation is a thing. Just my opinion of course.

  • @junba1810
    @junba1810 Місяць тому +2

    Your channel is insanely underrated! I also want to note that the blog's on the website are really useful too! When I am actually working in Unity I prefer reading than watching a video, so it's really helpful!

  • @Fossilsmudge
    @Fossilsmudge Місяць тому +2

    hey i'd be interested in a video about attributes in unity. ive seen some but dont completely know how to use them, create them, and most built in ones.

  • @andisarifi5805
    @andisarifi5805 Місяць тому +2

    A unity scripting course? Sign me up!

  • @Fossilsmudge
    @Fossilsmudge Місяць тому +1

    just started learning unity and your vids are a breath of fresh air, easy to follow and very helpful!

  • @batty251
    @batty251 Місяць тому +1

    This is how I usually do Unity scripts:
    Usually if something that has movement its all in a movement script for that game Object and then a separate animations script that handles the animations.
    For everything else if its a global variable I use containers via scriptable objects.
    UI script for UI systems.
    Interactions are usually done on the Game Object's movement script unless its a global then I have a dedicated script that handles like a bullet hitting a game Object so it adds force.
    I like to have my scripts used in a global setting not just to 1 game Object specifically.
    A door script can have just a door script to every door or if its a portal then a portal script to every portal and have the scene loaded in as a string array and u pick what scene it will go to from that array etc.
    This is helpful thinking and I enjoyed the video.

  • @immortalsun
    @immortalsun Місяць тому

    Very useful, Mr British Man, thank you!

  • @timmygilbert4102
    @timmygilbert4102 Місяць тому

    I separate feature and architecture, any code will have some specific cuisine, so brstvto contain what's unique in single places. For example, move is a feature, it takes input modifier and position and output back new position, it shouldn't know about anything else, the manager is the one responsible for linking input type, position and controller results to the move element, that keep bug seeking into two type, feature implementation bug, or architecture communication bug, it help refactoring because you either rewrite features or rewrite architecture, even when both need rewrite it's a clear separation of concern. In practice it makes your code more modular, but modularity is only as good as how many utils class your code depends on 😂 the math class sipping everywhere is inevitable.

  • @_PadoStudio
    @_PadoStudio Місяць тому

    He should be over 100k subscribers.... algorithm do you WORK

  • @kozmobotgames
    @kozmobotgames Місяць тому

    Thanks for the tutorial!

  • @Atenvardo
    @Atenvardo Місяць тому +1

    Love out! Another great video, on a random side note, what IDE do you use? Looks very clean for Mac which is nice.

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      Visual Studio, which is unfortunately, now discontinued on mac 😭

    • @Atenvardo
      @Atenvardo Місяць тому

      @@GameDevBeginner Ohhh I heard that, what’s the next alternative? Only one I have found that runs good on Mac on par is Rider, paid unfortunately ): But great with Unity

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      I'll probably try VS Code next, or whatever is offered up with Unity by default, if only for the sake of using what beginners will ultimately use too. I have heard Rider is good though.

  • @b5fan504
    @b5fan504 Місяць тому

    I submit that anyone who thinks an entire game's worth of code should go into one script, doesn't yet know enough about what they're doing. This is why we have principles and guidelines and architectural patterns.

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      I certainly wouldn't do it that way but if it works, it works: ua-cam.com/video/cFRT9E0C3XM/v-deo.html

  • @dibaterman
    @dibaterman Місяць тому +1

    Imo, single responsibility is best used when dealing with AI behavior. For most other scripts it's just about a free for all. But doing things like splitting up the jump behavior then allows you to adjust exactly how that jump behavior will work as well. Take it to the next step you can store the behavior as a SO which can be swapped as needed.

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      Neat idea with the scriptable object, would be ideal for that.

    • @dibaterman
      @dibaterman Місяць тому

      @@GameDevBeginner More than an idea, it's my usual approach, honestly I'd love to share my state machine architecture with anyone I come across, if you are interested.
      I have 0 talent for doing videos but I feel the SM would benefit a lot of people.

    • @dibaterman
      @dibaterman Місяць тому

      I just did a Live Stream helping someone get into C#, that's my best way to give back.

  • @notadev9000
    @notadev9000 Місяць тому

    So in your example, does the fireDisplay still subscribe to OnThrust action defined in IMoveable? And all IMoveable objects can invoke that action when they move?

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      Yes, and yes. Anything that implements IMoveable would need to include it.

  • @aarongeorge347
    @aarongeorge347 Місяць тому

    I've personally been exploring a composition architecture to go along with how Unity already does game objects and components. Unity Events help keep components ignorant, but I fear I'm going to end up over-using them...

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      In my experience Unity Events can be great for local connections, like on a prefab, and especially if you want to set things up in the Inspector. Personally, I think they work best when the connection is more likely to be unique. Otherwise they can be a lot of work to keep setting them up in the same way on different objects.

  • @simplycoding347
    @simplycoding347 Місяць тому

    Great ❤😊

  • @watercat1248
    @watercat1248 Місяць тому

    to be fer i use all kind off method's.
    for example the player movement for my game have movement,jump,ground check all on the same code and it's interesting for all the other codes.
    however some code in my game is like chin because i haven't though for better system for example on the game im working on is multiple game and many off my weapons i will have the same type off ammo,
    that means i need to have 2 codes one for the ammo type and one for whatever weapon the use the same ammunition.
    in general i prefer to make evry code impeding with each other in few words to work on it's own but unfortunately for that is not possible for my project
    the reason is because my game is complex and some system's have to refresh other codes
    one good example for this is the PlayerHelth for my game it have 3 or more code in the order to work's correctly
    1. one code that it's just the player health.
    2. that is hurt box.
    3. whatever object dealing damages to the player
    my point no matter what wii do wean i make health for my player i have to use at least 2 code one for the health and the other in the order to do the damage or heal the player.
    and i don't even mention all the other part that effects by the for health like kill death ect,
    that it's crucial in multiplayer PvP fps Game.

  • @chizuru1999
    @chizuru1999 Місяць тому

    Can you please share what font and theme you use in jetbrains? Awesome video btw.

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      So it was visual studio, gruvbox theme, and I think the font is menlo 👍🏻

    • @chizuru1999
      @chizuru1999 Місяць тому

      @@GameDevBeginner ye i thought its gruvbox, i use in vscode but isnt the mac version for vs discontinued? Thanks

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      @@chizuru1999 It is and it's a shame because, even though VS Code has gruvbox themes, none of them look exactly like my visual studio one, which I love!

    • @chizuru1999
      @chizuru1999 Місяць тому

      @@GameDevBeginner Yes bro. They feel totally different lol. And I guess its something to do with apple not allowing it. And its a headache to maintain the visual studio for linux/mac. Last time I checked it works on mono only. Thankfully I mostly use win for work 😁

  • @nTu4Ka
    @nTu4Ka Місяць тому

    Celeste was coded as one big class afaik.

  • @arthurjvnb
    @arthurjvnb 8 годин тому

    My biggest problem with interfaces in Unity is that it is not serializable out of the box. I do love using interfaces, but that single limitation makes me almost never use it inside Unity (only interfaces from System itself).

    • @GameDevBeginner
      @GameDevBeginner  5 годин тому +1

      Absolutely, it would be great to be able to drag a reference to a field based on the interface it uses

    • @arthurjvnb
      @arthurjvnb 5 годин тому

      @GameDevBeginner yeah. I hope they add that feature in the near feature (so that we do not need to rely on Odin or Naughty assets)

  • @snesmocha
    @snesmocha Місяць тому

    Inheritance… in modern oop… that’s gonna lead to disasters untold. I’m not much of a Unity dev but can’t you just make a global function that can be referenced that checks for the entity state?

  • @mikhailhumphries
    @mikhailhumphries Місяць тому

    What was that system. thing?

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      The Action? it's a ready made delegate, with a void return type and that takes, in this case, a float. I only put system in front of it because I hadn't added the System namespace to the top of the class. See this video for more: ua-cam.com/video/J01z1F-du-E/v-deo.htmlsi=RWUbPiRSqMOxD8UG&t=398

  • @esnevip
    @esnevip Місяць тому

    Reupload less than a day in?

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому

      no? are you asking because of the thumbnail? UA-cam lets you test them.

    • @esnevip
      @esnevip Місяць тому

      @@GameDevBeginner if the thumbnail changed it might be why

    • @GameDevBeginner
      @GameDevBeginner  Місяць тому +1

      That'll be it then. There was a clear winner so I ended the thumbnail test early.

  • @Timmel7
    @Timmel7 Місяць тому +1

    From what I've heard here, I don't think I'd ever want to work with you. Instead of using the simplest principles to write good code, it seems like you write bad code and then put a lot of thought into justifying your misdeeds by misinterpreting those principles.