I'm new to ECS and am having issues finding the right data structure to use.
For context, I have a galaxy with solar systems that produce resources at a given rate (ResourceProducer
component). Resources are tallied in a stockpile (ResourceStockpile
) and the individual types are defined in a Resources
enum.
public struct ResourceProducer : IComponentData {
public Resources resourceType;
public float quantity;
public float interval;
}
(ResourceStockpile
is the same but no interval
)
I had originally intended to have multiple producers and stockpiles per location, one per resource type, but Unity objects to having multiple of the same component on a single entity (and I've seen some Stack Overflow posts supporting this philosophy).
So I then created a
public struct ResourceQuantity {
public Resources resourceType;
public float quantity;
}
and changed my stockpile to use
public ResourceQuantity[] resources;
(Planning to worry about production rate later as I can't represent it here. Also, I'd prefer a list, but an array seemed more appropriate given the error below)
But now I get an issue in my baker....
public class ResourceStockpileBaker : Baker<ResourceStockpileAuthoring> {
public override void Bake(ResourceStockpileAuthoring authoring) {
Entity entity = this.GetEntity(new TransformUsageFlags());
AddComponent(entity, new ResourceStockpile {
resources = authoring.resources
}) ;
}
}
AddComponent
fails to compile with
The type 'ResourceStockpile' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'IBaker.AddComponent(Entity, in T)'
It feels like I may be dealing with an XY problem... I'm going down a rabbit hole without knowing it's the right one.
What's the correct way to represent an entity that produces multiple resources at different rates, to be accumulated in a stockpile for later usage?