- Home /
How to use multiple DynamicBuffer in one entity
I need to use multiple DynamicBuffers for my entity but im getting errors because the second one redefines the type of the first
Heres the error:
InvalidOperationException: Attempted to access ArchetypeChunkBufferType<Block> which has been invalidated by a structural change.
And the code:
private void Start(){
heightMap = new float[xSize,zSize];
heightMap2 = new float[xSize,zSize];
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype entityArchetype = entityManager.CreateArchetype(
typeof(Translation),
typeof(Chunk),
typeof(Heightmap),
typeof(RenderMesh),
typeof(LocalToWorld),
typeof(RenderBounds)
);
Entity entity = entityManager.CreateEntity(entityArchetype);
DynamicBuffer<Block> dynamicBuffer = entityManager.AddBuffer<Block>(entity);
DynamicBuffer<VertexBuffer> dynamicVertexBuffer = entityManager.AddBuffer<VertexBuffer>(entity);
for (int x = 0; x < xSize; x++)
for (int y = 0; y < ySize; y++)
for (int z = 0; z < zSize; z++)
{
float simplex1 = noise.GetSimplex(x * .8f, z * .8f) * 10;
float simplex2 = noise.GetSimplex(x * 3f, z * 3f) * 10 * (noise.GetSimplex(x * .3f, z * .3f) + .5f);
float heightMap = simplex1 + simplex2;
float dirtHeight = ySize * .5f + heightMap;
dynamicBuffer.Add(new Block
{
material = y > dirtHeight ? BlockMaterials.air : BlockMaterials.dirt
});
}
for (int x = 0; x <= xSize; x++)
for (int y = 0; y <= ySize; y++)
for (int z = 0; z <= zSize; z++)
{
dynamicVertexBuffer.Add(new VertexBuffer { vertex = new Vector3(x, y, z) });
}
entityManager.SetComponentData(entity, new ChunkComponent
{
xSize = xSize,
ySize = ySize,
zSize = zSize
});
//entityManager.SetComponentData(entity, new NativeArray<Vector3>.CopyFrom(vertex));
}
Comment
Answer by andrew-lukasik · Jul 24, 2021 at 08:43 PM
Use of EntityCommandBuffer
fixes this issue.
MyScript.cs
using UnityEngine;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Entities;
using Unity.Collections;
using Unity.Rendering;
public class MyScript : MonoBehaviour
{
[SerializeField] int _width = 10, _height = 10, _depth = 10;
[SerializeField] Mesh _mesh = null;
[SerializeField] Material _material = null;
void Start ()
{
var ecb = new EntityCommandBuffer( Allocator.Temp );
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype archetype = entityManager.CreateArchetype(
typeof(Translation)
, typeof(Chunk)
, typeof(ChunkComponent)
, typeof(Heightmap)
, typeof(RenderMesh)
, typeof(LocalToWorld)
, typeof(RenderBounds)
, typeof(VertexBuffer)
, typeof(Block)
);
Entity entity = entityManager.CreateEntity( archetype );
entityManager.SetSharedComponentData( entity , new RenderMesh{
mesh = _mesh ,
material = _material ,
} );
for( int x=0 ; x<_width ; x++ )
for( int y=0 ; y<_height ; y++ )
for( int z=0 ; z<_depth ; z++ )
{
float simplex1 = noise.snoise(new float2(x*.8f,z*.8f)) * 10;
float simplex2 = noise.snoise(new float2(x*3f,z*3f)) * 10 * (noise.snoise(new float2(x*.3f,z*.3f)) + .5f);
float height = simplex1 + simplex2;
float dirtHeight = _height * .5f + height;
ecb.AppendToBuffer( entity , new Block{
material = y>dirtHeight ? (byte) 0 : (byte) 1
} );
}
for( int x=0 ; x<=_width ; x++ )
for( int y=0 ; y<=_height ; y++ )
for( int z=0 ; z<=_depth ; z++ )
{
ecb.AppendToBuffer( entity , new VertexBuffer{
vertex = new Vector3(x,y,z)
} );
}
ecb.SetComponent( entity , new ChunkComponent{
xSize = _width,
ySize = _height,
zSize = _depth
} );
ecb.Playback( entityManager );
}
struct Chunk : IComponentData
{
}
struct ChunkComponent : IComponentData
{
public int xSize, ySize, zSize;
}
struct Heightmap : IComponentData
{
}
struct VertexBuffer : IBufferElementData
{
public Vector3 vertex;
}
struct Block : IBufferElementData
{
public byte material;
}
}
Your answer
Follow this Question
Related Questions
when using Dynamic Buffers(ECS), How can you add two variables 2 Answers
Creating a dynamic mesh with audio = crash after 30secs ? 0 Answers
Hierarchy in Game? 0 Answers
AI on dynamic navmeshes and moving platforms? (Drawbridges, Elevators, Monorail trains, etc) 1 Answer
Find a variable using a string 1 Answer