Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
johnmarsden
Visitor

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 11
destruk
Binge Watcher

Re: Accessing global functions in SceneGraph components?

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

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> 
RokuNB
Roku Guru

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
Visitor

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
RokuNB
Roku Guru

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
Visitor

Re: Accessing global functions in SceneGraph components?

Ah yes, I'm seeing that now. Stripped away after passing them in 😞  That's unfortunate.
0 Kudos
Tyler_Smith
Binge Watcher

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
0 Kudos
georgejecook
Streaming Star

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://georgejecook.github.io/
https://linkedin.com/in/georgejecook/
Roku developers slack group (https://join.slack.com/t/rokudevelopers/shared_invite/zt-4vw7rg6v-NH46oY7hTktpRIBM_zGvwA) : georgejecook

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

Re: Accessing global functions in SceneGraph components?

You're welcome 🙂
Tyler Smith
0 Kudos