I'm trying to create a nice modify function of an entity's components in my WIP small "game framework". However I'm stuck at creating the function when trying to modify more than one component (using packed parameters)
This is my function for a single component, which works fine and behaves as I like
template <typename C>
void mod_comp(Entity ent, std::function<void(C&)> cb) {
auto& c = get_comp<C>(ent);
return cb(c);
}
// Called like this
mng.mod_comp<Velocity>(ent, [](auto& vel) {
// Modify velocity force value
vel.f += 5;
});
When trying to create the function for multiple components I've found 2 problems that I cant figure out how to solve.
template <typename... C>
void mod_comp(Entity ent, std::function<void(C...)> cb) {
return cb(get_comp<C>(ent)...);
}
// Supposed to be called like this
mng.mod_comp<Velocity, Position>(ent, [](Velocity vel, Position pos) {
pos.x += vel.f;
});
But this gives a "no match error" even tho the packed parameter ...C
(Velocity, Position
) matches the parameters in the lambda (Velocity, Position
). I can't figure out of to fix this.
The main problem however is if the lambda parameters can be simplified like the single mod function like this([](auto& vel, auto& pos)
and also forward as references. I feel like this should be possible as nothing is unknown to the compiler but my C++ is limited.