Where to store HashMap

I need a hashmap to get entity prefab by it’s id (Dictionary<string, Entity>). Where should i store this hashmap?

  • Somehow in IComponentData?
  • I need to use IBufferElementData with some additional stuff?
  • BlobAsset?
  • Maybe i should store it as field/property of some system?
  • Some Singleton? Injectable POCO? Other oldschool stuff?

I wouldn’t recommend having your key be a string, but if that is unavoidable, your best bet is a NativeHashMap<FixedString64, Entity>

Hello,

I too would avoid using a string key, a hash of it maybe or in my case I use the asset GUID converted to uint :

string Guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(PREFAB));
uint keyId = BitConverter.ToUInt32(Encoding.ASCII.GetBytes(Guid), 0);

Since you are talking about prefab, I assume your data will be static, no new prefab should be added at runtime, nor should there be a reason for a prefab entity to be destroyed. Otherwise, you would have to worry about cleaning up your references and adding new ones in your dictionary.

I have similar situations where I need a fast cache of struct data so I made a BlobMultiHashMap that I store as a blob asset reference on a singleton entity.

This allows me to make systems that rely on that cached data wait on its creation by requiring the singleton entity.

Another benefit is that since this cache is built only once and will be read often, I built the BlobMultiHashMap to store the keys in a sorted manner so that when querying a key, I can use a binary search and gain a bit on performance over a NativeHashMap which performs a linear search (as far as I can tell).

The only downside I have for now is that with entities 0.17 there is a regression where you can’t have a generic blob so I have to use a modified local copy of the package (see Known issues in the read me of the package).

Yes I know than i should use FixedString64, or something other than string, but it is not relevant to question.
Yes, data is static.

So you think that it’s better to store static data in BlobAsset? And to store hashmap i need custom class?
Is there any built-in functionality in dots to store dictionary like structures other than NativeHashMap that i can use only on Logic side of ECS.

As far as I know that what BlobAsset are for. That’s what I use them for and it works for me.

Unfortunately there is no Unity BlobMap implementation (maybe in 0.50 ?) there have however been several community made one (more here ).

Since you are only writing from one place (single thread) an after that only reading, I guess there would be no arm in using directly an unsafemultihashmap in a component on a singleton entity but clearly not a fan of it…