#Struct containing array of another struct, which in turn also contains an array of another struct

1 messages · Page 1 of 1 (latest)

tall plank
#

Is it possible to Bake a struct that contains a NativeArray of a different struct onto an Entity if that struct also contains a NativeArray of a 3rd struct?

The editor does not give great errors about Baking failing, but I see my Entity is not being created so I assume the baking process is failing.

I am getting this error message, so I am assuming it has to do with my struct containing arrays.

ArgumentException: Blittable component type 'ScraperData' on GameObject 'Scraper' contains a (potentially nested) pointer field. Serializing bare pointers will likely lead to runtime errors. Remove this field and consider serializing the data it points to another way such as by using a BlobAssetReference or a [Serializable] ISharedComponent. If for whatever reason the pointer field should in fact be serialized, add the [ChunkSerializable] attribute to your type to bypass this error.

#

This is my struct "hierarchy"

{
    public NativeArray<MapChunk> chunks;
}

public struct MapChunk
{
    public FixedString32Bytes name;
    public int2 origin;
    public NativeArray<MapTile> tiles;
}

public struct MapTile
{
    public int id;
    public int3 position;
    public NativeArray<Static> statics;
}

public struct Static
{
    public int id;
    public int3 position;
    public int hue;
}```
#

And this is my Baker

{
    [field:SerializeField] public Vector2 Origin { get; private set; }
    [field:SerializeField] public int ChunkSize { get; private set; }

    [field: SerializeField] public Vector2 MapSize { get; private set; }
    [field:SerializeField] public int[] DirtTiles { get; private set; }

    [field: SerializeField] public int[] WaterTiles { get; private set; }

}

public struct ScraperData : IComponentData
{
    public float2 origin;
    public int chunkSize;
    public float2 mapSize;

    public NativeArray<int> dirtTiles;
    public NativeArray<int> waterTiles;
    public NativeHashSet<int> mapTextureIDs;

    public Map map;
}


public class ScraperDataBaker : Baker<ScraperDataAuthoring>
{
    public override void Bake(ScraperDataAuthoring authoring)
    {
        var e = GetEntity(authoring, TransformUsageFlags.None);
        AddComponent(e, new ScraperData
        {
            origin = authoring.Origin,
            chunkSize = authoring.ChunkSize,
            mapSize = authoring.MapSize,
            dirtTiles = new NativeArray<int>(authoring.DirtTiles, Allocator.Persistent),
            waterTiles = new NativeArray<int>(authoring.WaterTiles, Allocator.Persistent),
            mapTextureIDs = new NativeHashSet<int>(0, Allocator.Persistent)
        });
    }
}```
tall plank
#

I assume this falls under the "a NativeArray cannot itself contain a NativeArray" rule

#

this is a huge paradigm shift I have to train myself out of

#

It's not still the case that Entities cannot contain NativeArray at all right? I see videos from back in Entities 0.4 where you must use DynamicBuffer to store a collection of data on an Entity

gusty fog
#

you can't bake a nativearray at all

#

don't even worry about baking a native array of native containers, you're skipping ahead of the what you can't do

#

you're creating a pointer that only exists in that editor point of time

#

you make a build and you just have a pointer to a random piece of memory

#

likely about to crash your app for being out of range of the apps memory space

#

tldr: native containers not allowed in baking

tall plank
#

alrighty, thought maybe that changed in future versions of Entities

#

so does that mean no entity component can ever contain a Native container?

#

or I guess just during baking

#

because as you said it was an editor->playmode thing

gusty fog
#

you can never access an entity component with a native container in a job

#

but you can query in a system

#

and then pass it to a job

#

so native containers on components is useful for singletons but that's about it

#

(but thtat is very useful)

proven rock
#

Another way around might be flattening your array of arrays into a single 1D array. Supposedly you know their length beforehand. Then use blob array to bake them into subscene.

#

I did this for my spritesheet library.

#

At runtime, I carry around a range(start_index, len) structure to access the correct sprite frames.

gusty fog
#

you can have nested blob arrays can't you? don't really need to flatten if using blobs

tall plank
#

i believe you can yes, but blobs are read only data during time of creation, correct? Honestly I have never once tried to use my own blob assets yet.

gusty fog
#

they are designed to be readonly

#

but you can in theory change them

tall plank
#

thing is I need to populate these arrays not at editor time, but at runtime

#

i just wanted the arrays to "be there" at editor time so I could use them as a place to store the data

#

but I am used to monobehaviour workflow