Forum Discussion

mkdir1995's avatar
mkdir1995
Visitor
8 years ago

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!

2 Replies

  • 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.
  • "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!!  😄