How to handle Events in Unity DOTS! (C# Events from ECS)

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ต.ค. 2024
  • ✅ Get the Project files and Utilities at unitycodemonke...
    Let's learn 2 ways we can handle Events in Unity DOTS! The final class is included in the Project Files.
    Unity DOTS / ECS Tutorials
    • Unity DOTS / ECS Tutor...
    What are Events? (C# Basics)
    • What are Events? (C# B...
    If you have any questions post them in the comments and I'll do my best to answer them.
    🔔 Subscribe for more Unity Tutorials / @codemonkeyunity
    See you next time!
    📍 Support on Patreon / unitycodemonkey
    🤖 Join the Community Discord / discord
    📦 Grab the Game Bundle at unitycodemonke...
    📝 Get the Code Monkey Utilities at unitycodemonke...
    #unitytutorial #unity3d #unity2d
    --------------------------------------------------------------------
    Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.
    I've been developing games for several years with 7 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.
    You can see my games at www.endlessloopstudios.com
    --------------------------------------------------------------------
    Website: unitycodemonke...
    Twitter: / unitycodemonkey
    Facebook: / unitycodemonkey

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

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

    Here's how you can handle C# Events while working with Unity DOTS! Stay tuned for the complete DOTS game coming soon!
    🌐 Have you found the videos Helpful and Valuable?
    ❤️ Support on Patreon www.patreon.com/unitycodemonkey or get the Game Bundle unitycodemonkey.com/gamebundle.php

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

    I think this is the most important ECS video that Code Monkey has done. Thank you Code Monkey.

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

    This April will be my 2 year anniversary since learning programming and Unity, The tutorial that got me hooked was a Flappy Bird Tutorial, so this is an inspiring milestone to be 'starting over' again and continuing a life of learning. Cheers!

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

    Been trying to figure out how to do event like systems in the same frame for a month now. Thanks for the tips!

  • @colinwithers4260
    @colinwithers4260 ปีที่แล้ว +5

    Update for ECSv1.0 ? :)

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

    Thanks 🧡! This helped me with a project I'm working on.

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

      Nice! I'm glad this video helped!
      I'm currently researching various methods of handling events in DOTS myself

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

    Code Monkey - Could you do a video on how you learn and research new material like this? You seem to learn it very fast, and spin it back to us. Id love to see what your research and production life is like. Thanks again! This is great!

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

      Yup I've thought about covering that, just need to figure out how to present it.
      Essentially I just go through the documentation and go through a lot of trial and error. This one took me probably around 20h or so trying all sorts of approaches before I found one that works.

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

      @@CodeMonkeyUnity Amazing work, Thank you!

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

    Great video. I will need to check out all your vids on DOTS to learn it myself. Congrats on the 100k subscriber's award! :)

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

    Congrats on your play button! Keep up the great content!

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

    Hey mate, love your videos and keep up the awesome work!
    I have another solution for events in DOTS, thought you might be interested with another way to do things, like always, with coding, every solution has its pros and cons.
    Using DOTS value changed filter as an events for entities, you can have events that uses burst complie and have no syncing points without the need to create and destroy entities too, using the full adventages of DOTS, however a little tricky to setup but worth it.
    Create an entity component with enums as it value, IComponentData for events that are like triggers or IBufferElementData for events that need to be queued, can use ComponentDataProxy wrapper for GameObject for events that need to listen for UI button clicks.
    Quick example of a UI button click event:
    [Serializable][Flags] public enum BUTTON_EVENT : byte { NONE, CLICK }
    public struct InputButtonData : IComponentData {
    public BUTTON_EVENT Value;
    }
    [DisallowMultipleComponent]
    public class InputButtonComponent : ComponentDataProxy {
    public void OnClick () {
    Value = new InputButtonMoveData { Value = BUTTON_EVENT.CLICK };
    }
    }
    Entities.WithChangeFilter ()
    .ForEach ((ref InputButtonData evt) => {
    evt.Value = BUTTON_EVENT.NONE;
    // Do Stuff
    }
    When changing the value back to a NONE doesn’t trigger the change filter on the entity either, the event is clean and ready to be triggered again, many other ways using the DOTS change filter for event like structures, hope you find this useful.

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

      That works but only works inside the DOTS World, using that method you cannot broadcast it out to managed objects.
      What lead me to research Events was how I was working on Flappy Bird in DOTS and I wanted to update the UI with the score whenever the pipes passed. The UI is built in the normal way so I needed the DOTS System to broadcast an event that the UI could capture.
      But yeah if you just need to capture your events inside the DOTS World then that approach is excellent.

  • @Lythowastaken
    @Lythowastaken 4 ปีที่แล้ว

    Hey, Im working my way into DOTS and found ur Video. First to say, nice video!
    The way how I need to handle Events in DOTS still looks unclean and I hope in the future Unity will add a good way to use Events. If im thinking about stuff like OnPlayerDeath, OnPlayerRespawn and and and more I guess I would get huge headache if I need to do this with DOTS.
    Or maybe I need to think different in DOTS. But all my previous applications were heavy event based so I guess it will be hard to get out of this mindset :D Last year I saw an article about DOTS and how collision is handled. I hope this is better nowadays.

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

    Great video. And I actually wanted to make a suggestion, Not so much on the code side, but more on the organization side and I think it will help keep things a little more organized What I have done so far with my experimentation projects when it comes to writing entities is I have a folder in my project hierarchy called Data, Inside that folder, I have two additional folders, one called Entities and another called Systems. I put systems that work on entities inside their own folder with all C# files So if I have for example a PlayerMoveSystem, I have a folder called PlayerMove. Then when it comes to entities I do the same thing. I make a folder for each entity giving it a name, then any extra components I give that a seperate folder underneath the main entity. for anything that is common between several entities, I put that inside their own folder called common
    By doing this, through the project view, I am able to see what systems I have defined, as well as what entities and or components that each entity relies on (for the most part) unless it's using a common component.
    I know that there really aren't any best practices established yet, but I have found that this works well and gives a great quick glance visual representation of your data your working with right inside the project view

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Yeah organization definitely seems to be very important in DOTS since so many things will be split into a lot of files, which is great since it helps keep your code clean.
      I'm finishing up the Flappy Bird DOTS project and that's exactly what I?m doing, separating the Components and Systems into folders instead of 20 scripts in one folder.

  • @dann5295
    @dann5295 4 ปีที่แล้ว

    Great lesson! I'll surely be revisiting this a few more times

  • @Styx6363
    @Styx6363 4 ปีที่แล้ว

    I'm 18 and only have an intermediate understanding of Python, what would you recommend I start with to get my game development hobby rolling? Should I do an online course on C#? Or is there somewhere you would recommend I learn how C# is unity? Any advice would be greatly appreciated, I'm currently studying Maths,Physics and computer science at A-level. Keep up the awesome vids (:

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Regarding C# there's tons of content out there, I'm not familiar with anything in particular but I'm sure you'll find something.
      Regarding Unity watch this playlist which will get you acquainted with how the engine works:
      th-cam.com/play/PLzDRvYVwl53vxdAPq8OznBAdjf0eeiipT.html
      The maybe try making a simple game like this th-cam.com/video/b5Wpni9KPik/w-d-xo.html
      Best of luck!

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

    Nice video! Also expecting world LOD things mentioned in subscene video.

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

    i like the creating entity method but is it heavier than queue?

  • @andersencastaneda6080
    @andersencastaneda6080 4 ปีที่แล้ว

    Your video tutorials are awesome. Where do you find updated DOTS documentation?

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

      Mostly by reading the Manual and looking at the Changelog docs.unity3d.com/Packages/com.unity.entities@0.14/manual/index.html

  • @comtronic
    @comtronic 4 ปีที่แล้ว

    The black text on white background color scheme really gives me retro feeling coding..

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

    You could use in instead of ref for the Pipe component in ForEach.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Good point! I write ref automatically but I definitely have to remember to use in.

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

    Oh. It's so complexity

  • @andrewshandle
    @andrewshandle 4 ปีที่แล้ว

    Great video.
    I was thinking about creating an "Events" Entity and just add a new DataComponent to it to represent each "event", that way you could build separate Systems designed to handle each "event". Since you can't just subscribe to an event doing it this way, it doesn't have nearly the flexibility of your second implementation but it allows for a pure DOTS implementation.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Yup, if all you need is to capture your events while still inside the DOTS World then a simple component is all you need.

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

    Hi, have you figured out how to do UI event handling within DOTS? A tutorial on that would be great.

  • @ЕгорКречун-з7и
    @ЕгорКречун-з7и 4 ปีที่แล้ว

    very good video, but have you tried to make an entity that will store the component event and receive its data through EntityQuery.GetSingletonEntity ()?

  • @Marconius6
    @Marconius6 4 ปีที่แล้ว

    These work really well, thanks for the video!
    I do kinda feel like events are completely anti-DOTS though and you should just like, not use them at all. The whole point of events is to interrupt the flow of execution and jump over to somewhere else, which is exactly the thing you do *not* want to do in DOTS.
    A better design pattern in my opinion would be to set a flag or something in one process (like a collision check); then have a different system that just checks these flags and does something based on them. That way the two processes are separate, which means their dependencies are also kept separate. This allows collision detection to keep running on one thread, while you processing the results can run on another. The only time this doesn't work is if you really need something to immediately happen on collision (like destroying an entity), but in that case I would argue that should be in the same system and same job anyway, so you don't need an event.

  • @justinwhite2725
    @justinwhite2725 4 ปีที่แล้ว

    Thank you for this! I’ve been spawning entities to act as events and then have systems process them. It’s janky compared to c# events. @10:12 yeah.. kind of like that, except I have a different system looking for those events and processing them, which leads to a lot of systems that don’t run most of the time. Systems don’t run when there are no entities that match the criteria so they don’t waste processing time.

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

    Video Idea: Show ways to spawn Entities in Unity Dots. Spawn from Event. Spawn every 1 Second and so on. The Idea is based around a TowerDefense game. I couldn't find a good way to do this yet. Maybe you can spawn an Archtype that you have to define. Maybe you find a better way to spawn a Prefab so its easier.

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

      Yup, I covered how to spawn a Entity Prefab here: th-cam.com/video/pk-C_h0WJZs/w-d-xo.html

    • @Roadis
      @Roadis 4 ปีที่แล้ว

      ​@@CodeMonkeyUnity Damn how did I missed this Video very nice thanks. Btw. NetCode just got an update so it works again with newest Entities Version. So maybe you can start there with the Basics too.
      I tried it a bit seems to work fine.

  • @draknessxxdrankxu7384
    @draknessxxdrankxu7384 4 ปีที่แล้ว

    thanks a lot , very useful !

  • @Army21
    @Army21 4 ปีที่แล้ว

    How about this idea? Within jobs, create an entity event. Have some mono code that consumes those events and converts them into c# events, which can handle multiple subscribers.

  • @Gers217
    @Gers217 4 ปีที่แล้ว

    Well I think I have to watch the DOTS playlist to understand this

  • @turntupgamer878
    @turntupgamer878 4 ปีที่แล้ว

    Thank you. This is helpful but i am trying to learn to use trigger events with Hybrid ECS PhysicsShape. I have been trying for about a week now :(. I cannot find any simple enough tutorials or guides on it though. I joined your discord, hopefully someone else has done this!

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      DOTS Physics Triggers? I covered it here th-cam.com/video/B3SFWm9gkL8/w-d-xo.html

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

    Great video! Firing events through creation of entities with event component seems to be a right way of handling events in dots. But listening to events through system(c# event field) seems strange. As I understand communication between systems in ecs has to be handled only through components. But I may be wrong. Anyway, thanks for great dots tutorials!

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

      If you want to listen to events on another DOTS System then sure using just the component and not firing off the C# Event makes sense.
      This is more for the case where you want managed code to listen to events in DOTS, so for example the UI, which is currently not supported in DOTS, with this you can listen to the DOTS Event and update the managed UI objects.

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

    Dude get out of my head. I was literally thinking 20 mins ago how I might do events in ECS then you posted this video

    • @457Deniz457
      @457Deniz457 4 ปีที่แล้ว +2

      He hacked ur brain :P

    • @AniketSingh-hr8mi
      @AniketSingh-hr8mi 4 ปีที่แล้ว +2

      Next : How to read someone's mind in Unity DOTS! (C# Events from ECS)

  • @jytyp
    @jytyp 4 ปีที่แล้ว

    How do you get the art for your games? Do you make it yourself or some other way?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      I have bought some art but most of it I make it myself, usually by just modifying assets I already have.

  • @meetpurandare1163
    @meetpurandare1163 4 ปีที่แล้ว

    Nice video keep it up

  • @Spherous
    @Spherous 4 ปีที่แล้ว

    Love your tutorials, hate your light mode.

  • @NikolayRogchev
    @NikolayRogchev 4 ปีที่แล้ว

    I did not understand why do we need Event Component to fire the event? Can't we just fire the event directly in the job?

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

      The Job works on multiple Worker Threads whereas C# events can only be called from the Main Thread.
      If you called it directly from inside Entities.ForEach you would be giving up all the Multithreading performance benefits.

    • @NikolayRogchev
      @NikolayRogchev 4 ปีที่แล้ว

      @@CodeMonkeyUnity I see, so the best approach would be to abstract this boilerplate code to a generic class to be used across multiple projects?

  • @HOSTRASOKYRA
    @HOSTRASOKYRA 4 ปีที่แล้ว

    Please show the correct way to control the state of entities. For example, there are two cubes. Both are spinning. When a button is pressed one does not rotate.

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

      Could be a simple Tag component or maybe all cubes have a certain component holding a boolean for whether or not they should be manipulated. Depends a lot on the specific scenario.

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

      @@CodeMonkeyUnity it would be great to see a video about ISystemStateComponentData

  • @ArturasMalcevas
    @ArturasMalcevas 4 ปีที่แล้ว

    Ding!

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

    Hi dude,can you make a coding how to open crafting table like minecraft and put item

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Yup, I covered that exact system right here th-cam.com/video/LmQ6U3YkHHk/w-d-xo.html

  • @timwest9327
    @timwest9327 4 ปีที่แล้ว

    docs.unity3d.com/Manual/JobSystemJobDependencies.html It looks from this like you can create a job that depends on a job. so you can avoid you sync point by populating the native array in one job and then use that job as an input dependency for a 2nd job that reads the queue. This way you let Unity know that the 2nd job must run after the first without forcing a sync point.

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

      In order to fire the C# Event you need to be running without Burst and on the Main thread. So you need to use Run(); and cannot use Schedule();

  • @whiitehead
    @whiitehead 4 ปีที่แล้ว

    At the intro of your videos, it sounds like you say "inept tutorials".

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 ปีที่แล้ว

      Yeah my enunciation isn't perfectly clear so I can see how it might sound like that, I'm saying "in-depth tutorials"

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

    Can you make a video showing how to generate meshes/primitive shapes using DOTS/JOBS? A follow up to your older video here. th-cam.com/video/gmuHI_wsOgI/w-d-xo.html

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

      Yeah, my next video is covering Blob Assets and after that I will start working on converting my Mesh based animation system into DOTS.
      It's not possible to do 100% with DOTS since Mesh and Graphics can only be called on the main thread but everything else before the final draw can be made super fast.