-
My goal is to return my own Entity wrapper class instead of The approach I tried: template <typename ComponentType> void Scene::onConstruct(std::function<void(Entity&)> func) {
registry.on_construct<ComponentType>().template connect([](entt::registry ®istry, entt::entity e) {});
} Even this simple example (without any way to call
I (hopefully) understand the limitations and design choices behind delegates, although many specific examples seem (to me) inconsistent. In theory I could also pass a pointer to the lambda as a sort of "capture" according to this blog post by @skypjack but as I couldn't even get the first example to compile this didn't help. Next I tried to use the unary operator as mentioned in #132 but that didn't help. (Although as I understand that wasn't supposed to be a working example as lambdas can't be used as template parameters.) From the error I could guess that the problem is related to that this is called from within a How could this be made to work? Are there any alternatives that I haven't yet considered that would be a better fit for my problem? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Okay, I found one solution. A class SceneCallback {
private:
Scene *scene;
EntityCallback func;
public:
SceneCallback(Scene *s, EntityCallback f);
void connect(entt::registry ®istry, entt::entity e);
};
...
void SceneCallback::connect(entt::registry ®istry, entt::entity e)
{
auto *entityP = scene->get(e);
if (entityP) {
func(*entityP);
}
} There needs to be a place where these callback objects are stored. class Scene {
public:
std::forward_list<Entity> entities;
std::forward_list<SceneCallback> callbacks;
...
template <typename ComponentType> void onConstruct(EntityCallback func);
...
}
...
template <typename ComponentType> void Scene::onConstruct(EntityCallback func)
{
registry.on_construct<ComponentType>().template connect<&SceneCallback::connect>(callbacks.emplace_front(this, func));
} It works, although if anyone has a better approach I would be glad to hear it. |
Beta Was this translation helpful? Give feedback.
-
Sorry for the late reply. I'm a little busy due to personal issues. 🙏 Consider that the whole signal support is a storage mixin in EnTT. Something you can swap off as you like. You can also replace it with your own implementation that works with lambdas if you prefer, it's also pretty easy to code I guess. |
Beta Was this translation helpful? Give feedback.
Sorry for the late reply. I'm a little busy due to personal issues. 🙏
The point is that delegates receive their callbacks as template arguments. You can't have lambdas in this case (well, you can have non-capturing lambdas with C++20, but it's technically a pointer to a free function at the end of the day).
Therefore, what you were trying to do in your first example wasn't possible. The last one gets the job done instead.
Consider that the whole signal support is a storage mixin in EnTT. Something you can swap off as you like. You can also replace it with your own implementation that works with lambdas if you prefer, it's also pretty easy to code I guess.
I decided to use delegates becaus…