One of the goals of this blog is to help developers who are new to BrightScript grasp the syntax and concepts of the language. To that end, today we present the most iconic of beginner programming projects, Hello World! We are actually going to write Hello World! three different ways and, in doing so, introduce you to three BrightScript components you can use in your own channels.
All BrightScript components are documented in the Component Reference document found in the Roku SDK. The SDK can be downloaded from our developer site. Developers new to the Roku platform will also want to read the Developer Guide and the Channel Packaging and Publishing documents. These documents explain how to put your Roku into development mode and how to side-load your channel for testing.
Variation 1 – roPosterScreen
At some point while using Roku, you’ve probably seen a blank screen that says “retreiving…” in the center. More often than not, when you see that message, you are looking at a roPosterScreen. An empty roPosterScreen will display that message by default, but you can change the text of that message programatically. Our first Hello World! uses roPosterScreen’s ShowMessage() function to change the message text on an empty roPosterScreen. Here’s how it’s done:
sub Main()
' create our screen
screen = CreateObject("roPosterScreen")
' setup a message port so we can receive event information
port = CreateObject("roMessagePort")
screen.SetMessagePort(port)
' change the screen's message text
screen.ShowMessage("Hello World!")
screen.Show()
' start our event loop
while true
msg = Wait(0, port) ' wait for an event
if type(msg) = "roPosterScreenEvent"
' we got a poster screen event
if msg.isScreenClosed()
' the user closed the screen
exit while
end if
end if
end while
screen.Close()
' any time all screens in a channel are closed, the channel will exit
end sub
Variation 2 – roOneLineDialog
roOneLineDialog is an SDK component that displays a dialog with a single line of text and an optional busy animation. Typically, you would use roOneLineDialog to inform the user that your channel is busy performing some task and they should wait for that task to complete before continuing. Here’s Hello World! complete with busy animation:
sub Main()
' create and display a roPosterScreen as a backdrop
screen = CreateObject("roPosterScreen")
port = CreateObject("roMessagePort")
screen.SetMessagePort(port)
screen.Show()
' create our roOneLineDialog
dialog = CreateObject("roOneLineDialog")
' set the text of the dialog
dialog.SetTitle("Hello World!")
dialog.ShowBusyAnimation()
' display the dialog
dialog.Show()
' pretend we're doing something important
Sleep(10000)
' close the dialog
dialog.Close()
' event loop for our roPosterScreen
while true
msg = wait(0, port) ' wait for an event
' make sure the message we got is of the type we are expecting
if type(msg) = "roPosterScreenEvent"
if msg.isScreenClosed()
' the user closed the screen, exit the while loop
exit while
end if
end if
end while
screen.Close()
' anytime all screens within a channel are closed, the channel will exit
end sub
Variation 3 – roMessageDialog
roMessageDialog displays a message to the user and can also solicit feedback from them via a collection of buttons. Our final Hello World! will use a roMessageDialog with two buttons to solicit input from the user and then use a roPosterScreen and its ShowMessage() function to confirm to the user that we received that input:
sub Main()
' create and display a roPosterScreen as a backdrop
screen = CreateObject("roPosterScreen")
port = CreateObject("roMessagePort")
screen.SetMessagePort(port)
screen.Show()
' create our roMessageDialog
dialog = CreateObject("roMessageDialog")
' give the dialog the same message port as the poster screen so we can get events from both
dialog.SetMessagePort(port)
' set the text of the dialog
dialog.SetTitle("Hello World!")
' add some buttons
' assign them different indexes so we can tell them apart
' when we handle their respective isButtonPressed() events
dialog.AddButton(1, "Hello!")
dialog.AddButton(2, "Goodbye!")
dialog.SetMenuTopLeft(true)
' display the dialog
dialog.Show()
' event loop
while true
msg = wait(0, port) ' wait for an event
' make sure the message we got is of the type we are expecting
if type(msg) = "roMessageDialogEvent"
if msg.isButtonPressed()
' the user pressed a button on the roMessageDialog
' the index of the button assigned in the AddButton() function
' will correspond to the value returned by the event's GetIndex() function
buttonIndex = msg.GetIndex()
if buttonIndex = 1
' the user pressed the "Hello!" button
screen.ShowMessage("You pressed Hello!")
else
' the user pressed the "Goodbye!" button
screen.ShowMessage("You pressed Goodbye!")
end if
dialog.Close()
end if
else if type(msg) = "roPosterScreenEvent"
if msg.isScreenClosed()
' the user closed the screen, exit the while loop
exit while
end if
end if
end while
screen.Close()
' anytime all screens within a channel are closed, the channel will exit
end sub
There you have it. Three different implementations of Hello World! using three different SDK components that you can use in your own channels.