Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
haamro
Level 7

[SOLVED] Returning value from function with Task

Hello, I am trying to get a content from the web using Task. The Sub works great, except I am having difficulty returning the value from the function. Can somebody help me.

<?xml version="1.0" encoding="utf-8" ?>
 
<component name = "postergridCR" extends = "Task" >
 
  <interface>
    <field id = "postergriduri" type = "string" />
    <field id = "postergridcontent" type = "string"/>
  </interface>
 
  <script type = "text/brightscript" >
    <![CDATA[
 
    sub init()
      m.top.functionName = "getContent"
    end sub
 
    sub getContent()
      readInternet = createObject("roUrlTransfer")
      readInternet.setUrl(m.top.postergriduri)
      postergridcontent = readInternet.GetToString()
      m.top.postergridcontent = postergridcontent
   end sub
 
    ]]>
  </script>
 
</component>

I am trying to return the content from the above function
function getwebcontent(webpage as string)

   m.myObj = createObject("RoSGNode", "postergridCR")
   m.myObj.postergriduri=webpage
   m.myObj.functionName="getcontent"
   m.myObj.control = "RUN"
   return m.myObj.postergridcontent  <<======== How do I return the content ??

end function

Edit: Post marked as solved
0 Kudos
15 Replies
norcaljohnny
Level 7

Re: Returning value from function with Task

What type of content is it? XML, JSON ?

I will wait around for another 15 mins or so and see if you respond.

In case I miss you here is one way and forgive me but I like to use a more standardized code if you will.
contentreader.xml
<?xml version = "1.0" encoding = "utf-8" ?>

<component name = "ContentReader" extends = "Task" >

  <interface>
    <field id = "contenturi" type = "uri" />
    <field id = "content" type = "node" />
  </interface>

  <script type = "text/brightscript" >

    <![CDATA[

    sub init()
      m.top.functionName = "getcontent"
    end sub

    sub getcontent()
      content = createObject("roSGNode", "ContentNode")
      contentxml = createObject("roXMLElement")
      readInternet = createObject("roUrlTransfer")
      readInternet.setUrl(m.top.contenturi)
      contentxml.parse(readInternet.GetToString())
      if contentxml.getName()="Content"
        for each item in contentxml.GetNamedElements("item")
          itemcontent = content.createChild("ContentNode")
          itemcontent.setFields(item.getAttributes())
          end for
          end if
      m.top.content = content
    end sub

    ]]>

  </script>

</component>

the.xml
<component name = "SimplePosterGrid" extends = "Scene" initialFocus = "postergrid" >
  <interface>
    <field id="itemContent" type="node" onChange="showpostergrid"/>
  </interface>
  <script type = "text/brightscript" >

    <![CDATA[

    sub init()
      m.postergrid = m.top.findNode("PosterGrid")
      m.readPosterGridTask = createObject("roSGNode", "ContentReader")
      m.readPosterGridTask.contenturi = "http://yourfilehere.xml"
      m.readPosterGridTask.observeField("content", "showpostergrid")
      m.readPosterGridTask.control = "RUN"
    end sub

    sub showpostergrid()
        m.postergrid.content = m.readPosterGridTask.content
       m.postergrid.setFocus(true)
        end sub

    ]]>

  </script>

  <children>
    <PosterGrid
      id = "PosterGrid"
      basePosterSize = "[ 375, 225 ]"
      numColumns = "4"
      numRows = "3"
      itemSpacing = "[ 50, 75 ]" />
  </children>

</component>

0 Kudos
haamro
Level 7

Re: Returning value from function with Task

it will be just plain text auth code I have to add to the videoURL before it plays, so was trying to write a function that returns auth code.
0 Kudos
joetesta
Level 10

Re: Returning value from function with Task

You probably want to set an observer on the task and break your function up into 2 pieces;  Instead of this
    sub getContent()
      readInternet = createObject("roUrlTransfer")
      readInternet.setUrl(m.top.postergriduri)
      postergridcontent = readInternet.GetToString()
      m.top.postergridcontent = postergridcontent
   end sub


Do something like this:
    sub getContent()
      m.readInternet = createObject("roUrlTransferTask")
     m.readInternet.observeField("content","contentSet")
      m.readInternet.Url = m.top.postergriduri
   end sub

   sub contentSet
      m.top.postergridcontent = m.readInternet.content
   end sub


