Your Digital Media Has Never Looked So Good

 
mkdir1995
Topic Author
Posts: 34
Joined: Tue Feb 14, 2017 10:15 am

Global variables in Scene Graph

Wed Jan 31, 2018 2:41 pm

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!
 
User avatar
squirreltown
Posts: 824
Joined: Sun Apr 21, 2013 2:20 pm

Re: Global variables in Scene Graph

Wed Jan 31, 2018 3:16 pm

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
 
mkdir1995
Topic Author
Posts: 34
Joined: Tue Feb 14, 2017 10:15 am

Re: Global variables in Scene Graph

Tue Feb 06, 2018 3:05 pm

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!!  :D

Who is online

Users browsing this forum: No registered users and 2 guests