Skip to main content
Changed return type to the expected type
Source Link
Applekini
  • 8.5k
  • 8
  • 37
  • 64

I'm a little rusty with C# but something like this might work:

public List<Entity>List<FilterType> GetEntities<FilterType>()
{
    // Assume all entities are valid
    List<Entity> entities = new List<Entity>(allEntities);
    var fields = FilterType.GetFields();
    foreach(var field in fields)
    {
        var componentType = field.FieldType;
        foreach(var entity in entities)
        {
            // Remove any invalid entities
            if(!entity.HasComponent(componentType))
                entities.Remove(entity);
        }
    }
    
    List<FilterType> filters = new List<FilterType>();
    foreach(var entity in entities)
    {
        FilterType filter = (FilterType) Activator.CreateInstance(FilterType);
        foreach(var field in fields)
            field.SetValue(filter, entity.GetComponent(field.FieldType));
        filters.Add(obj);
    }

    return entities;filters;
}

This uses reflection to get the type of all (public!) fields in the struct/class FilterType and then checks if an entity does not have that component. In that case that entity is invalid and is removed from the final list result.

I'm a little rusty with C# but something like this might work:

public List<Entity> GetEntities<FilterType>()
{
    // Assume all entities are valid
    List<Entity> entities = new List<Entity>(allEntities);
    var fields = FilterType.GetFields();
    foreach(var field in fields)
    {
        var componentType = field.FieldType;
        foreach(var entity in entities)
        {
            // Remove any invalid entities
            if(!entity.HasComponent(componentType))
                entities.Remove(entity);
        }
    }
    return entities;
}

This uses reflection to get the type of all (public!) fields in the struct/class FilterType and then checks if an entity does not have that component. In that case that entity is invalid and is removed from the final list result.

I'm a little rusty with C# but something like this might work:

public List<FilterType> GetEntities<FilterType>()
{
    // Assume all entities are valid
    List<Entity> entities = new List<Entity>(allEntities);
    var fields = FilterType.GetFields();
    foreach(var field in fields)
    {
        var componentType = field.FieldType;
        foreach(var entity in entities)
        {
            // Remove any invalid entities
            if(!entity.HasComponent(componentType))
                entities.Remove(entity);
        }
    }
    
    List<FilterType> filters = new List<FilterType>();
    foreach(var entity in entities)
    {
        FilterType filter = (FilterType) Activator.CreateInstance(FilterType);
        foreach(var field in fields)
            field.SetValue(filter, entity.GetComponent(field.FieldType));
        filters.Add(obj);
    }

    return filters;
}

This uses reflection to get the type of all (public!) fields in the struct/class FilterType and then checks if an entity does not have that component. In that case that entity is invalid and is removed from the final list result.

Source Link
Applekini
  • 8.5k
  • 8
  • 37
  • 64

I'm a little rusty with C# but something like this might work:

public List<Entity> GetEntities<FilterType>()
{
    // Assume all entities are valid
    List<Entity> entities = new List<Entity>(allEntities);
    var fields = FilterType.GetFields();
    foreach(var field in fields)
    {
        var componentType = field.FieldType;
        foreach(var entity in entities)
        {
            // Remove any invalid entities
            if(!entity.HasComponent(componentType))
                entities.Remove(entity);
        }
    }
    return entities;
}

This uses reflection to get the type of all (public!) fields in the struct/class FilterType and then checks if an entity does not have that component. In that case that entity is invalid and is removed from the final list result.