How To Use DYNAMIC BUFFERS? || Unity DOTS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you may notice that you can't add lists and even native lists into components in unity ECS Dynamic buffers work almost the same way as lists you specify the type that the buffer will be holding and then you can add it to entities the same way as you would do it with components let's say that our player has a inventory and we need to store a list of all of the items that he currently has so I will create new script that will be the inventory system the inventory system is inheriting from I system in the void on Create and on update we are just getting the entity manager and next we'll create the player inventory entity so I have defined a player entity variable D type is entity and in the oncreate function using the entity manager I'm creating entity then I also setting its name so that we can easily find it and you can set names of entities only in unity editor so that's why I have the unity editor if here now we'll need to specify the type that the dynamic buffer will be holding so I will create new script for that I called the script inventory buffer element where we will not Define the dynamic buffer yet we'll just Define the element that the dynamic buffer will be holding we can delete all of that stuff and we'll need to add using unity. entities and because we are working in the data oriented technology stack this will be a struct and it will be inheriting from I buffer element data so that's how we Define a buffer element of the dynamic buffer it has to be a struct it has to be inheriting from ibuffer element data and we can add any managed variables into it we want so each of the items in our inventory will have some item id item count and you can add anything else you want on the buffer element we can also specify the internal buffer capacity the buffer capacity is not really limiting the sign of the buffer it is just telling it that when we have less than 20 elements in the dynamic buffer all of the memory will be allocated with other entities in the chunk and when the size of the buffer is greater than 20 the memory will be stored in the Heap Which is less optimized but you really don't have to worry about the internal buffer capacity you don't even have to specify it but if you are specifying it you should set it to the most likely size of the buffer so let's say that the maximum size of the inventory will be five in this case I will set it just to five and now back in the inventory system using the entity manager we can just add the buffer to the player entity so we are using entity manager. add buffer and you can see that we are not specifying Dynamic buffer of the type inventory buffer element we are just specifying the type that the dynamic buffer will be holding and we are adding it to the player entity you can see that this function returns as the dynamic buffer of type inventory buffer element so you could also save it into a variable if you want now I will go to the onupdate function where I will be just checking if the player is pressing the leftmost button if this is true we can add some elements to the buffer first I am getting a reference for the dynamic buffer of type inventory buffer element so that we can later add new elements to it I'm also getting Singleton of the input component which we have been working on in one of the previous tutorials so feel free to check it out and the input component is just holding a Boolean if we are pressing the left Mouse button so if we are pressing left Mouse button and the item was not added yet then we can just add it to the buffer and because we already have a reference for the inventory buffer we can do a bunch of stuff with it for example we can add new elements to it which you can see it is taking in the inventory buffer element or we can also clear the inventory buffer we can get element at some index we can insert element to some index we can get the length we can remove some elements and so on but right now we'll be just adding new element so like this we can add elements to a buffer it works just the same way as list first we need to add the dynamic buffer to The Entity then we can access a reference for it and we can add new elements into the buffer I just added a bit more logic into the onupdate function so when we add the element to the buffer it means that we have added the item and when we are not pressing the leftmost button we can just reset the Boolean we can go to the entity's hierarchy and search for the player entity and when we select it yep we can see that it is holding a dynamic buffer of type inventory buffer element and because I've already clicked once it is containing some of the elements and as I'm clicking you can see that it is adding more and more of them so each of the elements in the inventory has its own Item ID and item count and we can add as many of these as we want next next we could generate some random item ID for each of the inventory elements to set random IDs of the items I added using unity. mathematics so that we can use the random then on create I'm initializing the random and as you are adding the inventory buffer element to the dynamic buffer we can specify the item ID so I'm getting that from the random and I have also set the item counts to one for now yep that was very simple so now we can see that as I'm adding the item each of them has the item count on one and the item ID is always different next when we will be adding new item we'll Loop through all of the inventory buffer elements to check if we have already added this element in the list if this is true we can then just increase the item count I just had to change some stuff in the onupdate function so first I'm generating the item id the same way just generating some random number then I'm looping through all of the elements in the inventory buffer so we can just just access that by inventory buffer. length which is size of the dynamic buffer and then I'm checking if the item ID that we have generated is equal to the item ID in the inventory buffer on the index of I if this is true it means that we can just add the item count of the element but we can't just change values of the inventory buffer so for example if I would do inventory buffer on the index of I I just can get the item count and item id but I can't change it so if I would do it like this it doesn't work so what we have to do instead is to cage the current element so we are getting that from the inventory buffer on the index of I and I'm saving that into inventory buffer element which I called new element then I'm just changing the item count of the new element and I'm assigning the new element back to the inventory buffer on the index of I this means that we have added the item we can break from the loop and if we haven't edit the item yet which means that the item ID is not yet in the inventory we can just edit so we are just looping through all of the elements of the inventory buffer if the item ID is the same we can just increase the item count so as I click for the first time it will add new element and now you can see it as I clicked for the second time we already had the element with the item ID on four in the list so it just increase the item count and as I am clicking further you can see that it is increasing the item counts if we already have the item id there so like this you could create a simple inventory system but this is not the only way that you can create Dynamic buffers you can also create them using the entity command buffer what the entity command buffer allows us to do is to queue up commands so that we can then execute them at the same time to use the entity command buffer we need to add using unity. collections and then we can just create the entity command buffer like that and using the entity command buffer we can add some buffer to a entity or we can append to buffer which means that it is just going to add some elements to it or we can also set buffer which I use just to clear the buffer The Entity command buffer can be especially useful for setting Dynamic buffers in jobs because in jobs you can't use classic entity manager and there are definitely a lot more use cases for dynamic buffers for example for this simple AI each of the AI has a dynamic buffer that is storing all of the waypoints as for where it should go next so this AI has five way points with some coordinates this AI has different way points and so on you can see the dynamic buffers are really useful because creating a list of way points on each of the enemies or creating list of the inventory items would be a lot harder without them and don't forget to check this video on creating AIS using no mesh cury I hope this video was useful if you have any questions or suggestions drop them down to the comments don't forget to like subscribe and I will see you in next videos bye thanks for watching this video till the end if you are looking for a Unity C or bold tutor then I am here for you so feel free to send me a message to my Gmail and take a look at my website for more info I can help you with your personal projects or teach you anything about game development you would want to know you are welcome
Info
Channel: Freedom Coding
Views: 572
Rating: undefined out of 5
Keywords: Unity, Unity2D, UnityEngine, GameDevelopement, IndieGameDev, PCGames, Coding, C#, Easy, Tutorial, Education, Unity3D, Programming, Bolt, UnityTurorials
Id: iV7Anil0YOE
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Mon Jan 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.