Using Generic Functions as Component Properties? #2559
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Rusty in the Discord suggests:
|
Beta Was this translation helpful? Give feedback.
-
You can't pass in concrete type for these "generic function types" because they don't have a name. Instead, as long as you don't require capture closures, you can do this: struct Condition<ConditionName>
{
name: ConditionName,
/// This will modify components on the Entity to remove the condition
pub add_condition: fn(Entity, &mut Commands),
/// This will modify components on the Entity to apply the condition
pub remove_condition: fn(Entity, &mut Commands),
pub applied: bool,
} The benefit of that approach is that you can have a single system that runs different add/remove logic for each entity. Depending on the context, you might be better served with something like this: struct Condition<T: ConditionFunctions>
applied: bool,
condition: T,
}
trait ConditionFunctions {
fn add_condition(entity: Entity, commands: &mut Commands);
fn remove_condition(entity: Entity, commands: &mut Commands);
}
struct SomeCondition;
impl ConditionFunctions for SomeCondition {
fn add_condition(entity: Entity, commands: &mut Commands) {
}
fn remove_condition(entity: Entity, commands: &mut Commands) {
}
} This locks you into a specific set of add/remove logic for |
Beta Was this translation helpful? Give feedback.
You can't pass in concrete type for these "generic function types" because they don't have a name. Instead, as long as you don't require capture closures, you can do this:
The benefit of that approach is that you can have a single system that runs different add/remove logic for each entity.
Depending on the context, you might be better served with something lik…