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: 
leosantosw
Reel Rookie

Variable scope in brightscript

Jump to solution

MainScene file:

 

<?xml version="1.0" encoding="utf-8"?>
<component name="MainScene" extends="Scene">
    <script type="text/brightscript" uri="MainScene.brs" />
    <script type="text/brightscript" uri="pkg:/components/UI/ScreenStack.brs" />
    <script type="text/brightscript" uri="pkg:/components/UI/HomeScreen.brs" />
    <children>
        <Spinner />
    </children>
</component>

 

MainScene.brs: 

 

function init()
    m.top.backgroundUri = ""
    m.top.backgroundColor = "#0E1420"
   
    m.screenStack = []
    InitScreenStack()
    ShowHomeScreen()
end function
...

 

I have a HomeScreen component, when user click in "Channels" i want create component and add in ScreenStack
HomeScreen.brs:

 

...
...
sub updateFocus()
    for each key in m.borders
        m.borders[key].opacity = 0
    end for
    m.borders[m.currentFocus].opacity = 1
end sub

function onKeyEvent(key as String, press as Boolean) as Boolean
    if press then
        nextFocus = invalid

        if key = "OK" then
            ClickButton(m.currentFocus)
            return true
        end if
        ... 
        ...
        ...
        end if
        
        if nextFocus <> invalid then
            m.currentFocus = nextFocus
            m.buttons[nextFocus].setFocus(true)
            updateFocus()
            return true
        end if
    end if
    return false
end function

function ClickButton(button as String)
    print "ClickButton: "; button
    if button = "channels" then
        m.CatagoryChannels = CreateObject("roSGNode", "ChannelsCategory")
        ShowScreen(m.CatagoryChannels)
    end if
end function

 

ShowScreen(m.CatagoryChannels) don't exist in this scope.

How i can resolve it?

0 Kudos
1 Solution

Accepted Solutions
leosantosw
Reel Rookie

Re: Variable scope in brightscript

Jump to solution

Resolved using observed:

sub ShowHomeScreen()
    m.HomeScreen = CreateObject("roSGNode", "HomeScreen")
    m.HomeScreen.observeField("categorySelected", "onCategorySelected")
    m.top.appendChild(m.homeScreen)
    ShowScreen(m.HomeScreen)
end sub

sub onCategorySelected()
    if m.homeScreen.categorySelected = "channels"
        category = CreateObject("roSGNode", "ChannelsCategory")
        ShowScreen(category)
    end if
end sub

 

View solution in original post

0 Kudos
1 REPLY 1
leosantosw
Reel Rookie

Re: Variable scope in brightscript

Jump to solution

Resolved using observed:

sub ShowHomeScreen()
    m.HomeScreen = CreateObject("roSGNode", "HomeScreen")
    m.HomeScreen.observeField("categorySelected", "onCategorySelected")
    m.top.appendChild(m.homeScreen)
    ShowScreen(m.HomeScreen)
end sub

sub onCategorySelected()
    if m.homeScreen.categorySelected = "channels"
        category = CreateObject("roSGNode", "ChannelsCategory")
        ShowScreen(category)
    end if
end sub

 

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.