Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
johnmarsden
Level 7

Accessing global functions in SceneGraph components?

Hey there,

How do you access global functions from within Scene Graph *.brs files? I have a utility class that has things like toString(), getScreenHeight(), etc that I want to access. 
' source/utility/general.brs

function toString(var)
  return ...
end function



There's a component being included called Menu.xml, which reaches out to a Menu.brs class that has a "init()" function. I've tried things like 
m.global.toString("test")
m.top.toString("test")
m.toString("test")
toString("test")


But none of it works. What's the best approach for accessing global utility functions from within components?
Thanks!
0 Kudos
11 Replies
destruk
Level 10

Re: Accessing global functions in SceneGraph components?

Copying the function you need into the component/duplicating it works.
0 Kudos
tmat1075
Level 8

Re: Accessing global functions in SceneGraph components?

What I've been doing is add an extra "script" tag to each of the xml files. 

<?rokuml version="1.0" encoding="utf-8" ?>

<!-- a Task handling the playback loop of both content abd ad -->
<component name="PlayerTask" extends="Task">

  <script type="text/brightscript" uri="pkg:/components/PlayerTask.brs" />
  <script type="text/brightscript" uri="pkg:/components/Helpers/VideoHelpers.brs"/>

  <interface>
      <field id="video" type="node" />
  </interface>

</component> 
Roku Employee
Roku Employee

Re: Accessing global functions in SceneGraph components?

Currently you cannot share global/utility functions between different components. There have been multiple requests to make that possible, keep asking and hopefully that will happen.

The workaround in use is duplicating code - whether literally or including via <script .../>, which leads to the same code being compiled/baked separately in each component. Please beware that using this too liberally may notably slow your components or launch times.
0 Kudos
johnmarsden
Level 7

Re: Accessing global functions in SceneGraph components?

What's the cost of passing references from an instantiated object to each component? I have an instantiated class called "String" that i want to pass to components by reference. I'm assuming this wont be too damaging or resource consuming correct? Will BrightScript duplicate referenced objects if passed to a component even though they aren't re instantiated?
0 Kudos
Roku Employee
Roku Employee

Re: Accessing global functions in SceneGraph components?

"johnmarsden" wrote:
What's the cost of passing references from an instantiated object to each component? I have an instantiated class called "String" that i want to pass to components by reference. I'm assuming this wont be too damaging or resource consuming correct? Will BrightScript duplicate referenced objects if passed to a component even though they aren't re instantiated?

Not sure i understand the question. By "instantiated class" do you mean a roAssociativeArray object with values and functions in it? If so, please note that RSG currently does not support functions inside component fields. See a list of what is supported here https://sdkdocs.roku.com/display/sdkdoc ... Attributes
The unsupported elements - in this case functions - would just be silently stripped away.
0 Kudos
johnmarsden
Level 7

Re: Accessing global functions in SceneGraph components?

Ah yes, I'm seeing that now. Stripped away after passing them in Smiley Sad  That's unfortunate.
0 Kudos
Tyler_Smith
Level 7

Re: Accessing global functions in SceneGraph components?

Something we've been doing is storing a Utility component in the global scope and exposing our utilities via functional fields.

You can see how to add functional fields to a component here:
https://sdkdocs.roku.com/display/sdkdoc ... onalFields


Sample implementation/call:

m.global.addFields({
  Utilities: CreateObject("UtilityComponent")
})




m.global.Utilities.callFunc("myFunction", {})
Tyler Smith
Senior Developer, REDspace
redspace.com
0 Kudos
georgejecook
Level 7

Re: Accessing global functions in SceneGraph components?

@tylersmith - you sir, rock!

I had missed functional fields, and their wonderful promise until I saw this - THANKS.
George Cook
https://linkedin.com/in/georgejecook/
Roku developers slack group (http://tiny.cc/nrdf0y) : georgejecook

Contact me on roku developer slack group, or via pm to discuss consultancy/work opportunities/rooibos unit testing framework
0 Kudos
Tyler_Smith
Level 7

Re: Accessing global functions in SceneGraph components?

You're welcome Smiley Happy
Tyler Smith
Senior Developer, REDspace
redspace.com
0 Kudos