BrightScript Language for Roku

28 / Jun / 2016 by Upasana Chauhan 0 comments

Today we will learn basics of BrightScript language.

What is BrightScript language
As the name suggests BrightScript language is a scripting language, the main purpose of its development was for Roku device. In our first blog we have already discussed what Roku is, Introduction and setup in Roku TV The BrightScript language is written in C, though it’s syntactically does not follow C, it resembles more like Python/Ruby/Lua/Basic. BrightScript program runs over its engine above the kernel layer in Roku.

Why it is important to learn
It is important to learn BrightScript language because Roku supports only this language for developing apps/channnels. BRS is not a case dependent language.

IDE for BrightScript language
There is one popular IDE for Roku, which is Eclipse. On there SDK documentation page you will find below mentioned content.

BrightScript Plugin Update
Roku has released a completely updated version of the Eclipse BrightScript Plugin. The new plugin required Eclipse 4.5 (Mars) as a minimum, DLTK 5, and Java 8. The update site URL for the plugin is now http://rokudev.roku.com/rokusdk. The previous version at http://rokudev.roku.com/updates is no longer supported. For complete details see this article on the Roku developer blog.

Basics of BrightScript language

1. Function
To start with any logic we need to create either a function or sub. The Sub is discussed below.
The syntax of function is

Function functionName(parameter1 as type1, parameter2 as type2) as type
	returns obj
End Function

Here, every case needed to be end by END tag. The parameter1, parameter2 are optional. Type could be String, Integer, Boolean, default, invalid depending on situation. Function returns any object based on type declared.

2. Sub
The only difference in function and sub is there is no return type.
The syntax of function is

Sub functionName(parameter1 as type1, parameter2 as type2)
End Sub

3. If/Else/End if
Conditional statement used in Roku, with the following syntax.

If(condition1) then
else if (condition2)
else
end if

This is a normal syntax used for if/else clause. THEN is not compulsary to mention, but its a good practice to write.

4. For
Looping statements used in Roku is FOR.
The syntax for FOR is,

For i = 0 to 10
	Print i
	i = i + 1
End For

5. Return
Return statement is used to return any value from function.

6. While
While in Roku has a significant inportance. For any screen to remain visible in Roku, while statment is used.

While (true)
	msg = wait (0, port)
	if type(msg) = “anyScreenEvent” then
		//do task
		//exit while
	end if
end while

Here, the control will remain in while section till the time Exit while is not called in any of the case. “anyScreenEvent” will be discussed in our next blog.

7. Type ( value )
It returns the type of the value, like, invalid, int, boolean, string, or any customized type created by developer.

8. Print
It prints any value on console.

Print anyValue

9. m.value (Global)
This is used to declare global values in BrightScript language. Values declared with m tag can be accessed inside function or outside brs class also.

10. invalid
This is a important type user in BrightScript langugae. For easy interpretation we can assume it as null. Any value with type invalid will crash your code if not handled well. Therefore it is important to apply cases like,

if(parameter  invalid)
	Print “hello I am not invalid”
End if

11. ParseJson
This function will parse a string formatted according to RFC4627 and return an equivalent Brightscript object. If the string is not syntactically correct, Invalid is returned.

Function ParseJson (jsonString As String) As Object
End Function

12. Wait
This function waits on Object that are “writable” (those that have a MessagePort interface).
Wait() returns the event object that was posted to the message port.
If timeout is zero, “wait” will wait forever.
Otherwise, Wait will return after timeout milliseconds if no messages are received. In this case, Wait returns a type “invalid”.

Function Wait (timeout As Integer, port As Object) As Object
End Function

This blog helps you in understanding basic coding standards used in BrightScript language. Using these we can build our first basic Roku App.

In next blog we will look into problems and solutions faced while implementing features like Ads, GA, Makefile, parse HTML in Roku.

FOUND THIS USEFUL? SHARE IT

Leave a comment -