Bevy: How should I remove a specific component from the entity.

Created on 20 Aug 2020  路  9Comments  路  Source: bevyengine/bevy

The usage of bevy-ecs makes me feel very comfortable. But when using Commands to modify the World, I did not find a method for removing only one components, so how should I remove only one component from the entity? Do I have to use the thread local system to get the world then remove it?

documentation ecs question

Most helpful comment

Note this should be avoided if possible, this is by design a pretty slow function: bevy tracks groups of entities that have the same components (such a group is called an archetype). When you remove a component, the entity will change archetype

All 9 comments

I wrote that function and that is the correct use case. :+1:

commands.remove_one::<SomeComponent>(entity);

Ah, close enough. I still haven't internalised the turbofish rules

Note that Commands::remove_one is not on a released version of bevy yet.

Do I have to use the thread local system to get the world then remove it?

Using the current version on crates.io, 0.1.2, that seems to be the only way.

Alternatively, you may specify bevy as a git dependency until the next release, something like:

# Cargo.toml 
[dependencies]
bevy = { git = "https://github.com/bevyengine/bevy", rev = "979a134" }

https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories

Note this should be avoided if possible, this is by design a pretty slow function: bevy tracks groups of entities that have the same components (such a group is called an archetype). When you remove a component, the entity will change archetype

Is there a god alternative to recommend? (For better performance) I am admittedly new to game design using ECS, but would plan on using components to add/remove statuses. E.g. add a poison component to deal damage over time and remove when healed or after some amount of time.

In cases like these (multiple seconds between adding/removing components for maybe a hundred entities), is the performance hit from changing archetypes a concern?

In cases like these (multiple seconds between adding/removing components for maybe a hundred entities), is the performance hit from changing archetypes a concern?

Probably not if it's at the scale of a second, if you're doing that a few thousands of times a frame it's going to start to take a toll

Is there a god alternative to recommend?

Another option would be to just add an Option<Poison> component from the beginning. For something like poison, it's probably fine to just use Poison component though

Yup Option is a good approach. You could also add a StatusEffect component, which then contains things like Poison inside.

Thank you everyone, I made a mistake. I forgot to check the master branch, so actually this should be what the next version will have.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cart picture cart  路  14Comments

cart picture cart  路  52Comments

cart picture cart  路  28Comments

aclysma picture aclysma  路  17Comments

NickelCoating picture NickelCoating  路  15Comments