I am attempting to test my inventory system, for that I need to create a Entity with ItemContainer and fill said container with Entiteis that represent the Items, currently I am attempting to use DynamicBuffers
I sort of know what I am doing wrong, but I have no idea how is the “correct way”.
private void Start() {
if (World.Active != null) {
DatabaseLoader.loadDatabase();
EntityManager em = World.Active.GetOrCreateManager<EntityManager>();
Entity cont = em.CreateEntity(new ComponentType[] { ComponentType.Create<ItemContainer>() });
em.AddBuffer<ItemContainerContent>(cont);
DynamicBuffer<ItemContainerContent> buffer = em.GetBuffer<ItemContainerContent>(cont);
foreach (KeyValuePair<long, ItemBase> item in ItemProcessor.GetItems()) {
ItemBaseReference ibr = new ItemBaseReference();
ibr.ItemReference = item.Key;
Entity it = em.CreateEntity(new ComponentType[0]);
em.AddComponentData(it, ibr);
buffer.Add(it);
}
container = cont;
hasContainer = true;
UpdateInventory();
}
}
With this code, I get the “The NativeArray has been deallocated, it is not allowed to access it” error, because I used the EntityManager to create a Entity or something.
But then comes the question, when and how will I ever be able to fill the DynamicBuffer? I can’t create the buffer and fill it before the Entity, is it impossible to change a DynamicBuffer from outside a System?
Do I have to create a system, whose sole purpose is to, one time only, fill a buffer with debug values?
And I am not even entirely sure how I would go about it