Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use CommandBuffer.SetComponent then crash in Playback #31

Closed
vkensou opened this issue Nov 11, 2024 · 3 comments
Closed

use CommandBuffer.SetComponent then crash in Playback #31

vkensou opened this issue Nov 11, 2024 · 3 comments

Comments

@vkensou
Copy link

vkensou commented Nov 11, 2024

I use CommandBuffer in QuerySystem.Update

            for (int i = 0; i < entityList.Length; i++)
            {
                var e1 = entityList[i];
                cmd.SetComponent(e1.entityId, new Position(e1.position));
            }

Then crash when Playback

KeyNotFoundException: The given key '1' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <51fded79cd284d4d911c5949aff4cb21>:0)
Friflo.Engine.ECS.ComponentCommands`1[T].SendCommandEvents (Friflo.Engine.ECS.Playback playback) (at F:/code2/others/Friflo.Engine.ECS/src/ECS/CommandBuffer/Commands/ComponentCommands.cs:165)
Friflo.Engine.ECS.CommandBuffer.SendCommandEvents (Friflo.Engine.ECS.Playback playback) (at F:/code2/others/Friflo.Engine.ECS/src/ECS/CommandBuffer/CommandBuffer.cs:373)
Friflo.Engine.ECS.CommandBuffer.Playback () (at F:/code2/others/Friflo.Engine.ECS/src/ECS/CommandBuffer/CommandBuffer.cs:190)
ECS2.CollidePushSystem.OnUpdate () (at Assets/Scripts/ECS2/CollidePushSystem.cs:113)
Friflo.Engine.ECS.Systems.QuerySystemBase.OnUpdateGroup () (at F:/code2/others/Friflo.Engine.ECS/src/ECS/Systems/QueryBaseSystem.cs:101)
Friflo.Engine.ECS.Systems.SystemGroup.OnUpdateGroup () (at F:/code2/others/Friflo.Engine.ECS/src/ECS/Systems/SystemGroup.cs:290)
Friflo.Engine.ECS.Systems.SystemGroup.Update (Friflo.Engine.ECS.UpdateTick tick) (at F:/code2/others/Friflo.Engine.ECS/src/ECS/Systems/SystemGroup.cs:264)
Friflo.Engine.Unity.ECSSystemSet.Update () (at F:/code2/others/friflo-ecs-unity/Scripts/Runtime/Components/ECSSystemSet.cs:111)

the source file:

internal override void SendCommandEvents(Playback playback)
{
    var indexes     = playback.entityChangesIndexes;
    ...
   ref var change  = ref changes[indexes[entityId]];
}

entityChangesIndexes is empty.
Seems entityChangesIndexes is only used in ExecuteTagCommands and UpdateComponentTypes. If I juse UpdateComponent, it always empty.

@friflo
Copy link
Owner

friflo commented Nov 11, 2024

Hi @vkensou

I can reproduce this issue.
For a workaround you should be able to use cmd.AddComponent() instead of cmd.SetComponent()

friflo added a commit that referenced this issue Nov 12, 2024
@friflo
Copy link
Owner

friflo commented Nov 12, 2024

Decided to make CommandBuffer.SetComponent() obsolete.
In the next version it will have the same behavior as AddComponent().

The idea for SetComponent() was throwing an exception if an entity is missing the given component.

Reasons:

  • It could be confusing:
    • if removing the component before calling SetComponent()
    • if calling AddComponent() before calling SetComponent()
  • The performance will drop when implementing a specific SetComponent()

See: #31

@friflo friflo closed this as completed Nov 12, 2024
@friflo
Copy link
Owner

friflo commented Nov 12, 2024

Fixed in 3.0.0-preview.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants