Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Most straightforward way to create a lookup table that can be accessed in Burst?

Discussion in 'Entity Component System' started by strblueshell, Dec 22, 2022.

  1. strblueshell

    strblueshell

    Joined:
    Apr 29, 2022
    Posts:
    9
    Suppose I want to create a lookup table game mapping keys of some blittable type to values of some blittable type. I'd guess this is a pretty common use case.

    Prior to Entities 1.0 I would have just used a NativeHashMap, stored it on some managed (singleton) component, and then retrieved the NativeHashMap prior to executing an Entities.ForEach call. From my understanding the Entities.ForEach call would still be burst-compiled and it could read from the NativeHashMap.

    However, since Entities.ForEach seems to not be recommended anymore, I am looking for an equivalent way to do this using idiomatic ForEach and/or ISystem. It seems to me that ISystem requires the entire OnUpdate call to be burst-compiled or not, which means I can't access any managed components. And from my understanding, idiomatic ForEach isn't bursted unless run within a bursted ISystem.

    Does anyone know of a solution? I'd like to keep using NativeHashMaps, if possible, just for the dictionary-like syntax. I imagine there is a way to do this with blob assets but there currently is no official BlobHashMap anyway.
     
  2. Anthiese

    Anthiese

    Joined:
    Oct 13, 2013
    Posts:
    72
    You can still use unmanaged singletons to store collections if you want. Same usage pattern as using a managed singleton, just use a struct instead of a class. I’ve been doing this in 1.0.0-pre.15 with no problems using NativeParallelHashMap / NativeMultiHashMap.
     
  3. strblueshell

    strblueshell

    Joined:
    Apr 29, 2022
    Posts:
    9
    Oh, oops. Somehow I had gotten the impression that native collections couldn't be in unmanaged components. I guess that is pretty straightforward, then.