taylorcw
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2017
09:47 AM
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
Output in Debugger:
Thanks
Chris
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 3
tim_beynart
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2017
10:46 AM
Re: Global Array Variable in Screengraph
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.
Try making a copy of the array, then updating it, then assigning the entire copied array to the field.
NB_
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2017
12:40 PM
Re: Global Array Variable in Screengraph
i modded quoted code a bit to focus on the issue:
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" 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
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2017
05:36 PM
Re: Global Array Variable in Screengraph
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.
Output:
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!
@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!