Managing System Update Order in Unity ECS - Unity DOTS 2021 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to today's video we're going to be talking all about system update order and system grouping inside unity's entity component system which is of course part of their data oriented technology stack now this is an important topic because you know when we're developing in unity's ecs we're often going to have a lot of systems each kind of doing their own thing sometimes they depend on others sometimes they don't so it's really important that we you know group our systems and order them appropriately so things happen in correct orders recently i was participating in a game jam about cleaning up trash out of the ocean and i created a game called trash empire made with unity ecs i did put up a postmortem video on that if you did see it you know i mentioned that um kind of towards the end of developments i was running into some issues when it came to my system update order and job dependency management and all that now job dependency management is going to be a topic for another video coming up soon but today we're going to be focused on specifically system update order and grouping and just how you know showing that if you do kind of stay on top of it as you go it's pretty easy to manage and you won't really run into any headaches later on so the way i kind of want to structure this video is we're going to start off by showing you what unity does by default because you don't necessarily have to do any of these things and you can kind of create you know like a simple little tech demo without really needing to worry about any of this however things start to get a little bit more complicated and you have systems that are you know again kind of interweaving and maybe you know need to run in specific orders you know that's when you can kind of start getting yourself into a little bit of trouble now if you're still pretty new to unity's entity component system i would highly recommend watching the uh kind of intro to systems video that i did recently i'll put a link up in the card as well as in the description below but basically in that video it's going to go over what systems are and generally how they work so i think it's kind of important to have that understanding before we talk a little bit more about the management of them all right so let's get right into it i have unity open here there's not really a whole lot of stuff going on as far as the sample project is concerned today because most of the stuff is actually going to be happening inside the entity debugger and i just kind of want to show you what unity does by default so if we enter play mode here you'll see that you know basically you know all these systems start popping up inside the entity debugger here now you see that these are basically grouped into three kind of top level categories so we have the initialization update and prelate update so these three phases are going to happen every single frame and this basically comprises the main player loop here so if we actually kind of like expand these and see what's under each of them you see that there are these things known as system groups just right in the top level of these so these are basically just kind of the default system groups that unity provides you and everything that we're going to be creating as far as systems and system groups are going to be falling under one of these three phases here so there's of course the initialization system group which is in the initialization phase and this is really basically just going to be things that are taking place before the simulation you know before most of your game logic is going to be taking place so that's kind of what's happening in the initialization system here and if you actually expand it you can kind of see what systems are running inside here it's basically just kind of like some setup things next moving into the update phase you'll see that we have the simulation system group so this is actually where most of your game logic is going to be inside this simulation system and this is where we're you know actually calculating values and doing data transformations and you know really all the stuff that's actually happening in our game it's most likely going to be happening inside of here and then finally there's the pre-light late update phase which has the presentation system group most of the stuff just kind of has to do with um like rendering and all that but every once in a while we may need to you know run some things in this phase that need to you know happen after the simulation system has completed so these groups are very important to keep in mind because these groups are the things that actually trigger the update methods of each system all right so to demonstrate what that means is i basically just created this empty system here only thing it needs is this on update method and so this method is actually being triggered by the group that it is associated with it so if we enter play mode you'll see where these systems basically get added by default so they're actually going inside the update phase inside the simulation system group so we have this test system a you'll see it's just kind of running inside this simulation system group now the order in which these systems run is basically from top to bottom and by default it kind of goes in like an arbitrary place in there if we're not specifying anything but it is deterministic so basically every time we run our game you know the order that our systems are running in it's always going to be exactly the same you know if we add in some new systems maybe they'll kind of get slotted in in different places or if we do specifically change the order on them of course that'll change the order but as long as we don't do any of that the order is going to stay exactly the same every time we run the game now what if we wanted to get this system to run somewhere other than the simulation system group like say inside the initialization system group well it's actually really easy to do we can just add another attribute here and we just say update in group type of we have to make sure that we do a type of rather than just passing in the group then here we can just say the initialization system group and it's really that easy we'll just come back over to unity and when we enter play mode you'll see that inside the initialization system group we now have our test system a running however we can actually get a little bit more complex with this and we can create groups of our own so for example let's go ahead and just come back to this script here and we'll create ourselves a simple little group it's really easy to do of course you just do a public class we'll call this a test group a and just it needs to inherit from the component system group and then you see we can basically just have that empty class right there and then we'll say this component system group you know we can kind of nest that where these groups live and by default this group will show up in the simulation system group but um it's one of those things where it's just like a little bit better sometimes to make sure you're explicitly stating where it's going to go so we can just say that this test group is going to update inside the simulation system group so now we can take the name of this group test group a and we'll set test system a to update inside of test group a so what does that exactly mean well basically we'll see inside the simulation system group you see that we now have another one of these groups called test group a and you'll see under that we have test system a and so this is really handy because if we have a bunch of systems that we kind of want to group together you know we can put them all in a group we can basically arrange the order of those inside the group and then if we want we can kind of basically determine where we want that group to live inside the simulation system player loop so now let's kind of talk a little bit about system order and kind of how that works so let's just go ahead and create another public class here and call this the test system b course is going to be a system base and it's just going to be an empty one and we'll also want this one to update inside the group which is of type of test group a and here we're actually going to add another attribute which is called the update after and so here again we'll just do a type of and want this to update after the test system a so basically what this means is inside of the test group a we're going to have test system a run first and then test group b run afterwards so now you'll see again inside the simulation system group we have our test group a and you'll see that test system a runs before test system b now if we wanted to say do it the other way around instead of an update after we could do an update before so we'll have a test system b run before a then i see inside the test group a test system b is running before test system a now it's important to note that we can basically only order systems relative to systems that are basically at the same level of them inside the same group basically so system a and system b can only be ordered against each other we couldn't say order test system a against like this new world system here for example it basically has to be all at the same kind of level here now if we wanted the test system a to run after the new world system we'd have to go to the test group and then we'd have to say you know okay run this after the new world system so we kind of have to like basically just go a level up so it does take you know a little bit of just kind of you know coming into the entity debugger and just making sure that everything is laid out how you want it to now there's actually one more way that we can kind of determine the order of these systems inside our group and it's actually without using this update before update after stuff actually inside the update in group we can actually do a comma after this and then we can do an order first or order last so if we set order first equal to true these are just boolean variables that means that this system is going to be you know ordered first inside this group of systems now we can basically do the same thing with test group a we can say you know we also want this one to be order first equal to true and then for example we could say create a test system c and we'll set order last equal to true here now come back to unity inside test group a you'll see test system a and test system b those are both you know updated first inside this list and then test system c is going to be ordered last inside the list now again this kind of goes back to the whole thing about you know systems basically picking an arbitrary location at first because we didn't specify you know what should update first a or b because these both have the update first attribute this means that we can order them against each other so for example if we go into test system b we can put update before test system a because these both have the order first attribute that means they can be ordered against each other basically the update first and last attributes will take precedence over the update before and update after attributes so you'll see that inside test group a we have test system b then test system a followed by test system c so with that being said that is an overview about system update order i know it kind of goes a little bit deeper than you might see think off the surface but overall it is pretty simple stuff we're basically just you know specifying these groups and where we want these systems to um update in these groups and which groups they belong to you know it's pretty easy stuff but it's one of those things that again you just kind of need to be aware of pay attention to the entity debugger make sure your systems are actually updating in the orders that you want them to because again you can kind of get into some weird situations like that happened to me in the game jam project that i was working on where some things were updating before things when they should actually be updating after things um you know of course was pretty easy to go in and clean up but it's just one of those things where you know as you go along and you build out your project just really pay attention to where this falls in the hierarchy and if that is you know the proper location for it and of course you know feel free to use those kind of groups to your advantage to further organize out your systems anyways with that being said that is going to wrap up today's video really do hope you enjoyed it and you found it helpful if you did i'd really appreciate it if you hit that like button also feel free to subscribe to the channel for lots more videos about unity's entity component system and the data origin technology stack of course if you have any questions for me or suggesting for future videos feel free to leave those down in the comment section below or join us over on discord over at tmg.dev slash discord hope you have a fantastic rest of your day i'll see you the next one [Music] you
Info
Channel: Turbo Makes Games
Views: 308
Rating: undefined out of 5
Keywords: Unity DOTS, Unity ECS, Unity DOTS tutorial 2021, Unity ECS tutorial 2021, unity data oriented technology stack, unity data oriented tech stack, how to make a game with unity DOTS, unity esc, unity dot, unity data oriented programming, unity data oriented design, turbo makes games, johnny turbo, system update order, ecs update before, ecs update after, ecs update in group
Id: oX0NElpfXgg
Channel Id: undefined
Length: 11min 47sec (707 seconds)
Published: Fri Dec 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.