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: 
_creare_
Channel Surfer

While true loop keeps crashing roku

Jump to solution

 

 

 

When I run the below code my roku has a black screen and eventually crashes.
I think the issue is the while true loop but I haven't figured out how to have a wait or sleep

I found sleep() but it did not do anything to stop the crash
Neither did a thing with wait(0,port) (I may have implemented it wrong)

 

 

Here are all my files, their names and locations are designated by "FILE pkg:/*****/****.***"

File pkg:/components/Pong.brs

 
    Function init() 'Function is run when XML is parsed
        m.top.backgroundColor = "0xabc123FF" 'Set background color to black
        m.top.backgroundURI = "" 'Set background URI (image) to empty so background color displays
        
        m.Ball = m.top.findNode("ball"'Sets pointer to TopLabel node to adjust fields
        m.Ball.blendColor="0xFFFFFFFF"

        ballXPos = CreateObject("roFloat")
        ballXPos = 0
        bounceDirection = CreateObject("roInt")
        bounceDirection = 0

        while true
            if ballXPos > 1920
                bounceDirection = 1
            end if

            if ballXPos < 0
                bounceDirection = 0
            end if

            if bounceDirection = 0
                ballXPos = ballXPos + 50
            else
                ballXPos = ballXPos - 50
            end if
            m.Ball.translation = [ballXPos50]
        end while
        
    end Function
 
File pkg:/components/Pong.brs
 
<?xml version="1.0" encoding="UTF-8"?>
<component name="Pong" extends="Scene" >

<script type="text/brightscript" uri = "pkg:/components/Pong.brs"/>

    <children>
        <Label id="testLabel" 
            text="SCREENSAVER!"
            width="1920" 
            height="1080" 
            horizAlign="center"
            vertAlign="center"
            font="font:LargeBoldSystemFont"
        />
    <Poster
        id="ball"
        uri="pkg:/images/blackcircle.png"
        width="400"
        height="400"
        translation="[0,0]"
    />
    
    </children>
</component>
 
FILE pkg:/source/main.brs
 
sub Main()
    
    screen = createObject("roSGScreen"'Creates screen to display screensaver
    port = createObject("roMessagePort"'Port to listen to events on screen
    screen.setMessagePort(port)

    scene = screen.createScene("Pong"'Creates scene to display on screen. Scene name (AnimatedScreensaver) must match ID of XML Scene Component
    screen.show()

    while(true'Uses message port to listen if channel is closed
        msg = wait(0port)
        if (msg <> invalid)
            msgType = type(msg)
            if msgType = "roSGScreenEvent"
                if msg.isScreenClosed() then return
            end if
        end if
    end while
end sub
0 Kudos
1 Solution

Accepted Solutions
sanity-check
Roku Guru

Re: While true loop keeps crashing roku

Jump to solution

Yeah, you can't do stuff like that on the UI thread - i.e. not in any visual component.

https://developer.roku.com/en-gb/docs/developer-program/core-concepts/threads.md

You could create a 'Task' node which updates its ballXPos field on an interval, and have the UI code watch that field and update the x position when it changes.

Or if you don't want to bother with tasks, you can use a Timer node set to repeat and run your game loop code in that:

https://developer.roku.com/en-gb/docs/references/scenegraph/control-nodes/timer.md

View solution in original post

2 REPLIES 2
sanity-check
Roku Guru

Re: While true loop keeps crashing roku

Jump to solution

Yeah, you can't do stuff like that on the UI thread - i.e. not in any visual component.

https://developer.roku.com/en-gb/docs/developer-program/core-concepts/threads.md

You could create a 'Task' node which updates its ballXPos field on an interval, and have the UI code watch that field and update the x position when it changes.

Or if you don't want to bother with tasks, you can use a Timer node set to repeat and run your game loop code in that:

https://developer.roku.com/en-gb/docs/references/scenegraph/control-nodes/timer.md

_creare_
Channel Surfer

Re: While true loop keeps crashing roku

Jump to solution

Thank you! This works really well!

A few things about their example for others who might find this thread :
Make sure to change the "extends" thing from "Group" to "Scene"
And you can also set the timer to really low values like 0.01

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.