Balbant Singh
Balbant Singh

Reputation: 187

How to write global function in brightscript?

I am working for Roku Scene Graph application, I want to write global user defined function which can be use and call any where in my application. is any way? Please suggest.

Upvotes: 2

Views: 1228

Answers (3)

JSON Derulo
JSON Derulo

Reputation: 76

I recommend creating a base component from which all your other components will extend. Include the scripts containing the functions you want to use in that component.

You can create an AA and have each key correspond to a function. You cannot set this AA as a field on the Global node (or any node for that matter).

Upvotes: 0

U.Mitic
U.Mitic

Reputation: 764

A global function script must be included in every XML file were you want to use it.

Steps:

  1. Create a brs file in /source called globalFunction.brs (or any name you choose), and write all of the global functions there.

  2. In every XML file (custom Screen, layout element etc) include the script after the <interface/> and before the <children/> with:

<script type="text/brightscript" uri="pkg:/source/globalFunction.brs" />

Upvotes: 5

gregc
gregc

Reputation: 1

Depending on the size and purpose of that globalFunction.brs in the other answer, one could move all of the functionality into a so-called "long-running task" and use interface functions. From there use:

    m.referenceToTask.callFunc("myFunc", 
{param1 : parmvalue1, 
param2 : parmvalue2})

It adds ~2ms-ish to the time it takes to do the work. Maybe worth it, maybe not. YMMV :)

Upvotes: 0

Related Questions