SpiderPig Posted January 21, 2023 Share Posted January 21, 2023 I would like to do something like this with the call-back for World::Pick(): bool CollisionCallback(shared_ptr<Entity> entity, shared_ptr<Object> extra) { if (entity->GetCollider() == nullptr) { return false; } return true; } ... function<bool(shared_ptr<Entity>, shared_ptr<Object>)> collision_callback = CollisionCallback; ... world->Pick(origin, target, radius, true, collision_callback, nullptr); I want to be able to change the callback function by setting the variable which is in a class. Can I convert an std::function to suit the argument somehow? bool filter(shared_ptr<Entity>, shared_ptr<Object>) ... PickInfo Pick(const Vec3& p0, const Vec3& p1,const dFloat radius = 0.0, const bool closest = false, bool filter(shared_ptr<Entity>, shared_ptr<Object>) = NULL, shared_ptr<Object> extra = NULL); Quote Link to comment Share on other sites More sharing options...
klepto2 Posted January 21, 2023 Share Posted January 21, 2023 You might be able to use std::bind like in the createthread sample. https://www.ultraengine.com/learn/CreateThread?lang=cpp here is some stackoverflow sample which might help you: https://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions 1 Quote Windows 10 Pro 64-Bit-Version NVIDIA Geforce 1080 TI Link to comment Share on other sites More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 Shouldn't you be able to store the function pointer in a class member? Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Solution Josh Posted January 21, 2023 Solution Share Posted January 21, 2023 I think this is weird design. You could call a class method from within the callback. If the same class might use different types of callbacks, then you would probably just create a class for your pick handling: class PickFilter { public: virtual bool Filter(shared_ptr<Entity> e) { return true; } }; class ColliderPickFilter : public PickFilter { public: virtual bool Filter(shared_ptr<Entity> e) { return e->collider != NULL; } }; bool Callback(shared_ptr<Entity> e, shared_ptr<Object> extra) { auto filter = extra->As<PickFilter>(); if (filter) return filter->Filter(e); else return true; } 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted January 21, 2023 Author Share Posted January 21, 2023 You mean instead of using an std::function? I suppose I could if theres no way to cast it. I have things setup using std::functions through my classes and was just wondering if there was actually a way to make it work. If not I'll use another method like you suggested. Quote Link to comment Share on other sites More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 When you use std::function any arguments in that function stay in memory as long as the function does. In many cases this can create circular references. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.