Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Komag
Level 9

How to run a function in an array from another function

The question went away but I figured this still might be helpful for others...

Example of having a button call a function based on current array choice:

         Sub makesound()
? "Making sound"
end Sub

array = []
arrayitem = { title:"sound", action:makesound}
array.Push(arrayitem)

if button=codes.BUTTON_SELECT_PRESSED
doit=array[0].action()
end if

The () should be left off the name of the function until it's time to actually run the function

(Have I missed anything?)
0 Kudos
2 Replies
belltown
Level 7

Re: How to run a function in an array from another function

That code will run, although 'doit' will always be Invalid since a Sub never returns anything.

Other than that your assertion is correct. When you store a reference to a function in an associative array or an array or in any other variable, you are doing just that, storing a reference to the function (its address if you like). Any time you have a function or a reference to a function stored in a variable, you invoke the function by using the function call operator (), which executes the function passing in any parameters enclosed within the parentheses and returns the result from any Return statement.
https://github.com/belltown/
0 Kudos
Highlighted
squirreltown
Level 9

Re: How to run a function in an array from another function

Thanks Komag & belltown, I figured it out - actually read the Docs!
Kinetics Screensavers
0 Kudos