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

How to use Brightscript codes from 'source' folder for Scene Graph

Hello, sorry if the title might be misleading, as I don't know how to phrase it in regards to my problem

I just started recently onto Roku dev and I have made a file in 'source/rokuPlayer.brs', which contains the code below, which basically a wrapper of the roVideoScreen
[size=85][font=Monaco][color=#3629d2][/color][/font][/size]
function createRokuVideoPlayer(rokuPlaybackItem as Object) as Object
    obj = {
        rokuPlaybackItem : rokuPlaybackItem        
        playerName : "RokuVideoPlayer"  
        port : invalid
        player : invalid
        prepare : function() as Boolean
            m.initializePlayerPrepare()
            return true
        end function
        
        play : function() as Boolean
            m.player.resume()
            return true
        end function
        
        pause : function() as Boolean
            m.player.pause()
            return true
        end function
        
        close : sub() 
            m.initiatePlayerShutdown()
        end sub
        
        seek : function(millisecond as Integer) as Boolean
            m.player.seek(milliseconds)
            return true
        end function
        
        initializePlayerPrepare : sub()
            m.logger.logDebug(LINE_NUM, "Preparing Player ...")
            m.completePlayerPrepare()   
        end sub
        
        completePlayerPrepare : sub()
            m.port = createObject("roMessagePort")
            m.player = createObject("roVideoScreen")    
            m.player.setMessagePort(m.port)
            m.player.setPositionNotificationPeriod(1)
            m.player.setContent(m.rokuPlaybackItem)
            m.displayPlayer()            
        end sub
        
        initiatePlayerShutdown: sub()
            'TODO: do cleanup and shutdown of player
            m.player.close()
        end sub
        
        displayPlayer : sub()
            m.player.show()        
            while true
                msg = wait(1, m.port)        
                if type(msg) = "roVideoScreenEvent" then
                    print "showHomeScreen | msg = "; msg.getMessage() " | index = "; msg.GetIndex()
                    if msg.isScreenClosed()
                        print "Screen closed"
                        exit while
                    else       
                        print "Unexpected event type: "; msg.GetType()
                    end if
                else
                    print "Unexpected message class: "; type(msg)
                end if
            end while        
        end sub
    }
end function


Once that's done, I then create an Scene Graph XML file on 'components/Homescreen.xml', that contains this code, which is a UI screen composed of a list of items that when selected, should show up the roku player

<?xml version="1.0" encoding="UTF-8"?>

<component name="HomeScreen" extends="Scene" initialFocus = "builtinList">

   <interface>
        <!-- Specifies the content for the GridPannel -->
        <field id="rokuContents" type="array" onChange="onRokuContentsUpdated"/>        
        <field id="rowItemSelected" type="int" alwaysnotify="true" alias="builtinList.itemSelected"/>
    </interface>

  <!-- main handler -->
  <script type="text/brightscript">
  
  <![CDATA[
sub init()      
 m.list = m.top.FindNode("builtinList")  
 m.top.observeField("rowItemSelected", "OnItemSelected")
 
 m.content = createObject("RoSGNode","ContentNode")
 addSection("BuiltIn Content")
 addItem("Loading contents")
 
 m.list.content = m.content
 
 m.top.setFocus(false)
end sub
 
sub addSection(sectiontext as string)
 m.sectionContent = m.content.createChild("ContentNode")
 m.sectionContent.CONTENTTYPE = "SECTION"
 m.sectionContent.TITLE = sectiontext
end sub
 
sub addItem(itemtext as string)
 item = m.sectionContent.createChild("ContentNode")
 item.title = itemtext
end sub

' Row item selected handler
Sub OnItemSelected()
   ? "[HomeScreen] OnRowItemSelected"
   if m.top.rokuContents <> invalid
       focusedIndex = m.list.itemFocused
       rokuItem = m.top.rokuContents[focusedIndex]       
       ? "Selected Index " + focusedIndex.toStr()       
   rokuPlayer = createRokuVideoPlayer(rokuItem)
   rokuPlayer.prepare()       
   end if
End Sub

' If jsonContent is set, focus on the LabelList
sub onRokuContentsUpdated()
? "onJsonContentUpdated"
  m.content = createObject("RoSGNode","ContentNode")
  addSection("BuiltIn Content")
   if m.top.rokuContents = invalid   
  addItem("No Built-in Contents")  
   else
    for each rokuItem in m.top.rokuContents
    addItem(rokuItem.title)
    end for   
   end if
  
  m.list.content = m.content
   m.list.setFocus(true)
end sub
    ]]>

  </script>
  
  
  <children> 
    <LabelList
      id = "builtinList"
      textHorizAlign="left" 
      numRows="10"  
      itemSize = "[700,48]"   
   translation = "[160,92]" 
      /> 
  </children>
</component>
[color=#4e9192][size=85][font=Monaco][color=#009193][/color][/font][/size][/color]


However, when I compiled this onto Roku, I got this error when I selected an item
Function Call Operator ( ) attempted on non-function. (runtime error &he0) in pkg:/components/HomeScreen.xml(46)
[color=#000000][size=85][font=Menlo]046:            rokuPlayer = createRokuVideoPlayer(rokuItem)[/font][/size][/color]
Backtrace:


I was under the impression that the global function I made should be applicable on the scene graph but I was wrong.  I did some research and I found this link and this link. It talks about registering this 'Global scope' from a component script to a non-component script but its not the other way around, so those did not fix my problem.

I'm not looking for a workaround or code conversion her, such as converting the video player to use the video node in the scene graph. I'm just asking if it is possible to use any codes I made over the 'source' folder onto the XML files for Scene Graph
0 Kudos
3 REPLIES 3
joetesta
Roku Guru

Re: How to use Brightscript codes from 'source' folder for Scene Graph

Yes.  You'll want to do something like this in your XML file:


<script type="text/brightscript" uri="pkg:/source/rokuPlayer.brs" />
aspiring
0 Kudos
pmpascua
Visitor

Re: How to use Brightscript codes from 'source' folder for Scene Graph

"joetesta" wrote:
Yes.  You'll want to do something like this in your XML file:


<script type="text/brightscript" uri="pkg:/source/rokuPlayer.brs" />


I have already tried this and yet I'm still getting the same error as the brightscript code in it is within the Scene Graph XML scope
0 Kudos
pmpascua
Visitor

Re: How to use Brightscript codes from 'source' folder for Scene Graph

I have found the workaround based on the sample app from the Roku Ads framework. Basically, you have to implement the listeners outside of the XML scene graph instead of inside, usually on the main.brs file, to be able to use the codes designed onto the source folder. I don't really like this approach but I don't have much of a choice here 
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.