Forum Discussion

tmat1075's avatar
tmat1075
Visitor
9 years ago

Is there a simple way to set and get a "global" variable?

I've got a login flow working, and after login in successfully, the API I'm using returns an "access token".  Coming from an ObjectOriented approach, I'd just set an object that contains the token and pass that around where ever I needed it.  That doesn't seem to be the paradigm with Roku/ScreenGraph.

Is there anyway to set that token after it's retrieved, and be able to access it easily in other parts of the program?   

4 Replies

  • destruk's avatar
    destruk
    Streaming Star
    You could set it as a global reference in the main.brs or main thread.
    For scenegraph --
    screen=CreateObject("roSGScreen") 'creates roSGScreen
    m.global=screen.getGlobalNode()
    m.global.addfield("token","string",FALSE)

    Then to set the variable use
    m.global.token=value
    Where value is the string you want to place into the global space.
    And then you can reference it within any component or routine in the app as m.global.token
    Or change it again anywhere in the app with m.global.token=value again
  • You can still pass it around just as OOP thought you, as long as you don't have to cross threads (main v render v task) - then things get more funky. Still doable but you would have to get rather specific what/where/how you want to pass around
  • Thanks destruk-- that worked.  I had tried using "m.global" before, but I wasn't adding the "m.global.addfield()" in main.brs.  That fixed everything.