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: 
groman00
Visitor

Observer callback functions

I'm seeing this on the 7.1 release notes:
Parameters can now be passed to observer callback functions (ifSGNodeField).
https://sdkdocs.roku.com/display/sdkdoc ... 04/05/2016

There's no mention of how to use parameters with observeField in the docs. Does anyone have an example?
https://sdkdocs.roku.com/display/sdkdoc/ifSGNodeField
Greg Roman
Senior Software Engineer, AOL Alpha
0 Kudos
5 REPLIES 5

Re: Observer callback functions

Hi Greg, it's possible to call an anonymous function that calls your function with parameters. Although it maybe be a bit troublesome it's just barely if it even is slower than calling it directly so it won't be noticeable at all. Please see the example below.

ObserveField("MyField", "AnonymousFunction")

Sub AnonymousFunction
MyFunction(MyField) <-- <Function that I want to call with parameters>
End Sub

Notice that I use MyField as just an example parameter. In this case MyField would be changed to the parameter that I want to set off the Observer. If you need more than one parameter passed these can be achieved through things like using the global variable, registry (not recommended), etc.
0 Kudos
EnTerr
Roku Guru

Re: Observer callback functions

Hi RokuChristianL - and welcome to the forum!

i don't think you understood the question, your answer does not relate to passing parameters to observer callback functions.
Of course inside a function one can call another function with arguments, that's not the point.
0 Kudos
EnTerr
Roku Guru

Re: Observer callback functions

@groman00 -
this was clarified a few days later by update in the doco page, see this diff for the changes - or just re-read "First Form" sub-section of ifSGNodeField.observeField()
0 Kudos
NicholasStokes
Binge Watcher

Re: Observer callback functions


I am running this file in task.xml

 

<?xml version=“1.0” encoding=“utf-8" ?>
<!-- Copyright 2016 Roku Corp. All Rights Reserved. -->
<component name=“ContentReader” extends=“Task”>
<script type=“text/brightscript”>
<![CDATA[
sub init()
m.top.functionName = “getAPIResponse”
end sub

sub getAPIResponse()
port = CreateObject(“roMessagePort”)
request = CreateObject(“roUrlTransfer”)
request.setRequest(“GET”)
request.setURL(m.top.contenturi)
jsonString = request.GetToString()
jsonParsed = ParseJson(jsonString)
m.top.content = jsonParsed
end sub
]]>
</script>
</component>

 

And Used this below function in main.brs file

 

sub Main()
print “in showChannelSGScreen”
m.readXMLContentTask = createObject(“roSGNode”, “ContentReader”)
m.readXMLContentTask.observeField(“content”, “setcontent”)
m.readXMLContentTask.contenturi = “http://www.sdktestinglab.com/Tutorial/content/xmlcontent.xml”
m.readXMLContentTask.control = “RUN”
readData = m.top.content
print “readData”
end sub

but this function is not executed in main.brs file
Can you please help me to execute this function from main.brs Or provide the sample examples

0 Kudos
renojim
Community Streaming Expert

Re: Observer callback functions

@NicholasStokes, I can't help you run the task from Main(), but there isn't any need to run a task from Main().  Just put the code from task.xml right in Main().  You can use roUrlTransfer in Main().  Also, please use code tags when posting code.  It makes it much, much, much easier to read.  To find it, you have to expand the toolbar above the edit box for your post and it looks like </>.

sub Main()
   print “in showChannelSGScreen”
   port = CreateObject(“roMessagePort”)
   request = CreateObject(“roUrlTransfer”)
   url = "http://www.sdktestinglab.com/Tutorial/content/xmlcontent.xml"
   request.setURL(url)
   jsonString = request.GetToString()
   jsonParsed = ParseJson(jsonString)
end sub

 

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.
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.