Forum Discussion

taylorcw's avatar
taylorcw
Streaming Star
8 years ago

Global Array Variable in Screengraph

I'm trying to set some m.global values in an array, but the push command is not setting anything.  What am I missing?

Following is in main.brs
m.global = screen.getGlobalNode()
m.global.addFields( {accessToken: []} )
m.global.accessToken.Push("testing_123")
print "Array Count: "; m.global.accessToken.Count()
print "Array Data: "; m.global.accessToken


Output in Debugger:

Array Count:  0
Array Data: <Component: roArray> =
[
]


Thanks
Chris

3 Replies

  • I have found some weirdness with trying to write to arrays and associative arrays that are members of a node.
    Try making a copy of the array, then updating it, then assigning the entire copied array to the field.
  • i modded quoted code a bit to focus on the issue:
    "taylorcw" wrote:
    globalNode = screen.getGlobalNode()
    globalNode.addFields( {accessToken: []} )
    globalNode.accessToken.Push("testing_123")

    Right, that won't work as expected. The reason being that when you access RSG node field, a copy is being made and that's what the operation works on. The globalNode is a Node - and even as using dot-notation seems like a garden-variety assoc.array access - it isn't. It does not return "the" references to the stored object, it returns "a" reference to a deep-copy of it - with all consequences from that, like being unable to mutate the original or the copy being slow.

    When evaluating above expression `globalNode.accessToken.Push("testing_123")`, first a copy of accessToken content is made, then .push() is called on it, which appends the new value - and on completion of the operation, the array is dropped (de-referenced and thus GC'd), as if that call were [].push("testing_123").

    See this discussed more in e.g. https://forums.roku.com/viewtopic.php?f ... 27#p533825

    There is no single elegant workaround. Depending on the use case, sometimes it's better to replace nested structures of roArray/roAA with hierarchical Node structures. I.e. translate roAA into node fields (cue addFields()) and roArray into children (cue addChildren()).
  • taylorcw's avatar
    taylorcw
    Streaming Star
    Thanks for the replies. This new screengraph programming is going to kill me with nuances like this.

    tim_beynart -- i used your suggestion and it worked. 

      testArray=createObject("roArray", 0, true)
      testArray.Push("testing_123")

      m.global.addFields( {accessToken: []} )
      
      m.global.accessToken = testArray
      print "Array Count: "; m.global.accessToken.Count()
      print "Array Data: "; m.global.accessToken


    Output:
    Array Count:  1
    Array Data: <Component: roArray> =
    [
       "testing_123"
    ]


    I was able to to Push into a local array followed by m.global.accessToken replace on other nodes which seems to be working.

    Thanks guys!