You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Background
I'm using Arch (1.2.8 via NuGet) as the backbone of my game. Aside from typical components like Position, Velocity or Physics there is also a Player component. When the PlayerSystem detects a certain state for a player, it triggers an animation by adding an animation component. When the animation has finished, the component is removed. This worked perfectly with one player.
Recently, I added support for multiple entities with player components and noticed a strange behavior within inline queries. There are no parallel queries, threads or tasks used.
Issue
Whenever I use entity.Add(...) or entity.AddOrGet(...) within an inline query update method, the referenced components are getting randomly changed.
In the example below a and b should be equal, but b always is the index of another player. If I comment out the .Add(...) call, everything works as expected.
Example (simplified)
private readonly QueryDescription _query = new QueryDescription().WithAll<Player>();
public override void Update(in GameTime gameTime)
{
var playerUpdate = new PlayerUpdate(this);
World.InlineEntityQuery<PlayerUpdate, Player>(in _query, ref playerUpdate);
}
private readonly struct PlayerUpdate(PlayerSystem system)
: IForEachWithEntity<Player>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(Entity entity, ref Player player)
{
var a = player.Index; // returns current index
entity.Add(new ScaleAnimation { ... });
// the following method gives the same result:
// ref var animation = ref entity.AddOrGet<ScaleAnimation>();
var b = player.Index; // returns index of other component
}
}
The text was updated successfully, but these errors were encountered:
Thanks for your post! However, the behavior you describe is "intentional". The structural modification of an entity copies it into a new archetype. Its slot is taken by another entity. Since you use ref to access its components in exactly this slot, the values also change because they are overwritten.
This is the disadvantage of the inline query. However, the source generator or custom enumeration or the standard queries can be used to override this behavior.
Background
I'm using Arch (1.2.8 via NuGet) as the backbone of my game. Aside from typical components like Position, Velocity or Physics there is also a Player component. When the PlayerSystem detects a certain state for a player, it triggers an animation by adding an animation component. When the animation has finished, the component is removed. This worked perfectly with one player.
Recently, I added support for multiple entities with player components and noticed a strange behavior within inline queries. There are no parallel queries, threads or tasks used.
Issue
Whenever I use
entity.Add(...)
orentity.AddOrGet(...)
within an inline query update method, the referenced components are getting randomly changed.In the example below
a
andb
should be equal, butb
always is the index of another player. If I comment out the.Add(...)
call, everything works as expected.Example (simplified)
The text was updated successfully, but these errors were encountered: