Made a ECS reference script using DynamicBuffers (has more then 1 value type). I need to innitialize the data for the “CreateDropPile” to function.
I’ve tried to run the “OnStartRunning” inside the function referencing the “World” SystemState, creating a static instance of the reference data, and some other stuff.
Any ideas?
Code:
DropPileSystem
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Random = Unity.Mathematics.Random;
public partial struct DropPileSystem : ISystem
{
public EntityManager entityManager;
public EntityCommandBuffer entityCommandBuffer;
public DynamicBuffer<DropPileDynamicBuffer> dropPileDynamicBuffer;
public Entity dropPileReference;
public Entity holdablePileReference;
public DynamicBuffer<HoldableItemDynamicBuffer> holdableItemDynamicBuffer;
public void OnStartRunning(ref SystemState state)
{
entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
entityCommandBuffer = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
dropPileReference = SystemAPI.GetSingletonEntity<DropPileReferences>();
holdablePileReference = SystemAPI.GetSingletonEntity<HoldableItemReferences>();
holdableItemDynamicBuffer = SystemAPI.GetBuffer<HoldableItemDynamicBuffer>(holdablePileReference);
dropPileDynamicBuffer = SystemAPI.GetBuffer<DropPileDynamicBuffer>(dropPileReference);
UnityEngine.Debug.Log(dropPileDynamicBuffer.Length);
}
public void CreateDropPile(Entity holdableItemType)
{
UnityEngine.Debug.Log(dropPileDynamicBuffer.Length);
DropPileDynamicBuffer dropPileTemplate = GetDropPileTemplate(holdableItemType);
UnityEngine.Debug.Log(dropPileTemplate);
Entity newDropPile = entityManager.Instantiate(dropPileTemplate.dropPile);
entityCommandBuffer.SetComponent(newDropPile, new DropPile
{
dropPileItemEntity = holdableItemType,
dropPileItemQuantity = 1,
});
}
HoldableItemReference
using UnityEngine;
using Unity.Entities;
using Unity.Mathematics;
using System.Collections.Generic;
using static HoldableItemCategorySO;
using Random = Unity.Mathematics.Random;
public class HoldableItemReferencesAuthoring : MonoBehaviour
{
[SerializeField] private List<HoldableItemSO> holdableItemSOList;
public class Baker : Baker<HoldableItemReferencesAuthoring>
{
public override void Bake(HoldableItemReferencesAuthoring authoring)
{
Entity entity = GetEntity(TransformUsageFlags.Dynamic);
DynamicBuffer<HoldableItemDynamicBuffer> holdableItemDataBuffer = AddBuffer<HoldableItemDynamicBuffer>(entity);
for (int i = 0; i < authoring.holdableItemSOList.Count; i++)
{
holdableItemDataBuffer.Add(new HoldableItemDynamicBuffer
{
itemEntity = GetEntity(authoring.holdableItemSOList[i].gameWorldPrefab, TransformUsageFlags.Dynamic),
itemID = i,
inventoryQuantityMax = authoring.holdableItemSOList[i].holdingAmountMax,
inventorySpacePointsCost = authoring.holdableItemSOList[i].inventorySpacePointsCost,
stackCategory = authoring.holdableItemSOList[i].stackCategory,
itemCategory = authoring.holdableItemSOList[i].itemCategory,
handheldOffset = authoring.holdableItemSOList[i].handheldOffset,
});
}
AddComponent(entity, new HoldableItemReferences());
}
}
}
public struct HoldableItemReferences : IComponentData
{
}
public struct HoldableItemDynamicBuffer: IBufferElementData
{
public Entity itemEntity;
public int itemID;
public int inventoryQuantityMax;
public float inventorySpacePointsCost;
public StackCategory stackCategory;
public ItemCategory itemCategory;
public float3 handheldOffset;
public Random randomSeed;
}
Input Command System:
if (inputData.ValueRO.isInteracting)
{
DropPileSystem dropPileSystem = state.WorldUnmanaged.GetUnsafeSystemRef<DropPileSystem>(state.SystemHandle);
dropPileSystem.CreateDropPile(holdableItemDynamicBuffer[1].itemEntity);
}```