# Functions in user data of ComboBox's items array cannot be called

**URL:** https://qa.fmod.com/t/functions-in-user-data-of-comboboxs-items-array-cannot-be-called/16123
**Category:** FMOD Studio
**Created:** [August 29, 2020, 6:39am UTC](https://qa.fmod.com/t/functions-in-user-data-of-comboboxs-items-array-cannot-be-called/16123 "2020-08-29T06:39:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![JeffreyKumley](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@JeffreyKumley](https://qa.fmod.com/u/JeffreyKumley)
#### Post date: [August 29, 2020, 6:39am UTC](https://qa.fmod.com/t/functions-in-user-data-of-comboboxs-items-array-cannot-be-called/16123/1 "2020-08-29T06:39:23Z")

</div>

In the console window’s terminal, enter the following:

```javascript
studio.ui.showModelessDialog( {
    windowTitle: "Hello world",
    widgetType: studio.ui.widgetType.Layout,
    layout: studio.ui.layoutType.VBoxLayout,
    contentsMargins: { left: 0, top: 0, right: 0, bottom: 0 },
    items: [
        {
            widgetId: "mSoundTypes",
            widgetType: studio.ui.widgetType.ComboBox,
            items: [
                { text: "SingleSound", userData: 9 },
                { text: "MultiSound", userData: function() { return 3; } }
            ],
            currentIndex: 0,
            onCurrentIndexChanged: function() {
                console.log(this.currentText());
                console.log(this.currentUserData());
                console.log(this.currentUserData()());
            }
        }
    ]
} )

```

If desired this is the same code, but as one line: `studio.ui.showModelessDialog( { windowTitle: "Hello world", widgetType: studio.ui.widgetType.Layout, layout: studio.ui.layoutType.VBoxLayout, contentsMargins: { left: 0, top: 0, right: 0, bottom: 0 }, items: [{ widgetId: "mSoundTypes", widgetType: studio.ui.widgetType.ComboBox, items: [ { text: "SingleSound", userData: 9 }, { text: "MultiSound", userData: function() { return 3; } }], currentIndex: 0, onCurrentIndexChanged: function() { console.log(this.currentText()); console.log(this.currentUserData()); console.log(this.currentUserData()()); } } ] } )`

Next, select each option in the dropdown.  
Observe that the first option correctly prints `SingleSound`, prints `9`, and then errors with 9 not being a function.  
Observe that the second option correctly prints `MultiSound`, but then incorrectly prints `[object Object]`, and then errors with object not being a function.

Note that this is in contrast to entering the following into the terminal:

```javascript
myObject = { foo: 0, bar: "hello", buzz: function() { return 7; } }

```

After which, each of the following work fine and give the expected result:

- `myObject.foo` prints `0`
- `myObject.bar` prints `hello`
- `myObject.buzz` prints `function () { return 7; }`
- `myObject.buzz()` prints `7`

The existing behavior greatly limits the usability of the dropdowns. Nevertheless, there are workarounds, so I don’t think this is a high priority issue. It is also fairly niche I think, since there are so few questions about the scripting API’s custom UI on the forums.

One such workaround is to use each element as an index into an array of functions stored elsewhere or as a value to match against in a switch statement.

Another workaround is to construct the function later such as below:

```javascript
studio.ui.showModelessDialog( {
    windowTitle: "Hello world",
    widgetType: studio.ui.widgetType.Layout,
    layout: studio.ui.layoutType.VBoxLayout,
    contentsMargins: { left: 0, top: 0, right: 0, bottom: 0 },
    items: [
        {
            widgetId: "mSoundTypes",
            widgetType: studio.ui.widgetType.ComboBox,
            items: [
                { text: "SingleSound", userData: 9 },
                { text: "MultiSound", userData: "return parameterValue * 2" }
            ],
            currentIndex: 0,
            onCurrentIndexChanged: function() {
                console.log(this.currentText());
                console.log(this.currentUserData());
                var foo = new Function("parameterValue", this.currentUserData());
                console.log(foo(3));
            }
        }
    ]
} )

```

Although it does not work to construct the function in place as below:

```javascript
studio.ui.showModelessDialog( {
    windowTitle: "Hello world",
    widgetType: studio.ui.widgetType.Layout,
    layout: studio.ui.layoutType.VBoxLayout,
    contentsMargins: { left: 0, top: 0, right: 0, bottom: 0 },
    items: [
        {
            widgetId: "mSoundTypes",
            widgetType: studio.ui.widgetType.ComboBox,
            items: [
                { text: "SingleSound", userData: 9 },
                { text: "MultiSound", userData: new Function("parameterValue", "return parameterValue * 2") }
            ],
            currentIndex: 0,
            onCurrentIndexChanged: function() {
                console.log(this.currentText());
                console.log(this.currentUserData());
                console.log(this.currentUserData()(3));
            }
        }
    ]
} )

```

Anyway, it would be nice if the UI system supported functions the same way they are supported for object literals.

---

<div class="post-metadata">

### Author: ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)
#### Post date: [September 8, 2020, 7:01am UTC](https://qa.fmod.com/t/functions-in-user-data-of-comboboxs-items-array-cannot-be-called/16123/2 "2020-09-08T07:01:56Z")

</div>

This is definitely a bug, thank you for bringing this to our attention. We’ve raised a bug report and will take a look into it for a future release.
