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: 
steveww
Visitor

Global variables across script and component

I want to set a global variable in my main.brs when the application starts. Then to be able to read that variable from inside <script> elements of my components.
I have read the obscure documentation and have not managed to figure out the correct incantation to get this to work.
Can someone please provide a code snippet showing how do this?
7 REPLIES 7
EnTerr
Roku Guru

Re: Global variables across script and component

The docs are $(OBSCURE), agreed.
There is no direct way to share variables between main and RSG. Short-if-incomplete explanation: "because Roku!".

There is a round-about way though, see https://sdkdocs.roku.com/display/sdkdoc ... lobalScope
0 Kudos
steveww
Visitor

Re: Global variables across script and component

I have read this section many times and it's as clear as mud.
I have tried various icantations around that theme and none of them have worked.

A working bit of example code would be a huge help here. The whole global variable, or lack thereof, is a mess IMHO.
steveww
Visitor

Re: Global variables across script and component

Have a huge amount of hours on Google I found this https://github.com/rokudev/podcast-player-channel
Why oh why isn't this in the documentation?
0 Kudos
EnTerr
Roku Guru

Re: Global variables across script and component

"steveww" wrote:
Have a huge amount of hours on Google I found this https://github.com/rokudev/podcast-player-channel
Why oh why isn't this in the documentation?

Great question!
I don't know. It's like as if there are two competing camps inside RokuCo, ones write to https://sdkdocs.roku.com, where others write to the newly created https://github.com/rokudev . But that's not all - even the Confluence wiki has >1 examples with same name but different content, linked from different pages. It's a clusterf... sharding, i mean. RTFM is getting sharded.

It's like the people that work in the same company and likely under the same roof do not talk to each other - just like what the case is between Brighscript and XML people - the resulting "Roku Screen Graph" is another cluster bomb. Utter hack. (The embarrassment kind - not the good kind of hack). Said teams should meet up, drink some beer and get their sheets together... please?

All examples should be in ONE place, there should be no dupes, no different versions, no confusingly close names. Both (competing?) doco sites should cross-reference each other; not pretend the other one does not exist.
0 Kudos
steveww
Visitor

Re: Global variables across script and component

Finally got to the bottom of why my script is not working with globals.

The docs miss out the need to call addField
m.glb = screen.getGobalNode()
' you need to do this
m.glb.addField("foo", "string", false)
m.glb.foo = "bar"

However Roku has one nasty surprise up its sleeve!
m.glb.addField("foo", "rubbish", false)
m.glb.foo = bar
This fails silently, it does not throw an error nor warning, nothing! It just does nothing. Then later when you want to retrieve the stored value it returns invalid. Aarrgghh!
This little gem wasted quite a few hours.
0 Kudos
TheEndless
Channel Surfer

Re: Global variables across script and component

"steveww" wrote:
Have a huge amount of hours on Google I found this https://github.com/rokudev/podcast-player-channel
Why oh why isn't this in the documentation?

I have long wondered why this stuff isn't at least posted in the developer forum, let alone the official documentation.  With that said, it's a good idea to keep an eye on the developer blog, as that seems to be Roku's go to for this kind of information: http://blog.roku.com/developer
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Global variables across script and component

Indeed, the fact that .addField()/setField() silently fail is a nasty bug!
Not only that but they are supposed to return true/false depending on success. Guess what, they always return true (WTF?), even with rendezvous timeout from what i remember.

Pro tip - just do:
m.global = screen.getGobalNode() 
m.global.addFields({foo: "bar"})

Easier to type and read. Type auto-detect. Still silent-but-deadly.
0 Kudos