Jump to content

World::Pick() Callback from a variable?


Go to solution Solved by Josh,

Recommended Posts

Posted

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);

 

  • Solution
Posted

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;
}

 

  • Thanks 1

Let's build cool stuff and have fun. :)

Posted

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.

Posted

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.

  • Thanks 1

Let's build cool stuff and have fun. :)

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...