Question

I'm currently going from C# to C++ and rewriting some game engine code and I think I'm at a wall with tr1; Essentially what I want to do is have an input layer take input from the touchscreen and then fire a callback to notify any elements listening to that input. Now tr1/boost seem to make this super easy via the use of function and bind, however firing callbacks that are void() is no problem, but now I'm trying to send parameters through to these callback when invoke (enums for instance).

The problem here is that when I call bind, and insert the parameter, it doesn't act as a place holder rather it acts as the permanent value to be passed. For instance my code here:

Containing class:

tr1::function<void (MenuActions action)> callback;
callback = tr1::bind(&MenuScene::handleInputCallback, this, MenuActions::neutral);
m_menuUILayer->registerCallback(callback);

Has the intent that I create a function and bind it then registerCallback passes this callback into the member class and stores the reference. This part works, but now in the member class I can invoke the callback however the default parameters are always sent...

Member class:

if (m_callback)
{
    m_callback(MenuActions::backgroundTouchDown);
}

So here rather than MenuActions::backgroundTouchDown being sent to the main containing class in the callback, the default MenuActions::neutral is still being used. I'm wondering if I'm missing something, or perhaps my lack of sleep is just sending me down the wrong path? Thanks guys!

Was it helpful?

Solution

You should use placeholders

callback = tr1::bind(&MenuScene::handleInputCallback, this,
tr1::placeholders::_1);

and then call like

if (m_callback)
{
    m_callback(MenuActions::backgroundTouchDown);
}

and your function handleInputCallback can have default parameter, defaulted to MenuActions::neutral

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top