How to Use Entity Command Buffers in Unity ECS - Unity DOTS Tutorial [ECS Ver. 0.17]

  Рет қаралды 9,585

Turbo Makes Games

Turbo Makes Games

Күн бұрын

❗❗ Caution: This video was made with an older version of ECS. See pinned comment for further Details ❗❗
📌 Download the project files from this video: tmg.dev/ECS_ECB 📌
👨‍💻 Download the code featured in this video: tmg.dev/ECB_Code 👨‍💻
💻 My Game Development Setup: tmg.dev/GameDevPC 💻
👇 See below for time stamps 👇
- 0:00 - Entity Command Buffers
- 1:18 - What is an Entity Command Buffer?
- 2:39 - Advantages Over Entity Managers
- 3:44 - Built-in Sync Points
- 5:23 - More Advantages of Entity Command Buffers
- 5:57 - Tips for Using Entity Command Buffers
- 7:03 - [Tutorial] Project Overview
- 7:37 - [Tutorial] Creating an Entity Command Buffer
- 9:33 - [Tutorial] Adding Commands to the Buffer
- 10:57 - Entity Command Buffer Demonstration
- 11:38 - [Tutorial] Parallel Entity Command Buffer
- 16:02 - Final Demonstration and Wrap-up
Links:
Blog: tmg.dev
Twitch: / turbomakesgames
Twitter: / turbomakesgames
Game Studio: homecookedgames.com
GitHub: github.com/JohnnyTurbo
Music by: Joakim Karud / joakimkarud
📸 My Camera Gear: tmg.dev/CameraGear 📸
🎮 Let me know what other topics you want to learn about 🎮
#GameDevelopment #Unity3D #MadeWithUnity
Please 'Like' this video and share it with anyone who is interested in video game development.
Subscribe to the channel for much more independent video game developer related content including tutorials, design breakdowns, industry events, VLOGs, and much more fun stuff!

