Your Digital Media Has Never Looked So Good

 
steveww
Topic Author
Posts: 4
Joined: Thu Oct 06, 2016 7:19 am

Global variables across script and component

Thu Oct 06, 2016 7:26 am

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?
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Global variables across script and component

Thu Oct 06, 2016 11:29 am

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
 
steveww
Topic Author
Posts: 4
Joined: Thu Oct 06, 2016 7:19 am

Re: Global variables across script and component

Thu Oct 06, 2016 12:47 pm

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
Topic Author
Posts: 4
Joined: Thu Oct 06, 2016 7:19 am

Re: Global variables across script and component

Thu Oct 06, 2016 2:05 pm

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?
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Global variables across script and component

Thu Oct 06, 2016 5:34 pm

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.
 
steveww
Topic Author
Posts: 4
Joined: Thu Oct 06, 2016 7:19 am

Re: Global variables across script and component

Fri Oct 07, 2016 2:36 am

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.
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Global variables across script and component

Fri Oct 07, 2016 10:09 am

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)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Global variables across script and component

Fri Oct 07, 2016 10:15 am

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.

Who is online

Users browsing this forum: No registered users and 7 guests