{"id":36958,"date":"2016-06-28T16:42:29","date_gmt":"2016-06-28T11:12:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36958"},"modified":"2016-12-19T15:04:00","modified_gmt":"2016-12-19T09:34:00","slug":"brightscript-language-for-roku","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/brightscript-language-for-roku\/","title":{"rendered":"BrightScript Language for Roku"},"content":{"rendered":"<p>Today we will learn basics of BrightScript language.<\/p>\n<p><strong>What is BrightScript language<\/strong><br \/>\nAs 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, <a title=\"Roku Setup\" href=\"http:\/\/www.tothenew.com\/blog\/introduction-and-setup-in-roku-tv\/\" target=\"_blank\">Introduction and setup in Roku TV<\/a> The BrightScript language is written in C, though it&#8217;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.<\/p>\n<p><strong>Why it is important to learn <\/strong><br \/>\nIt is important to learn BrightScript language because Roku supports only this language for developing apps\/channnels. BRS is not a case dependent language.<\/p>\n<p><strong>IDE for BrightScript language<\/strong><br \/>\nThere is one popular IDE for Roku, which is Eclipse. On there SDK documentation page you will find below mentioned content.<\/p>\n<p><cite>BrightScript Plugin Update<br \/>\nRoku 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 . The previous version at  is no longer supported. For complete details see this article on the Roku developer blog.<\/cite><\/p>\n<p><strong>Basics of BrightScript language<\/strong><\/p>\n<p><strong>1. Function<\/strong><br \/>\nTo start with any logic we need to create either a function or sub. The Sub is discussed below.<br \/>\nThe syntax of function is<\/p>\n<p>[code]Function functionName(parameter1 as type1, parameter2 as type2) as type<br \/>\n\treturns obj<br \/>\nEnd Function[\/code]<\/p>\n<p>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.<\/p>\n<p><strong>2. Sub<\/strong><br \/>\nThe only difference in function and sub is there is no return type.<br \/>\nThe syntax of function is<\/p>\n<p>[code]Sub functionName(parameter1 as type1, parameter2 as type2)<br \/>\nEnd Sub[\/code]<\/p>\n<p><strong>3. If\/Else\/End if<\/strong><br \/>\nConditional statement used in Roku, with the following syntax.<\/p>\n<p>[code]If(condition1) then<br \/>\nelse if (condition2)<br \/>\nelse<br \/>\nend if[\/code]<\/p>\n<p>This is a normal syntax used for if\/else clause. THEN is not compulsary to mention, but its a good practice to write.<\/p>\n<p><strong>4. For<\/strong><br \/>\nLooping statements used in Roku is FOR.<br \/>\nThe syntax for FOR is,<\/p>\n<p>[code]For i = 0 to 10<br \/>\n\tPrint i<br \/>\n\ti = i + 1<br \/>\nEnd For[\/code]<\/p>\n<p><strong>5. Return<\/strong><br \/>\nReturn statement is used to return any value from function.<\/p>\n<p><strong>6. While<\/strong><br \/>\nWhile in Roku has a significant inportance. For any screen to remain visible in Roku, while statment is used.<\/p>\n<p>[code]While (true)<br \/>\n\tmsg = wait (0, port)<br \/>\n\tif type(msg) = \u201canyScreenEvent\u201d then<br \/>\n\t\t\/\/do task<br \/>\n\t\t\/\/exit while<br \/>\n\tend if<br \/>\nend while[\/code]<\/p>\n<p>Here, the control will remain in while section till the time Exit while is not called in any of the case. \u201canyScreenEvent\u201d will be discussed in our next blog.<\/p>\n<p><strong>7. Type ( value )<\/strong><br \/>\nIt returns the type of the value, like, invalid, int, boolean, string, or any customized type created by developer.<\/p>\n<p><strong>8. Print<\/strong><br \/>\nIt prints any value on console.<\/p>\n<p>[code]Print anyValue[\/code]<\/p>\n<p><strong>9. m.value (Global)<\/strong><br \/>\nThis is used to declare global values in BrightScript language. Values declared with m tag can be accessed inside function or outside brs class also.<\/p>\n<p><strong>10. invalid<\/strong><br \/>\nThis 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,<\/p>\n<p>[code]if(parameter  invalid)<br \/>\n\tPrint \u201chello I am not invalid\u201d<br \/>\nEnd if[\/code]<\/p>\n<p><strong>11. ParseJson<\/strong><br \/>\nThis 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.<\/p>\n<p>[code]Function ParseJson (jsonString As String) As Object<br \/>\nEnd Function[\/code]<\/p>\n<p><strong>12. Wait<\/strong><br \/>\nThis function waits on Object that are \u201cwritable\u201d (those that have a MessagePort interface).<br \/>\nWait() returns the event object that was posted to the message port.<br \/>\nIf timeout is zero, \u201cwait\u201d will wait forever.<br \/>\nOtherwise, Wait will return after timeout milliseconds if no messages are received. In this case, Wait returns a type \u201cinvalid\u201d.<\/p>\n<p>[code]Function Wait (timeout As Integer, port As Object) As Object<br \/>\nEnd Function[\/code]<\/p>\n<p>This blog helps you in understanding basic coding standards used in BrightScript language. Using these we can build our first basic Roku App.<\/p>\n<p>In next blog we will look into problems and solutions faced while implementing features like Ads, GA, Makefile, parse HTML in Roku.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":164,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":81},"categories":[3479,3477],"tags":[3655,3657,3656,3658,3644],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36958"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/164"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36958"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36958\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}