Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ query.Foreach( (Entity entity, ref int value ) => // you can access the
Console.WriteLine($"{entity} value is {value}");
});

foreach ((Entity entity, int value) in query.Foreach<int>()) // foreach makes so you can use continue and break
{
if (value > 0)
{
continue;
}

if (value < 0)
{
break;
}

Console.WriteLine($"{entity} value is {value}");
}

// having more then 5 components makes so you have to put double parenthesis.
// the reason is that a tuple only can hold in 8 values, so you create a second tuple in the tuple
foreach ((Entity entity, A aa, B bb, C cc, D dd, E ee, (F ff, G gg, H hh, I ii, J jj)) in query.Foreach<A, B, C, D, E, F, G, H, I, J>())
{
Console.WriteLine($"value x is {aa.x} value y is {aa.y}");
}

var all_entities = world.CreateQuery(); // a simple way to match against all entities is to make a query with no filters

query.GetEntities(); // returns a copy of all entities currently matching the query
Expand Down
289 changes: 289 additions & 0 deletions SimpleECS/QueryForeachFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3278,5 +3278,294 @@ public void Foreach<W1, W2, W3, W4, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1>> Foreach<C1>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2>> Foreach<C1, C2>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3>> Foreach<C1, C2, C3>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;

if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4>> Foreach<C1, C2, C3, C4>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5>> Foreach<C1, C2, C3, C4, C5>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, C6>> Foreach<C1, C2, C3, C4, C5, C6>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], c6[e]);
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7>>> Foreach<C1, C2, C3, C4, C5, C6, C7>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7, C8>>> Foreach<C1, C2, C3, C4, C5, C6, C7, C8>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7, C8, C9>>> Foreach<C1, C2, C3, C4, C5, C6, C7, C8, C9>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7, C8, C9, C10>>> Foreach<C1, C2, C3, C4, C5, C6, C7, C8, C9, C10>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7, C8, C9, C10, C11>>> Foreach<C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10) && archetype.TryGetArray(out C11[] c11))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e], c11[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
/// <summary>
/// iterates over the entities that match the query and returns a tuple of the entity and the components
/// possible to use continue and break
/// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc.
/// </summary>
public IEnumerable<Tuple<Entity, C1, C2, C3, C4, C5, Tuple<C6, C7, C8, C9, C10, C11, C12>>> Foreach<C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12>()
{
if (Update(out var world_info))
{
world_info.StructureEvents.EnqueueEvents++;
for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index)
{
var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data;
int count = archetype.entity_count;
var entities = archetype.entities;
if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10) && archetype.TryGetArray(out C11[] c11) && archetype.TryGetArray(out C12[] c12))
{
for (int e = 0; e < count; ++e)
yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e], c11[e], c12[e]));
}
}
world_info.StructureEvents.EnqueueEvents--;
}
}
}
}