#Native Containers in Components and EntityCommandBuffer

1 messages · Page 1 of 1 (latest)

bold atlas
#

I have a component with a NativeHashSet as one of the variables. When I add the component with the ECB I use hs = new NativeHashSet<int>(0, Allocator.Persistent).

Later in the code I want to add things to the hash set and I think I need to dispose it when I destroy the entity.

I cannot figure out how to do that with the AddBuffer, SetComponent and Dispose methods of the ECB.

Can anyone tell me how to do that?

short vigil
river anvil
#

^ remember this alone won't cleanup leaving play mode

#

Or if you leave play mode before ecb plays back

bold atlas
#

I wondered about those errors (and if I should care about them)

short vigil
#

Alternatively just having a world bound allocator

river anvil
bold atlas
#

It is.

river anvil
#

there's a reason all the experienced devs on discord constantly harp on about not putting native containers on entities (that aren't singletons though)

bold atlas
#

Yeah, it is difficult and I have a lot of leaks.

river anvil
#

causes so many unnecessary issues

bold atlas
#

My problem is so common, that I think I make some fundamental thing very complicated

river anvil
#

don't get me wrong, putting containers on entities can be very useful

#

i personally just took the approach of writing custom dynamic containers (based off dynamicbuffer) instead of using native containers

#

so memory is a automatically managed for you

bold atlas
#

I have damage zones and I want to damage every enemie that is inside the trigger collider. I want to cause damage every tick, different for each zone. I also want to damage every enemy only once per tick, per damage zone. So currently I have a NativeHashSet on every DamageZone that keeps track of HashCodes it has damaged during that tick. After tick time is zero, I clear the HashSet and start again.

#

That has to be a popular mechanic in Dots.

short vigil
bold atlas
#

How would I know if I damaged them previously, if I do not keep track?

#

I cannot give them a tag, as they might be in several damage zones at once

#

and as I understand the component system, each entity can only have one of each type

short vigil
#

Just doing a linear or logarithmic lookup is often fast enough

bold atlas
#

I don't really know how to use them

short vigil
#

Dynamic buffers are basically just lists

bold atlas
#

I think so. Might be less than 5 most of the time. I think in the lategame 100 per zone?

river anvil
#

probably just better using a dynamic buffer then

bold atlas
#

I'll look that up. Auto cleanup sounds good for my leak problems

river anvil
#

probably faster than a hashmap at those counts

short vigil
#

^ That too

bold atlas
#

Thanks for the idea.

short vigil
#

HashMaps are not super efficient for low amount of entries

bold atlas
#

Is there a good tutorial for dynamic buffers that you can recommend?

river anvil
#

just the manual