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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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
function createRokuVideoPlayer(rokuPlaybackItem as Object) as Object
    obj = {
        rokuPlaybackItem : rokuPlaybackItem        
        playerName : "RokuVideoPlayer"  
        port : invalid
        player : invalid
        prepare : function() as Boolean
            return true
        end function
        play : function() as Boolean
            return true
        end function
        pause : function() as Boolean
            return true
        end function
        close : sub() 
        end sub
        seek : function(millisecond as Integer) as Boolean
            return true
        end function
        initializePlayerPrepare : sub()
            m.logger.logDebug(LINE_NUM, "Preparing Player ...")
        end sub
        completePlayerPrepare : sub()
            m.port = createObject("roMessagePort")
            m.player = createObject("roVideoScreen")    
        end sub
        initiatePlayerShutdown: sub()
            'TODO: do cleanup and shutdown of player
        end sub
        displayPlayer : sub()
            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
                        print "Unexpected event type: "; msg.GetType()
                    end if
                    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">

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

  <!-- main handler -->
  <script type="text/brightscript">
sub init()      
 m.list ="builtinList")"rowItemSelected", "OnItemSelected")
 m.content = createObject("RoSGNode","ContentNode")
 addSection("BuiltIn Content")
 addItem("Loading contents")
 m.list.content = m.content
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 <> invalid
       focusedIndex = m.list.itemFocused
       rokuItem =[focusedIndex]       
       ? "Selected Index " + focusedIndex.toStr()       
   rokuPlayer = createRokuVideoPlayer(rokuItem)
   end if
End Sub

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

      id = "builtinList"
      itemSize = "[700,48]"   
   translation = "[160,92]" 

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]

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
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" />
0 Kudos

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

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.