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: 
belltown
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

That's the idea. Try it and see what happens.
0 Kudos
Motorcykey
Visitor

Re: Accessing a global object inside a SceneGraph Component

I just had the chance to try and my observer is not working. Or at least what I'm finding is that it's not being dispatched because 'm.global.usertype' isn't actually changing.

The code looks to be getting executed, however, when I output the 'm.global' SG Node afterward, I receive the following:

<Component: roSGNode> =
{
    change: <Component: roAssociativeArray>
    focusable: false
    focusedChild: <Component: roInvalid>
    id: "GlobalNode"
    usertype: "Preview"
}

The 'usertype' property is there, but still contains the value it was initialized at, which tells me there is something wrong with how I'm attempting to set the value in my else block.

I can comment up my code to hopefully give you a better idea of what I'm trying to achieve:

SubscribeScreen.brs

if (m.global = invalid)
   ' This block executes when the user opens the screen for the first time
    m.global = screen.getGlobalNode()
    m.global.id = "GlobalNode"
    m.global.addFields( {userType: "Preview"} ) ' This line adds "userType": "Preview" to the m.global roSGNode
else
   ' on subsequent opens, 'm.global' has been initialized, so we just want to update its 'userType' value which could have changed
    m.global.userType = "Monthly"
end if

RowListScene.brs

function init()
    m.top.backgroundURI = "pkg:/images/background.png"
    m.top.backExitsScene = true

   m.global.observeField("userType", "userTypeChanged") ' setting up my observer in the init() function
    ...
end function

' defining my observer handler
function userTypeChanged()
   ' but I never see this called, most likely because I have never seen the value update
    print "USER TYPE HAS CHANGED"
    m.userType = m.global.userType
end function
0 Kudos
belltown
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

Have you confirmed that the code m.global.userType = "Monthly" is actually executing?
0 Kudos
Motorcykey
Visitor

Re: Accessing a global object inside a SceneGraph Component

Only in that a print statement will log out when placed in that else block. Also, the app doesn't get hung up. It's an assumption, but I'm unsure how I would 100% confirm.
0 Kudos
Motorcykey
Visitor

Re: Accessing a global object inside a SceneGraph Component

If I place print statements before & after the line:
m.global.userType = "Monthly"
Both print statements log out in my debugger. So I am inclined to think that the code is being executed, but again it is just an assumption.
0 Kudos
belltown
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

You could put a print statement in SubscribeScreen.brs to see if it is getting initialized again.
0 Kudos
Motorcykey
Visitor

Re: Accessing a global object inside a SceneGraph Component

I found my solution and I think I need to abandon any hope of using the Global Node. I went with adding a new field to my screens interface:
<field id="userType" type="string" />

I was able to modify this value by setting it right before the while loop (and after the screen.Show() method) of my SubscribeScreen.brs file:
scene.userType = m.context.subscriptionType

Which I was then able to add an observer if any changes were made to "m.top.userType" within RowListScene.brs:
m.top.observeField("usertype", "userTypeChanged")
0 Kudos
EnTerr
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

"Motorcykey" wrote:
I didn't! So it sounds like it's best practice that for any SceneGraph Component I create that needs access to my functions from util.brs, I would basically "require" them as a dependency as follows. Makes sense!
<script type="text/brightscript" uri="pkg:/components/util.brs" />


It is not a "best practice" - it is "the only practice", since there is no other way to do it. Each XML component you define sees only the scripts explicitly pointed at, each lives in its own little world.

And for amusement's sake, i can say that this is also a "worst practice", just like it is a "best practice" - when you have freedom of choice of only 1 item, it's both the best and the worst in the series  :mrgreen:
0 Kudos
EnTerr
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

"Veeta" wrote:
"joetesta" wrote:
... Within the same file, define the functions '_someFunction' and '_getSomething'
Now you can instantiate myUtils from anywhere ( util = myUtils() ) and pass in variables to the defined methods ( result = util.getSomething(somevars) ).

Your example hit upon nearly the same pattern i use for my brs modules.  I don't think it's been formalized or blogged about anywhere but it would be nice if more Roku developers adopted this style.  I'll leave in-depth discussion for a different thread.

There is very little to no utility in doing so if there is no real data in the object.
Why do it then? You cannot pass the resulting AA outside scope (because: RSG) and inside the scope the "underscore" functions are already visible, so...
0 Kudos
EnTerr
Roku Guru

Re: Accessing a global object inside a SceneGraph Component

"Motorcykey" wrote:
Setting the values works when the screen is first called, however, the screen can be opened more than once. For example, logging out and logging back in again.

Current attempts to log back in just freeze the app when hitting the line:
m.global.addFields

I can't seem to call any roSGNode methods on m.global from SubscribeScreen.brs, however I can inside RowListScene.brs.

Wait a minute - are you telling me that you create >1 roSgScreen - as in, multiple times creating and closing a roSgScreen? And that you expect .getGlobalNode() on different RSG screens to return the same value (and not say, alternatively hang or crash because nobody has tried doing such crazy thing 🙂 )?
0 Kudos