Пікірлер: 49
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
❗❗ *Caution:* This video was made using an older version of Unity ECS. While the core concepts remain the same, some of the API and workflows have changed as of ECS version 1.0. I would recommend checking out my ECS 1.0 tutorial video for a good overview of the latest standards for Unity’s DOTS: kzbin.info/www/bejne/f4CZkGmPlL6Imqc Once again, the theory behind the concepts of this video are still relevant, however the API has changed. Stay tuned for further updated videos on this subject. Please let me know if you have any questions either here or in our Discord community: tmg.dev/Discord UPDATE: As of entities version 0.50 (released March 2022) you can no longer pass an entity query into an operation i.e. ecb.AddComponent(spawnerQuery) There is an updated API specific for entity queries. So in this case you'd do: ecb.AddComponentForEntityQuery(spawnerQuery)
@bAb1kk
@bAb1kk Жыл бұрын
That's why I'd scrolled to comments section :D
@FlipYourLearning
@FlipYourLearning 2 жыл бұрын
The perfect follow-up to the Entity Manager video.
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
Awesome, thank you!
@nizarbelhiba
@nizarbelhiba 3 жыл бұрын
Super Video as usual. Can u make a video about subscene please? Best approach tips and hints I don't know...etc..? My main concern is issue with gameobject which I turn to an entity through a subscene... I have the impression that its less performant
@TurboMakesGames
@TurboMakesGames 3 жыл бұрын
Certainly. I did a video on subscenes a while back, but it's definitely time for an updated version that goes a bit more in depth
@rogercabo5545
@rogercabo5545 2 жыл бұрын
​@@TurboMakesGames Yes would be great! posted a comment on this
@i2frame
@i2frame 2 жыл бұрын
You are so cool!
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
😎😎😎
@rogercabo5545
@rogercabo5545 2 жыл бұрын
​Great Job! I faced a simple problem, but never find any solution. I like to have a static SubScene, no movement. This sub scene should be loaded and destroyed at any time. Think about Lamps and street properties. Now I want to use a standard game object eg. a car (managed GO) and want to hit these street objects and make them disappear if the car hits. Seems to be impossible to me. That's a very common situation for any game where the player hits a static object. Did you have a idea how to realize? Would be great to have a video tutorial on this.
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
Thanks for the suggestion, that does sound like a cool idea for a tutorial! For context, what is the exact part of this you are having problems with? When working with subscenes, all the GameObjects in a subscene will be converted to Entities at runtime, and standard GameObjects will not be able to collide with or otherwise interact with them. If you still want to use a GameObject for your car, then you'll have to have an Entity mirror the position of your car to detect when it collided with a streetlamp. Or let me know if there are any other issues you are facing with that 😊
@rogercabo5545
@rogercabo5545 2 жыл бұрын
@@TurboMakesGamesGood idea to have a cloned dots collider based on a car position and rotation, that's acts in dots system. 1st problem, get the entity position/rotation/index of the hited object back into the standard game world. Then we are able to replace the entity instantaneous with a standard gameobject that destroys/falls down into 20-100 pieces. During performance reasons perhaps it's better to replace the dots entity with a standard destroyable gameobject on hit. Think about if we pack all destroyable pieces additionally into the dots physics calculation, it multiply colliders by perhaps 40 times or more. If we have ~500 hitable entities in the current player area, then we got 500*40 with all the pieces. 2nd problem optional, on loading a single Subscene, are we able to identify specified entities by any tag/value, so they stay already destroyed? If a player activate loading a Subscene by any trigger, then only entities 2, 50, 76, 5, (containing the colliders) should not show up. Would be really great to have a tutorial about this and to solve problems.
@Zwanderp
@Zwanderp 2 жыл бұрын
Hey Turbo, thanks for making these tutorials, they are invaluable. Perhaps you have some insight into an issue I am working through. I have the spawning of things working fine, but I would like to store references to the spawned entities within the spawner. The ECB returns a temporary entity handle when instantiating, so I cannot store that. Do you know what the correct way to acquire the entities after the ECB is executed is?
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
Hey Alex, glad to hear you've been finding these videos helpful! Yeah that's always an interesting situation - one solution I've used is to have a dynamic buffer with two entity fields on it one for the new entity being spawned then the other for the one that needs to reference the new one. You can then use ecb.AppendToBuffer() passing in a new buffer element of both entities. Then you have a system that runs after the ecb plays back (i.e. in the presentation system group) that goes through that dynamic buffer and finalizes the references. Kindof a hacky workaround for now, but it works. Might be a good idea for a quick video topic...
@Zwanderp
@Zwanderp 2 жыл бұрын
@@TurboMakesGames yeah that makes sense. Getting my head around using components to do small jobs that would normally be callbacks is hard. It feels excessive, but I guess it is the right way to do things in the ECS paradigm
@shapetvx5545
@shapetvx5545 2 жыл бұрын
A possible future video you could do is handling random level generation. Like a random dungeon generator where you use ECS to generate a dungeon with random rooms. Almost all guides I find online start with a prebuild scene they then convert to ECS. Almost nothing about completely random generation where you do not know the layout or other level information until runtime. Think like a dungeon crawler style game.
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
That sounds like a great idea for going deeper into creating game mechanics using ECS, thanks for the suggestion. Could you send me some of resources you've found online for random generation in ECS? Just want to see what they are doing now and how I could improve upon it, thanks!
@shapetvx5545
@shapetvx5545 2 жыл бұрын
​@@TurboMakesGames I am using the BSP Dungeon generation algorithm then applying to 3d. I found this Git Repo that implements BSP in ECS github.com/NagaChiang/unity-ecs-bsp-dungeon-generation This thread on Unity talks about procedural generation of a Minecraft like map. Similar concepts apply. forum.unity.com/threads/solved-conciling-procedural-generation-with-ecs.545352/ Outside of those I have really struggled to find anything good and not outdated on procedural generation and ECS. It has been mostly parsing through people's GitHub repos. No real KZbin videos exist (or if they do they exist deep within KZbin and/or I just have been googling the wrong things). It is really untapped as far as videos go. ECS seems like it would be really awesome for procedural generation yet the lack of resources online makes me wonder if I am wrong about this. The main two struggles I have had - How do I assign meshes/mat/other assets to an entity generated by a System? I made a few systems that implemented the BSP algorithm. I got my entities generated for the rooms but then could not figure out how to then generate meshes for the rooms/hallways within ECS. (A possible solution and also maybe a video is integrating the Addressable Library with ECS. I saw some posts on the Unity forums about people doing this but couldn't figure it out) One approach I saw that felt like a really messy and wrong way of doing it is to have a game object create template entities. Then make "populator" systems that oncreate make a copy of the template entities component data (meshes/materials/assets/etc) then look for entities that match the template and have a build flag component and use the template data to populate the entity. That seems really wrong - I also struggled with the idea of run once every once and a while systems. Like the systems that implements BSP. The solution I came to is to have a levelmanager entity that has a buildflag component. After the systems are finished building the data for a level they remove the build flag. When a new level needs to be built I add a rebuilddflag component to the level, have a system clean up current level data and add the buildflag back to the level entity. This seems to work alright but I think there is a better approach. I think that my approaches have been wrong so far and I probably had hammer nail syndrome where I was trying to do to much in ECS. Your video on spawners actually helped a lot. My current approach is to take any entity generation/management out of the ECS ecosystem and put that logic into a game object. Instead of having systems that builds a procedurally generated level I will do level generation in a game object then convert my level data to entities. Once a level is finished I have that same GO manage entity cleanup and build a new level. This approach seems cleaner and more in line with the conversion workflow most people are using for prebuilt levels but I am still messing with it. I will have a few bottle necks I'll have to optimize (with jobs?) for level generation but I will still see the performance boost during actual gameplay. Hopefully this is helpful to get an idea of some of the pitfalls new ECS coders fall into when approaching this sort of problem. Really any video you make on how you with more ECS experience would approach the problem of procedural generation would be an improvement as resources on how to do it are sparse or hard to find. Even a high level no code video on how you would approach a problem, how you would structure your code, and general advice would be insanely useful as most ECS resources talk about the various parts of ECS but very few take all the parts and talk about the do's and don't of putting them together and when to use them. Anyway this turned into a wall of text. Your videos have been extremely helpful to me trying to figure out ECS. So thanks.
@myelinsheathxd
@myelinsheathxd 3 жыл бұрын
Nice system! All I am really curious to see in future is combine different parallel written algorithms in the one project! So everybody makes new techs in the same architecture! And then, at the end of the final project, an individual could utilize others' products easily in one project with robust performance!
@TurboMakesGames
@TurboMakesGames 3 жыл бұрын
Interesting take. That is one of the cool things about the ECS structure is that you can somewhat easily drop in and take out systems to change the behavior of the game.
@mufelo
@mufelo 3 жыл бұрын
Is it safe to listen to events (C#) from ECS systems or how would one best propagate thing like UI events in? I noticed you used Input.GetKey here, but cant imagine that being very flexible in a bigger project.
@TurboMakesGames
@TurboMakesGames 3 жыл бұрын
That's a good question and I'd have to do a bit more research to give you a definitive answer. However unfortunately, at this point there is still not the ECC equivalent for a number of things so we still have to use things from traditional Unity to accomplish them, even if it is not necessarily ideal especially for a project at scale. Ideally you'd want to design your code around this to minimize the communication between the ECS world and standard Unity world
@mufelo
@mufelo 3 жыл бұрын
@@TurboMakesGames Indeed. My question pertains mostly to nicer patterns of user input from UI, new input system, etc. I remember having seen some kind of event queue implementation somewhere a year or two ago to address this problem, but could not find it. The biggest problems I find myself with when doing ECS is generally that and making anything look good since animations arent there quite yet (didnt try the animation package tho). I find the data oriented systems a logical way to build out worlds and would very much like to write some of my games this way.
@Songfugel
@Songfugel 2 жыл бұрын
Damn, ECB seems so much simpler now than in the early years of ECS when I was struggling to figure these things out, seems almost streamlined now. However, I kinda don't like these magic variables they have introduced all over the system to get key functionality, I wish there was a more unified IntelliSense supported way to access/see them. Like writing ecs and intellise would show you all scoped magic variables related to ecs Also some of the most frequently used components do not need names that long, particularly that index and Dependency. Why not just call the EntityInQueryIndex it index, queryIndex or even entityIndex and be done with it, it is a scoped variable anyway, no reason to have such a monstrous name on a variable that mainly causes more confusion with its name than helps.
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
I agree, many of the class/variable names in ECS are needlessly long, hoping that gets cleaned up as development continues.
@RogerValor
@RogerValor 2 жыл бұрын
I would also think that e.g. having some kind of namespace based placeholder if the names have to be exact would help. It would not require to adapt the editor to read the magic.
@AVENGER4565
@AVENGER4565 2 жыл бұрын
Thanks for the lesson. The question about instantiating: we all can see at 00:00 the capsule spawns in the center at first, then we set its transform to were it should be, and its teleports toward that point. Its looks awfull. I have same issue. I ve tried to fix it by spawning objects somewhere else, at the point where noone can see them bilnking. But that solution kinda stupid i guess. Whats surprised me, adding to my spawn system [UpdateInGroup(typeof(InitializationSystemGroup))], solved this problem, but i can still see at the end of video at your case. How to solve it? Thank you!
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
It has to do with the order at when you are spawning entities in relation to other systems. The translation component isn't actually the physical location of an entity in the world, it is just a "helper" component to set values in the LocalToWorld which is the true world position/orientation/scale. If you spawn your entities after that system runs but before rendering, the entity gets spawned, but the LocalToWorld is set to the origin - meaning that is where the entity will render. The next frame the LocalToWorld is updated to the correct position. The reason your solution works is because your entity is spawned before the system runs to update the LocalToWorld component, thus moving it to the correct location before rendering. Hope that made sense, I'm planning a video soon that will discuss this.
@AVENGER4565
@AVENGER4565 2 жыл бұрын
@@TurboMakesGames Is that possible to instantiate object with LocalToWorld different from the origin? I ve tried to find way to do this, but likely its not possible. Thats great that it will be video about this. I will be waiting for it. Thanks for the answer. Also i have another problem. The ECS approach have restriction that components can only be structures, which means that i can only use value types in them. Usually we can use in structures reference types also, but ECS noticing that and shows error. I understand why is that, but what if i have objects in game with behaviour that barely can be described by structure? I solve that problem by creating class models with their logics. Due to all Entites have Id, i always can find their models in dictionary by it. For example: in my game i have two or more planets and a ship. I want ship be patroling between two or more of them. For that purpose i use public static class which contains models for each planet and ship, and when i need to, for exaple, land a ship to the planet i find model of ship and planet i call IPlanet.AcceptLandingRequest(IShip) method of the planet, which means to add it into landing queue and some another logic. Until dock of planet is full ship it the queue, and its should orbit planet and placing them into dictionary. When another system calls IPlanet.SendShipToFlight(IShip) the next ship in landing queue marks as landed, so i destroy its Entity. The problem is that ECS forbid me to use Burst compiler when i use dictinioaries in tasks. I understand why, but i doubt that i use ECS in right way. I have tried to ask StackOverflow, but noone gived me best practice in such situation. Projects in public repositories with ECS usually not contain logic that more complex that just simply move something somwhere in tricky way. Sorry about such long question. But maybe you can give me a clue how to handle that situation. Thanks for attention!
@kaykyl6607
@kaykyl6607 10 ай бұрын
Is it possible to use command buffers on the OnCreate method?
@TurboMakesGames
@TurboMakesGames 10 ай бұрын
Yes it is!
@GoodNewsJim
@GoodNewsJim 2 жыл бұрын
Look I'm almost done with a MOBA and MMORPG. Turbo, if I become a bazillionaire on my MMO, track me down, and demand money. I'm generous, and give you a gorilla manilla of greenbacks. This helped me get rolling, just gotta figure out how to destroy an entity, which should be trivial. I could ask on Unity discord forum if no one knows. Edit: Actually it is hilarious, my code worked first compile. Remember Turbo, you need to find me and demand money if make a ton on Starfighter General on Steam.
@casproduksiyoncasoyun4497
@casproduksiyoncasoyun4497 2 жыл бұрын
With Entities 0.50, the ecb.AddComponent(spawnerQuery) part failed. Error: cannot convert from 'Unity.Entities.EntityQuery' to 'Unity.Entities.Entity' When I looked at the helper (at Visual Studio), I could define only the Entity or Entity NativeArray in arg (Entity e, ShouldSpawntag component),(Entity e),(NativeArray),(NativeArray, ShouldSpawnTag component). I have tried to ecb.CreateEntity() and EntityManager.AddComponent(entity) and than put in ecb.AddComponent() but unity created only empty enities. How we can solve this problem? P.S. Of course, I can easily solve it with Entities.ForEach, but I haven't solve it with Entity Command Buffer.
@casproduksiyoncasoyun4497
@casproduksiyoncasoyun4497 2 жыл бұрын
Finally I have found the solution for Entities 0.50 package update. In SpawnerControlSystem.cs: var spawnerQuery = EntityManager.CreateEntityQuery(typeof(EntitySpawnData)); var e = spawnerQuery.ToEntityArray(Unity.Collections.Allocator.Temp); // I added this code. if (Input.GetKeyDown(KeyCode.Y)) { ecb.AddComponent(e); // old version is "ecb.AddComponent(spawnerQuery); } In DestroyEntitiesSystem: var capsuleQuery = EntityManager.CreateEntityQuery(typeof(CapsuleTag)); var e = capsuleQuery.ToEntityArray(Unity.Collections.Allocator.Temp);// I added this code. ecb.DestroyEntity(e); //old version is ecb.DestroyEntity(capsuleQuery);
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
Yes, this API was changed in 0.50, you now have to do AddComponentForEntityQuery(spawnerQuery) - hope that helps!
@Mr_Anderson_one
@Mr_Anderson_one 3 жыл бұрын
Do you think DOTS will become the default in unity over time?
@Super_Hex
@Super_Hex 3 жыл бұрын
That's what they themselves said at some point. Will it remove MonoBehaviour completely? Honestly, I don't think so. If you haven't used it yet and you're thinking about picking it up, just keep in mind it's not exactly ready yet, and some functionality might require additional tinkering.
@TurboMakesGames
@TurboMakesGames 2 жыл бұрын
Can't predict the future, but if that were to ever happen it would not be for a VERY long time. Feels like DOTS is still a few years from being fully production ready then they'd have to make it much more approachable to newer developers whether that is through visual scripting or some other means - while data oriented programming is much more efficient than object-oriented, object-oriented is still much easier for humans to understand. So even if DOTS becomes the recommended way to use Unity, I think MonoBehaviours will still have a strong presence.
@calccalccalc
@calccalccalc 2 жыл бұрын
To me, ECS is like the MVC of game and simulation development. DOTS, hosting this superior architectural pattern to OOP, will therefore likely become more and more commonplace in Unity development, but not suddenly. It will take years for the old generation to become sufficiently less influential for the DOTS floodgates to open.
@manuillo94
@manuillo94 3 жыл бұрын
At this point I'm pretty confused about ECS and its usefulness. It looks to me like it's too complex and too finicky to be worth learning. More so since it just solve two or three very specific scenarios.
@TurboMakesGames
@TurboMakesGames 3 жыл бұрын
That's a fair point. I do see many applications for ECS, a lot of them outside of games too. However if ECS doesn't solve the particular problems you are trying to solve, and you don't have much of an interest of just learning and experimenting with the tech, then yeah I agree - at this point it may not be worth it to learn all the ins and outs of the tech right now. I'll keep experimenting and learning as the tech evolves and hopefully we get to a point where it is much more streamlined to implement and its benefits can be realized in more types of projects. Cheers!
@diopan9245
@diopan9245 3 жыл бұрын
I'm actually developing a videogame with thousands of fishes swimming in the scenario. They swim in schools, are able to hunt other fishes, follow "choreographic" paths , interact in some way with the main (human) character and so on. They obviously are also able to escape from predators. I've first implemented the game with "traditional" gameobjects. Then I moved to an hybrid approach: the main char is a Gameobject, the fishes are Entities. With the first implementation I was able to manage one-two hundred fishes With the ECS implementation I am able to manage more than two thousands fishes on my old i3-4160 @3.60ghz. So, if you need to manage thousand of "things" , I suggest you ECS. It's the only way (on Unity3d) for having decent performances in crowded scenarios.
@manuillo94
@manuillo94 3 жыл бұрын
@@diopan9245 That is very interesting but I see this approach way more useful in a simulation than in a videogame. In a simulation where physics matter I consider ECS the best and probably the only solution possible. But in a videogame, where the user can not track the behaviour of thousands of entities, you could probably fake it. I think this tech is awesome and will have many applications, just not for gaming specifically.
@diopan9245
@diopan9245 3 жыл бұрын
@@manuillo94 I agree with you ... partially. Let me explain: If you want to develop an FPS game, use the classic approach. If you want to develop a "third person fish Shepherd" game (I'm sorry , I didn't find a better definition for my game) then ECS seems to be the only solution. just to clarify, I'll post here some videos of the game I'm developing: kzbin.info/www/bejne/mqeof6eArKaLoqc kzbin.info/www/bejne/jpSniYyvmbVjbqc And, for example, I think that ECS should be useful also in developing a new Rome Total War with more soldiers on the ground.
@calccalccalc
@calccalccalc 2 жыл бұрын
I work with unity in a business context, and nearly every day I'm seeing use-cases for ECS in our project, which honestly makes coding in this OOP codebase all the more begrudging, because I've experimented with ECS enough now to know what it's capable of. If you're just wanting to make inherently low-complexity projects, you can definitely stick with OOP... ...but for AAA to medium-scope games, games on hardware-restricted standalone platforms like android/switch, and business applications, I'd say ECS is a good fit. So far in my experience, ECS is fantastic for reducing code complexity, handling data (both internal, and external like anything from hosted or cloud databases), and will have you utilising your hardware resources more efficiently, considerably so when taking into account the rest of DOTS. In my project, we take data from azure, which we then map to data structures, which becomes attached to GameObject instances. If you need to access that data from anywhere else (often you do), you need a reference to the GameObject that originally consumed that data, which leads to spaghettification (usually thanks to commonplace over-zealous management) and in the best case scenario, it just adds complexity, which isn't ideal when there is a lower complexity solution out there. Also, we use MRTK (Unity Hololens SDK) and even though it is an amazing tool, their buttons for some reason are super expensive to render. My plan is to convert them to DOTS along with the whole project, that would result in both architectural simplicity and better performance. This is a specific case, but more and more Unity jobs are becoming like mine, and I don't think we're going back. I'd say that you can enjoy coding without ECS/DOTS, but you'll have a better life in the long run utilising it. I say this because the Unity job market is trending and Unity it's self is being realised as a more versatile tool by forward-thinking businesses. I think it will be more than likely that in the not-too-distant future, you'll find that Unity will surpass Flash with it's ubiquity (and security lol). I mean, it's already there in the game market, but for providing high-fidelity, *juicy* experiences for marketing, end-user dashboards, general applications, etc, on all devices large and small, it will no doubt prove it's self to be a game changer, with DOTS at the vanguard.
How to Use Dynamic Buffers in Unity ECS - DOTS Tutorial [ECS Ver. 0.17]
16:28
О, сосисочки! (Или корейская уличная еда?)
00:32
Кушать Хочу
Рет қаралды 8 МЛН
OMG 😨 Era o tênis dela 🤬
00:19
Polar em português
Рет қаралды 10 МЛН
[柴犬ASMR]曼玉Manyu&小白Bai 毛发护理Spa asmr
01:00
是曼玉不是鳗鱼
Рет қаралды 46 МЛН
Using Entity Command Buffers - Unite Copenhagen
38:16
Unity
Рет қаралды 10 М.
The power of command buffers in Unity | JellyCar Worlds
8:43
Unity DOTS Entity Manager And Command Buffer
8:32
Developers Hub
Рет қаралды 867
Should You Use DOTS in 2024? (plus what is Unity ECS)
30:15
Turbo Makes Games
Рет қаралды 34 М.
INSANE shooter with WAY too many bullets (Unity ECS Tutorial) - PART 1
15:20
Sasquatch B Studios
Рет қаралды 4,3 М.
Создаем игру на Unity DOTS #1
18:09
ABG
Рет қаралды 10 М.
О, сосисочки! (Или корейская уличная еда?)
00:32
Кушать Хочу
Рет қаралды 8 МЛН