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

Global variables in Scene Graph

I'm wondering what the best way to create global variables within the Scene Graph SDK is. With the old SDK, I was able to just store everything in a .json file and use ReadAsciiFile() to reference the variables. I was using it in my main() function. 

variables.json file:

{
 "serverURL":             "http://api.server.com",
  "id":                    1234,
  "appVersion":           "1.1.0",
}


main.brs:

  configureAsString = ReadAsciiFile("pkg:/config/variables.json")
  m.append(ParseJSON(configureAsString))


According to this link https://sdkdocs.roku.com/display/sdkdoc/BrightScript+Support it says I can only use ReadAsciiFile() in a Task Node, which leads me to my questions:
1) Is there a simpler way? Do I NEED to create a Task Node? I'm still wrapping my mind around them, lol
2) If I absolutely need to create a task node, what is the most basic way? All I really need is to have a file with a list of variables that I can reference often throughout the code. Such as IDs, URLs, etc.

Any help would be appreciated! Thank you!
0 Kudos
2 REPLIES 2
squirreltown
Roku Guru

Re: Global variables in Scene Graph

screen = CreateObject("roSGScreen")
port = CreateObject("roMessagePort")
screen.setMessagePort(port)
m.global = screen.getGlobalNode()
m.global.AddField("mystuff", "assocarray", false)
m.global.mystuff = parseJSON(readAsciiFile("pkg:/your.json"))
scene = screen.CreateScene("Main")
screen.show()

I do it this way. m.global.mystuff will be available everywhere.
Kinetics Screensavers
0 Kudos
mkdir1995
Visitor

Re: Global variables in Scene Graph

"squirreltown" wrote:
screen = CreateObject("roSGScreen")
port = CreateObject("roMessagePort")
screen.setMessagePort(port)
m.global = screen.getGlobalNode()
m.global.AddField("mystuff", "assocarray", false)
m.global.mystuff = parseJSON(readAsciiFile("pkg:/your.json"))
scene = screen.CreateScene("Main")
screen.show()

I do it this way. m.global.mystuff will be available everywhere.

totally missed this reply, but I ended up using a Global Node as well and was able to easily retrieve it in the XML component. Thanks!!  😄
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.