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: 
FCF-Ben
Reel Rookie

Not getting observeField (or observeFieldScoped) to firing callback

Jump to solution

Trying to mash together two (or three) examples I found, to create a dialog that will fire if you press back arrow when on the home screen (or technically the last screen in the stack array). 

So what I am trying to achieve is a logic along the lines of:

1. Intercept the backarrow key (onkeyevent) 

2. Display dialog for "Confirm Exit" 

3 observeField "buttonSeleted" and set callback to "onButtonSelected"

4. Return true to tell Roku that we handled the backarrow

Then later (buttonSelected)

5. set m.top.exitApp = true (which breaks the while loop and exits the program)

I'm fairly certain step 1-4 is working, but step 5 is never called. (the print to the debug log never happens)

 

Here is the code:

  sub showExitDialog()
    m.exitDialog = createObject("roSGNode", "Dialog")
    m.exitDialog.backgroundUri = "pkg:/images/dialog-background.png"
    m.exitDialog.title = "Close channel"
    m.exitDialog.buttons = ["Exit", "Cancel"]
    m.exitDialog.optionsDialog = true
    m.exitDialog.message = "Do you want to close Channel"
    m.exitDialog.observeField("buttonSeleted", "onButtonSelected")
    m.top.dialog = m.exitDialog
  end sub

  'detect selection
  sub onButtonSelected()
    'use the exitDialoge Button Selected field
    print "Serring Exit to true"
    if m.exitDialog.buttonSelected = 0 then
        print "Exiting app"
        m.top.exitApp = true
    else
        print "Doing nothing"
    end if
  end sub
And the code to intercept the back arrow
function onKeyEvent (key as String, press as boolean) as boolean
    handled=false
    if press then 
        if (key = "back") then
            handled=true
            print "You pressed back"
            showExitDialog()
      end if
    end if
  return handled
end function

 

 



Also, I stole from two different examples, where the first one is 

https://developer.roku.com/en-gb/docs/references/scenegraph/standard-dialog-framework-nodes/standard...

And the second one was how to break the while loop

https://rymawby.com/brightscript/roku/Exiting-out-of-a-brightscript-scenegraph-app.html

 

Also, the example used observeFieldScoped, but could not find any documentation on what that does. (A bit new to brigthscript, so sorry if that is obvious)

0 Kudos
1 Solution

Accepted Solutions
renojim
Community Streaming Expert

Re: Not getting observeField (or observeFieldScoped) to firing callback

Jump to solution

If the code you've shown is a copy and paste then you have a typo:

 

m.exitDialog.observeField("buttonSeleted", "onButtonSelected")

Should be buttonSelected.

 

But that's not the only problem.  Even if you fix the line, add a print in front of it and I bet it prints out false meaning the statement failed and your observer will never be called.

 

print m.exitDialog.observeField("buttonSelected", "onButtonSelected")

That "buttonSelected" needs to be attached to a component.  I'm not sure how to accomplish that; I've only ever done it via an interface alias in the XML file for the node/group/component or whatever it's called.

 

XML:

<interface>
<field id="button_selected"
type="assocarray"
alias="dialog.buttonSelected"
/>
</interface>
<children>
<Dialog
id="dialog"
/>
</children>
</component>

BRS:

print m.top.observeField("button_selected", "onButtonSelected")

I tried a quick test trying to add it to the code you have, but I couldn't come up with the right way to specify it or I had some other problem with the code since it's not complete.

 

As for observeField vs observeFieldScoped, I've wondered the same thing, but I've never taken the time to investigate it.

I have to include my standard caveat:  As I've stated many times, I'm not the person to ask about scene graph as I absolutely hate it.  Maybe if I wasted took the time to learn more than the bare minimum I wouldn't hate it so much, but I don't see that ever happening.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.

View solution in original post

2 REPLIES 2
renojim
Community Streaming Expert

Re: Not getting observeField (or observeFieldScoped) to firing callback

Jump to solution

If the code you've shown is a copy and paste then you have a typo:

 

m.exitDialog.observeField("buttonSeleted", "onButtonSelected")

Should be buttonSelected.

 

But that's not the only problem.  Even if you fix the line, add a print in front of it and I bet it prints out false meaning the statement failed and your observer will never be called.

 

print m.exitDialog.observeField("buttonSelected", "onButtonSelected")

That "buttonSelected" needs to be attached to a component.  I'm not sure how to accomplish that; I've only ever done it via an interface alias in the XML file for the node/group/component or whatever it's called.

 

XML:

<interface>
<field id="button_selected"
type="assocarray"
alias="dialog.buttonSelected"
/>
</interface>
<children>
<Dialog
id="dialog"
/>
</children>
</component>

BRS:

print m.top.observeField("button_selected", "onButtonSelected")

I tried a quick test trying to add it to the code you have, but I couldn't come up with the right way to specify it or I had some other problem with the code since it's not complete.

 

As for observeField vs observeFieldScoped, I've wondered the same thing, but I've never taken the time to investigate it.

I have to include my standard caveat:  As I've stated many times, I'm not the person to ask about scene graph as I absolutely hate it.  Maybe if I wasted took the time to learn more than the bare minimum I wouldn't hate it so much, but I don't see that ever happening.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
FCF-Ben
Reel Rookie

Re: Not getting observeField (or observeFieldScoped) to firing callback

Jump to solution

hank you so very much @renojim 

And sorry for not responding yesterday, did not get a notification that I had a response to my question. 

 

And yes, I've been staring myself blind trying to figure out what part of the data inheritance structure I just don't understand, should have checked the spelling first 🙂

Thank you so very very much.

Now it works in that the dialog tires to send the message, but the While loop is never broken, and the app does not end. 

0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.