The task needs to have a top level field "content" that it sets once it has received and processed the server response.  As soon as the task sets "content" field (m.top.content from within the task), your brs will pick it up and assign to its own postergridcontent field.
aspiring
0 Kudos
haamro
Level 7

Re: Returning value from function with Task

I did what you recommended but got the following error. Also on your code, where is readinternet.getString() ?

Member function not found in BrightScript Component or interface. (runtime error &hf4) in pkg:/components/MainScene/task.xml(22)
022:        readInternet.observeField("content","contentSet")

New field id added
  <interface>
    <field id = "postergriduri" type = "string" />
    <field id = "postergridcontent" type = "string"/>
    <field id = "content" type = "string" />
  </interface>
0 Kudos
joetesta
Level 10

Re: Returning value from function with Task

 Also on your code, where is readinternet.getString() ?
That belongs in the task, not in the render thread.
As far as that error, you'd have to include more of your code.  It works on my Roku Smiley Very Happy
aspiring
0 Kudos
haamro
Level 7

Re: Returning value from function with Task

Thank you. Sorry I think I need more help here.  Here is my code. 
getContent() is in task, and I still don't get it why .getString() is not there. Also not sure where is  "roURLtransferTask " object from on your code.  Sorry I am a noob Smiley Happy

Task.xml
<?xml version="1.0" encoding="utf-8" ?>
 
<component name = "postergridCR" extends = "Task" >
 
<interface>
    <field id = "postergriduri" type = "string" />
    <field id = "postergridcontent" type = "string"/>
    <field id = "content" type = "string" />
</interface>
 
  <script type = "text/brightscript" >


    <![CDATA[
 
sub init()
    m.top.functionName = "getData"
end sub
 
 sub getContent()
      m.readInternet = createObject("roUrlTransfer")
      m.readInternet.observeField("content","contentSet")
      m.readInternet.Url = m.top.postergriduri
end sub

sub contentSet()
      m.top.postergridcontent = m.readInternet.content
end sub

   
   
    ]]>

  </script>
</component>


scenegraph.brs
getwebcontent ("http://example.com")

function getwebcontent(webpage as string)

   m.myObj = createObject("RoSGNode", "postergridCR")
   m.myObj.postergriduri=webpage
   m.myObj.functionName="getcontent"
   m.myObj.control = "RUN"
   return m.myObj.postergridcontent

end function
0 Kudos
joetesta
Level 10

Re: Returning value from function with Task

Think I goofed you up by giving you render thread code mixed in with your task code, and now you put the render thread code into the task.
This stuff belongs in your page brs (not task brs) the task brs will have the getString you're asking about.

function getwebcontent(webpage as string)
   m.myObj = createObject("RoSGNode", "postergridCR")
   m.myObj.postergriduri=webpage
   m.myObj.functionName="getcontent"
  m.myObj.observeField("postergridcontent","gotContent")   '<===================== CREATE OBSERVER BEFORE RUN'
   m.myObj.control = "RUN"
end function

sub gotContent()
   content = m.myObj.postergridcontent   '<==================  callback triggered when task sets m.top.postergridcontent'
end sub
aspiring
0 Kudos
haamro
Level 7

Re: Returning value from function with Task

Thank you very much, I am almost there but something is not working. gotContent() works and m.top.content value is set.

sub gotContent()
    m.top.content = m.haamro.HTTPcontent
   print m.top.content <======== This works
end sub


However, when I use getWebcontent function from other function/sub, m.top.content is set invalid. Is it because of the time it is taking to load the content ?

function myotherfunction(url)
getWebContent (url)
print m.top.content  <======= this does not work
end function
0 Kudos
joetesta
Level 10

Re: Returning value from function with Task

Although you say it works, I'm not certain you're doing it right without seeing the rest of the code. this wasn't mentioned earlier;  Is m.haamro your task and inside that task it's setting m.top.HTTPcontent ? 
m.haamro.HTTPcontent


In the last case where it isn't working, it's because you are trying to look at m.top.content immediately instead of waiting for the content to be set in the "gotContent" callback.
aspiring
0 Kudos