Jump to content

World::Pick() Callback from a variable?


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

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

 

Link to comment
Share on other sites

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

  • Thanks 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

  • Solution

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

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

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.

 Share

×
×
  • Create New...