# Way to pass parameter to InterfaceGl button callback?

**URL:** <https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532>\
**Category:** Using Cinder\
**Created:** [January 31, 2017, 10:38pm UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532 "2017-01-31T22:38:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![camb416](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/camb416/32/237_2.png) [@camb416](https://discourse.libcinder.org/u/camb416)\
**Post date:** [January 31, 2017, 10:38pm UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532/1 "2017-01-31T22:38:23Z")

</div>

Hi all,

Is there a way to pass a parameter to the callback function for a button in InterfaceGl?

Here’s my code (without a param)

```auto
void MyApp::setup(){
    mParams = ci::params::InterfaceGl::create( ci::app::getWindow(), "App parameters", ci::app::toPixels( ci::ivec2( 200, 400 ) ) );
    mParams->addButton( "Click me", bind( &MyApp::button, this ) );
}

void MyApp::button(){
    // Can this method receive an int or string parameter ie. the label of the button
    // or is there a way to getOptions?
}

```

Unsure of how to approach this problem…

Any help very appreciated!

---

<div class="post-metadata">

**Author:** ![gabor\_papp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/gabor_papp/32/769_2.png) [@gabor\_papp](https://discourse.libcinder.org/u/gabor_papp)\
**Post date:** [February 1, 2017, 7:17am UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532/2 "2017-02-01T07:17:13Z")

</div>

Hi,

Would you like to handle all buttons in one callback function? As far as I know, this is not really the way how Params is designed, but you can work around it. For example, with something like this:

```auto
void MyApp::setup{
    ...
    mParams->addButton( "Click me 0", [&]() { button( 0 ); } );
    mParams->addButton( "Click me 1", [&]() { button( 1 ); } );
}

void MyApp::button( int buttonId ){
    ...
}

```

-Gabor

---

<div class="post-metadata">

**Author:** ![camb416](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/camb416/32/237_2.png) [@camb416](https://discourse.libcinder.org/u/camb416)\
**Post date:** [February 2, 2017, 6:25am UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532/3 "2017-02-02T06:25:13Z")

</div>

Hi Gabor,

Thanks for the suggestion. This solves it for the moment.

Ideally, I’d like to have a single callback with some parameter indication what the originating button is, without having to duplicate that in the addButton. I just feel that it’d be the most succinct way of creating a lot of buttons with a simple switch/case statement that ideally referenced the labels of the buttons to limit the mystery between the visual display and the code.

Thanks for the recommendation, it’s working perfectly…

---

<div class="post-metadata">

**Author:** ![nay](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nay](https://discourse.libcinder.org/u/nay)\
**Post date:** [February 2, 2017, 9:56am UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532/4 "2017-02-02T09:56:36Z")

</div>

You can pass a variable into a lambda, maybe some variation on this would be useful:

```auto
for (int i=0; i<numButtons; i++) {
  mParams->addButton( "Click me " + toString(i), [&, i]() { button( i ); } );
}

```

---

<div class="post-metadata">

**Author:** ![rich.e](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/rich.e/32/1025_2.png) [@rich.e](https://discourse.libcinder.org/u/rich.e)\
**Post date:** [February 2, 2017, 2:40pm UTC](https://discourse.libcinder.org/t/way-to-pass-parameter-to-interfacegl-button-callback/532/5 "2017-02-02T14:40:39Z")

</div>

In practice, I usually find that using separate callback handlers for each button is easier to follow, especially if you weren’t the original person writing the code. That, and not having to pass an extra argument through the lambda saves a bit of typing in the most common case.
