You Are Using Update Loop Wrong | Practical Unity Tutorials

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

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

  • @SoloManGames
    @SoloManGames 4 หลายเดือนก่อน +6

    "The reason why all those early access games on steam are always laggy is because their developers didn't subscribe to my channel" 😂 Subscribed!

  • @Future_Guy
    @Future_Guy ปีที่แล้ว +23

    During my early Unity years, I challenged myself to create modules without using "Update" except when you need to accept Inputs from the user.
    This is something that I still follow and have also taught this to many of the junior devs in my company. It's almost always part of my TODOs for writing any thing.
    Events are the best.

    • @tomiczdarko
      @tomiczdarko  ปีที่แล้ว +8

      New unity input system is also even based, so you don't need to run input anymore in an update loop (of course not all input). And btw share this video to juniors in your company, would appreciate :D

  • @halfbakedproductions7887
    @halfbakedproductions7887 ปีที่แล้ว +7

    I've always done GetComponent just once in Awake() or Start() and assigning it to a variable within the object. Never thought of just dragging the reference in the Inspector, but good to know.

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

    Man, this video is just insane, thank you so much. I subscribed.

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

    Man, please never leave youtube, my game fps gone from 15 fps to 144fps (monitor max fps), thanks 💯

  • @flowisle-bg3pi
    @flowisle-bg3pi ปีที่แล้ว +1

    Just the kind of content I was looking for!

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

    I like your presentation. The only one I truly understand. Please keep these coming. Subscribed, and liked all of your posts.

  • @diligencehumility6971
    @diligencehumility6971 ปีที่แล้ว +8

    The only thing you should do each frame is listen for player input. Everything else should be event based. Send out an event (I like Action/Func), and whatever system interested in knowing can subscribe to that event.

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

      Even player input can be much easier to have as event-based thanks to the new input system. Once you've figured it out, it's so much nicer than doing crappy and possibly expensive checks in Update using if statements. The code is cleaner too.

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

      That sounds like a nightmare to debug.😮

  • @AimenG-h7y
    @AimenG-h7y ปีที่แล้ว

    Thank you ! your way of explaining is very soothing and easy ! THANKS

  • @jean-claudelemieux8877
    @jean-claudelemieux8877 ปีที่แล้ว +28

    My friend, your audio only goes into my left ear.

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

      Thanks for pointing out! Will fix in the next video.

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

      My brother, he is standing on our left.

  • @Justin-sv1if
    @Justin-sv1if ปีที่แล้ว +3

    This was an awesome video! Can't wait for more, specifically on coroutines!

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

      Coming soon :) I always wanted to give in depth tutorial on coroutines because I think other covered only surface :)

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

    Great video, I have subscribed.

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

    This is really great information.

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

    thanks for the advice, very useful!

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

    Svaka cast! Ovakvi detaljni tutorijali su nam potrebni!
    Kada sam poceo game development, moja prva prosta android igrica "Falling blocks" je jedva radila na telefonu upravo zbog lose optimizacije! Ali godinu dana kasnije, sa zavrsenom IT akademijom i dobrim znanjem C# jezika, sada svaka igrica radi u 60+ fps

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

    nice content, you got your 1000th subscriber 😁

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

    This video feels like a marvel henchman is menacingly teaching me C# instead of interrogating me lol

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

    Coroutine: be aware that this is not multi-threading. The coroutines still run on the main thread, just making it easier organize code that should run for a specific time. But its not optimizing CPU core usage.

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

    You have to be careful with OnTriggerEnter or OnCollisionEnter too. And don't forget about the collision filter.
    What can you say about UniTask? As an alternative to updates and coroutines?
    Suppose we run a UniTask loop with Delay Frame(120) or just Delay(seconds) So that the check does not take place every second, but only a certain number of times per minute?

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

    Thanks for the video man.. (The sound was a little off coz your voice came only from the left speaker, just a notice) Waiting for more informative videos ;)

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

      You welcome! I did mistake during recording, but that is improved in my latest video. Hope you will enjoy my other videos, and feel free to join our discord :)

  • @CBETVN
    @CBETVN 2 หลายเดือนก่อน

    You are hilarious dude. Subscribed.

  • @saniel2748
    @saniel2748 ปีที่แล้ว +4

    Around 6:30 you say that update loop can only be disabled when game object is disabled, which is not true at all. You can disable individual components using Behaviour.enabled property

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

      I agree. Though the point is the same :)

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

      @@tomiczdarko Well its much less risky to disable single component. You just gonna have to remember that GetComponent call will require "includeDisabled" set to true. I'd say its actually more powerful if combined with events. Say your component is disabled by default but it subscribes to some events. When events fire -> Component will enable itself -> Do the job -> Go back to disabled state.
      I'd rather do that than coroutines since coroutines are annoying to manage, can be especially annoying if you start multiple and then you're gonna deal with interruption nonsense

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

      @@saniel2748 I do agree. I wanted to bring awareness about coroutine to beginners, they can use them instead of an Update loop, especially if they need to wait for something and then finish which is way harder to do with an update loop. There are countless ways to do certain things, every problem has it's own approach, I didn't have time in this video to go with every possible endge case, just wanted to bring awareness. Anyway, thanks for the comments, really appreciate :)

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

    My left ear enjoyed the video

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

      Ahah you made my day 😂

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

    i really dont like to put anything in update and i try avoiding the most but sometimes im just not sure how to update a value without a start point, but as you stated, events is good to go

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

    i luckly can change my Audio out to mono because only left is weird haha. Thanks for the video anyway

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

    Science you explain you can also explain about FIXEDUPDATE
    You are the first one I finally got to understand thank you

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

      FixedUpdate is coming very soon :)

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

    That is a cool AR game.

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

    is doing some thing like decreasing the amount of times you want to call your function in your update with some cooldown using X-=time.deltatime bad?

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

      It’s not. It’s a unity way to decrease or increase passed time.

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

    very good content!

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

    When I really need to make check in update. I will always go for fixedUpdate and if I can go even lower I will create a slowUpdate I only call 4x per second for some AI check for example and when they are out of the camera bounds I disable them. No one notice the difference in game except the fps.

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

    your voice is entirely only on the left audio channel...

  • @MK-lk7nc
    @MK-lk7nc ปีที่แล้ว +2

    Great video, great tips. Good job. Here's something else I like to do, maybe worth a try sometime. Good for performance, code readability, portability and coder sanity.
    In a monobehavior sub-class definition that I know I will need to call many of, often, I'll create a static list of the class within its definition. then OnEnable, instances of that class call a static method to register themselves within that static list if they don't already exist within it, and the app is running. and OnDisable, they unregister themselves under same conditions. Then I have one static 'UpdateAll' function that iterates through the list and updates them en masse, and as much as possible, without calling specific functions within the class implementations. Then once - per -often, i call TheClassName.UpdateAll(), and if any are registered, they update. This seems to speed things up quite a bit, even though internally it's probably very similar to what Unity does under the hood, I suspect there's added expense with the standard way from context switching or something, thread allocation who knows.

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

      What you are taking about is called an observer pattern. I use it often in tasks where i need to observe and make changes on input.

    • @MK-lk7nc
      @MK-lk7nc ปีที่แล้ว +1

      @@tomiczdarko oh thanks for that clarification, I figured there was a formal title for it but didn't know it. Yes very helpful sometimes.

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

      Manually calling an Update function for a list of GameObjects is a known optimization tip and it is way faster (if you have A LOT of objects of course) than Unity's built in automatic Update calls, because Unity does the Update callbacks from the C++ side, which need to be routed to the C# side to be executed, and that adds a small overhead for each call. And that's (one of the many reasons) why DOTS/ECS is fast: it does process Entities by groups, not individually.

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

    Hey, what’s the AR game you showed a clip of here?

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

      Skeletons AR 2022, it's only available on Android as an open beta.

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

    Great video again ❤

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

    Coroutines are easy but you need to be aware how many times you are calling it, I often find myself in situations where my coroutine is getting called multiple times which results in erratic behavior and I spent hours figuring out what is wrong.

  • @alex.artechtattoo
    @alex.artechtattoo ปีที่แล้ว +1

    Thanks a lot for such great content, mate! Sub!

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

    good one..

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

    But try this
    Public class EnemyController : Monobehaviour
    {
    float health = 100;
    bool IsDead {
    get ;
    set {
    IsDead = value;
    if(IsDead) CheckDead(); }}
    }
    Void CheckDead()
    {
    Destroy(gameobject);
    }

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

    Subbed

  • @j.o.t.u.n.n
    @j.o.t.u.n.n ปีที่แล้ว +1

    At the beginning u felt if i didnt hand over ten k i was going to my knee caps slapped. So i subed instead

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

    Join our discord community and get the best Unity tips discord.gg/4CqnGESuRn. And sorry for the audio.

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

    You seriously need to check the audio using headphones when editing your video.

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

      I do. I am new to editing and made a mistake, hope it is better in my newer videos :)

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

    let me guess you will talk about fixed update

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

    RIP my left ear

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

    Umh, I think you really scratched the surface here. Okay, those 3000/frame checks will take some time, but its not the most important thing here. First of all, you need to learn habit to periodically check for stuff that is less important. For example, enemy can check for path to your player every 0.25 or every 0.5s or so. Promity check does not need to fire every frame, it can five every brief time, like 0.1s. But the most important is that every Update() call comes with a cost even if its empty, due to Unity costly marshalling operation. So instead of having 1000 entiteis with Update() loop, its actually much much better to have one object that iterates over list of 1000 enemies and manually calls method that updates logic (this pattern also makes easy to include levers how often you need to timeslice that particular entity type).

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

      Good comment! I recommend everyone to read this.

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

      @@tomiczdarko Thank you!

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

    Bros been insulting everyone whos looking at the thumbnail

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

    cool... like

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

    I cannot feel my right ear

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

    Mate you recorded your video mono, its pretty hard to listen to you. Still, the topic you talk about is a really nice one.

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

    Ähmm, even 100 000 checks per second is nothing. This is not where the bottleneck typically is for a Unity game. What is expensive are api calls.

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

    I quit unity then i moved to defold engine 2d and 3d lua ggame engine.

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

    Audio only in left ear, can't watch.

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

      Will be fixed in the next video, sorry :)

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

    Well, very bad video that will lead you to the wrong way.

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

    I disliked in first milisecond.

    • @Good.Idea.Zlovakia
      @Good.Idea.Zlovakia 11 หลายเดือนก่อน +1

      Me too. Because of sound. But after a while I changed my mind because content is on point.

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

    My left ear enjoyed this