Skip to main content
added 1 character in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually createcreated a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> values, Entity entity) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in values) {
        buffer.Add(entry);
    }
}

This is the resource production system that increases it:

public partial class ResourceProductionSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually create a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> values, Entity entity) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in values) {
        buffer.Add(entry);
    }
}

This is the resource production system that increases it:

public partial class ResourceProductionSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually created a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> values, Entity entity) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in values) {
        buffer.Add(entry);
    }
}

This is the resource production system that increases it:

public partial class ResourceProductionSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}
added 15 characters in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually create a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> valuevalues, Entity entity) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in valuevalues) {
        buffer.Add(entry);
    }
}

This is the resource processingproduction system that increases it:

public partial class ResourceProcessingSystemResourceProductionSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually create a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> value) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in value) {
        buffer.Add(entry);
    }
}

This is the resource processing system that increases it:

public partial class ResourceProcessingSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually create a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> values, Entity entity) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in values) {
        buffer.Add(entry);
    }
}

This is the resource production system that increases it:

public partial class ResourceProductionSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

If you want a component that doesn't have a 1:1 relationship with entities but a n:1 relationship, as in "an entity can have multiple instances of that component", then you need a Dynamic Buffer Component.

A dynamic buffer component is basically a component that is not a single structure but an array of structures.

I actually create a ECS prototype of a game that seems very similar to yours. That was about a year ago, so considering the speed with which DOTS evolves it probably no longer follows the idiomatic style of the week. But this is how I solved this back then.

This is my ResourceStorage structure. Each entity with an inventory has one per resource.

public enum ResourceType : short {
    METAL = 0,
    FOOD = 1
    // Remember to update the InternalBufferCapacity of the ResourceStorage buffer component below when adding more resources
}

[InternalBufferCapacity(2)] 
public struct ResourceStorage : IBufferElementData {
    public ResourceType type;
    public float max;
    public float current;
}

This is the code that adds an inventory to an entity:

public void SetResourceStorageData(IEnumerable<ResourceStorage> value) {
    EntityManager manager = World.DefaultGameObjectInjectionWorld.EntityManager;
    DynamicBuffer<ResourceStorage> buffer;
    buffer = manager.AddBuffer<ResourceStorage>(entity);

    foreach (ResourceStorage entry in value) {
        buffer.Add(entry);
    }
}

This is the resource processing system that increases it:

public partial class ResourceProcessingSystem : SystemBase
{
    protected override void OnUpdate()
    {

        float deltaTime = SystemAPI.Time.DeltaTime;
        
        Entities.ForEach((DynamicBuffer<model.ResourceStorage> storages) => {
            for (var i = 0; i < storages.Length; i++) {
                var storage = storages[i];
                storage.current += deltaTime;
                storage.current = math.clamp(storage.current, 0.0f, storage.max);
                storages[i] = storage;                
            }
        }).ScheduleParallel();
    }
}