Forum Discussion

steveww's avatar
steveww
Visitor
9 years ago

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?
  • 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" 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.
  • 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.
  • 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.