Handling multiple similar (enemy) types #9245
Replies: 1 comment 2 replies
-
Bevy is an ECS framework, thus it makes sense to go the latter route.
Rust can do this. In basic Rust, you would return either In Bevy, spawning different things as in your example works just fine (what did the borrow checker complain about?).
If you want to go the way you are going, the most straightforward implementation is generic functions: pub trait EnemyMovement: Component /*+ etc.*/ {...};
pub fn move_enemies<T: EnemyMovement>(
mut enemy_query: Query<(&mut Transform, &mut T)>,
...
) {
for (mut enemy, mut movement) in enemy_query.iter_mut() {
movement.move_enemy(...);
}
} But usual ECS approach is to have multiple systems, each of which contains movement code for each individual type (no traits needed). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is probably more design and/or Rust question, but to me it seems many games need this and I couldn't find any example or question related to the same problem.
My game has multiple enemy types. Every type has some common behaviour like movement and stats. Most of the behaviour is shared - each enemy type moves directly to the Player location - unless enemy is in some special state in which each has a different movement pattern.
Right now, I ended up simply copy-pasting many parts of the code because I have only three enemy types, but I keep thinking there must be a better way. I can't figure out two things:
I was hoping Traits could solve my problem, so I ended up having Movement trait and implementing it for each Enemy type
For spawning, I have a huge setup with colliders, materials etc. and wanted to simply add some of my
EnemyMovement
s based on RNG. And not having to copy the setup for each enemy type.But Rust cannot do this because return type for each branch is different. So I ended up with what I thought is a more sophisticated solution.
But borrow checker prevented that.
I also thought I would be able to have one movement system so I can at least move every enemy type at once. But seems like I also cannot query with dynamic types/traits like that.
Am I really missing something obvious (I hope so!)? Or am I using ECS in a completely wrong way? Or is this possible only using some expert knowledge Rust magic I obviously lack? There must be a better way! 🙂
Beta Was this translation helpful? Give feedback.
All reactions