Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Level 7

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

If you need to set a global variable within your main file (where your screen is created and set) you can do it via the global node. = screen.getGlobalNode() = "GlobalNode" {
    globalVariable: yourVariableHere
  } )

If you need to set a global variable outside of where your screen is set, I found a very helpful function in one of the examples (I forgot which one) but I use it in my code as well...
function AddAndSetFields( node as object, aa as object )
  addFields = {}
  setFields = {}
  for each field in aa
    if node.hasField( field )
      setFields[ field ] = aa[ field ]
      addFields[ field ] = aa[ field ]
    end if
  end for
  node.setFields( setFields )
  node.addFields( addFields )
end function

And then I use the function in a separate .brs file like so:

AddAndSetFields(, { globalContent: myVariableHere } )     

After this is set and working correctly, I can retrieve and use the variable I need by calling 😄
Hope that helps! I actually got that figured out by help from the forum!
0 Kudos