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

No longer able to remove an added component from ComponentAdded or ComponentChanged subscriber #146

Closed
dkominek opened this issue Dec 13, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@dkominek
Copy link

dkominek commented Dec 13, 2021

Hey, I've been using DefaultEcs for a while now and updated from 0.15.2 to 0.17.0 recently.

It looks like it is no longer possible to remove an added component in a ComponentAdded or ComponentChanged subscriber. I did some digging into the changes made between those versions, and it looks like the issue is caused by a change made in version 0.16.0 to the Entity.Set method.

The Entity.Set function was changed from using the component argument on line 184 in 0.15.2 to using a call to Entity.Get instead on
line 89 in 0.16.0.

Is there any way you could include a Entity.Has check before using Entity.Get here? This issue will block my upgrade path of this great library.

You can reproduce this by creating a subscriber on ComponentAdded, and removing the component being added from in that subscriber.

public class ComponentRemover {
    public ComponentRemover(World world)
    {
        world.SubscribeComponentAdded(ValidateComponent);
    }

    private void ValidateComponent(in Entity entity, in MyComponent component)
    {
        // maybe some validation happens here and we need to remove the component...
        entity.Remove<MyComponent>(); // ERROR: this line will produce a IndexOutOfRangeException
    }
}

Something like below or if you have a better method:

if (Has<T>()) 
{
    ComponentManager<T>.GetPrevious(WorldId)?.Set(EntityId, ? Get<T>() : null);
}
@dkominek dkominek changed the title No longer able to remove an added component from WhenAdded or WhenChanged subscriber No longer able to remove an added component from ComponentAdded or ComponentChanged subscriber Dec 13, 2021
@Doraku Doraku added the bug Something isn't working label Dec 13, 2021
@Doraku
Copy link
Owner

Doraku commented Dec 13, 2021

Thanks for reporting you are totally right I will add some tests so this regression no longer occurs in the futur.

@dkominek
Copy link
Author

Any rough estimate of when a fix will be released? I will stay on 0.15.2 until then :)

@Doraku Doraku closed this as completed in 84fd55c Jan 30, 2022
@Doraku
Copy link
Owner

Doraku commented Jan 30, 2022

Really sorry I took this long to fix this, start of the year has been as crazy as the end of the previous one ^^" I hope I'll get some free time more frequently!

@dkominek
Copy link
Author

dkominek commented Feb 1, 2022

All good! I'm just happy you find any time to work on this library 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants