Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Komag
Roku Guru

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 2
belltown
Roku Guru

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.
0 Kudos
squirreltown
Roku Guru

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