Search Unity

Best practice for immutable data?

Discussion in 'Entity Component System' started by huhund, May 21, 2019.

  1. huhund

    huhund

    Joined:
    Aug 14, 2014
    Posts:
    8
    I've now gotten a grasp of the basics with Unity ECS. I have proto with some characters moving around in a city. My next step is to pass waypoint data to these entities. I have currently predefined waypoints as empty game objects in the scene. Let's assume there will be around 1000 waypoints and that they will never change after scene load.

    Question1: What would be the preferred way of allowing my character systems to access this data? I have been told that simple static data should be avoided, correct?

    Question2: Should I convert the game object waypoints to some better format before allowing systems to use them? E.g. a native array or are game objects fine? After all, I would need to access the position of the waypoint.

    Best, Hu

    (sorry if this has already been discussed in some thread, I did try to search for similar topics but didn't find anything matching).
     
  2. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    You can use the new BlobBuilder/BlobAssetReference API to embed shared immutable data inside your IComponentData.
     
    huhund likes this.
  3. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    How does it work?
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    It use shared immutable allocated data (stored in big preallocated place, total 256mb if I remember sources, but I’m not sure about this, should look at sources) and storing pointer on CD (wrapped to BlobAssetReference). In this case you not split data by multiple chunks (like SCD)
     
    huhund and Orimay like this.
  5. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    Documentation is basically non-existent. Look at Library\PackageCache\com.unity.entities@0.0.12-preview.32\Unity.Entities.Tests\BlobificationTests.cs for examples. The Unity Physics examples also show how to use it.
     
    huhund and Orimay like this.
  6. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    Very cool, thanks!
     
  7. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    106
    Oh important warning: make sure and use 'ref var' on everything when you are using these blobs. If you copy a struct into a local variable and access a BlobArray/Ptr on the copied struct you will be reading potentially garbage memory.
     
    huhund and Orimay like this.
  8. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    What benefits of using BlobArray? Better memory allocating/accessing? Where can we read about blobs? (in general in google, i know)
     
  9. huhund

    huhund

    Joined:
    Aug 14, 2014
    Posts:
    8
    "Converting scene data to DOTS" talk from this weeks Unite has an example that exactly answers my question. Unity listens :).
     
    siggigg likes this.