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

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?   
0 Kudos
4 REPLIES 4
tmat1075
Visitor

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

And if it matters, the app is based on the Hero Grid code - https://github.com/rokudev/hero-grid-channel 
0 Kudos
RokuNB
Roku Guru

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

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
0 Kudos
destruk
Binge Watcher

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

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
0 Kudos
tmat1075
Visitor

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

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.
0 Kudos