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: 
jbrave
Channel Surfer

objects and functions question

Hi,

I have a function like this:

bigfunc=init()

function init() as object
return {label:functionname,lable1:function1name,lable2:function2name}
end function


(really, a huge pile of functions in one object)

I have an array of items which each have an action with the same name as the lable in bigfunc:

item[1].action="lable1"
item[1].id="23423452"
item[2].action="lable2"
item[2].id="23256562"


what I want to do is to use the value stored in item.action to execute the function when the item is selected, but do it using the lable in the BigFunc array:

this=item[1].action
id=item[1].id

bigfunc.this(id)

it's not working, I'm getting:

Member function not found in BrightScript Component or interface

I tried simplifying and bypassing bigfunc to directly call the function:

this="function1name"
this(id)

but I get:

Member function not found in BrightScript Component or interface.

I can explicitly call bigfunc.lable1(id)

and the function executes.

is there a way to do this without using exec()

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
2 REPLIES 2
kbenson
Visitor

Re: objects and functions question

"jbrave" wrote:

this="function1name"
this(id)

but I get:

Member function not found in BrightScript Component or interface.

I can explicitly call bigfunc.lable1(id)

and the function executes.

is there a way to do this without using exec()

- Joel


That's PHP style, which is sort of it's own unique PHP thing (go figure).

I'm not sure, but if it's a regular function, you may have to use eval(). If it's actually a method on an object, you are in luck. Gander at this:

function func1()
print m.v1
end function

manualObj = {
v1: 1
f1: func1
f2: func2
}

print manualObj.v1 ' prints "1"
print manualObj["v1"] ' also prints "1"
varname = "v1"
print manualObj[varname] ' prints "1" yet again

manualObj.f1() ' prints "1"
manualObj["f1"]() ' also prints "1"
funcname = "f1"
manualObj[funcname]() ' prints "1" yet again


The thing to remember is that a variable can contain a function, just as it can contain a value. In this case, a function is just a special type, like an int or string. In a language this is called "first class functions" (http://en.wikipedia.org/wiki/First-class_function). It's different than PHP, much more powerful and actually more common. The way[ to determine whether you are calling the function or accessing it's code/contents is whether you append the parenthesis (manualObj.f1 vs manualObj.f1() above).
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
jbrave
Channel Surfer

Re: objects and functions question



funcname = "f1"
manualObj[funcname]() ' prints "1" yet again




This looks like what I'm looking for,

Thanks!

-Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.