Your Digital Media Has Never Looked So Good

 
johnmarsden
Topic Author
Posts: 79
Joined: Wed Nov 18, 2015 1:37 pm

Accessing global functions in SceneGraph components?

Fri May 12, 2017 1:44 pm

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!
 
destruk
Posts: 2585
Joined: Sat Dec 18, 2010 4:58 pm

Re: Accessing global functions in SceneGraph components?

Fri May 12, 2017 7:06 pm

Copying the function you need into the component/duplicating it works.
 
tmat1075
Posts: 27
Joined: Mon Apr 17, 2017 9:35 am

Re: Accessing global functions in SceneGraph components?

Mon May 15, 2017 8:12 am

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> 
 
User avatar
RokuNB
Posts: 404
Joined: Fri Mar 31, 2017 2:22 pm

Re: Accessing global functions in SceneGraph components?

Mon May 15, 2017 10:18 am

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.
 
johnmarsden
Topic Author
Posts: 79
Joined: Wed Nov 18, 2015 1:37 pm

Re: Accessing global functions in SceneGraph components?

Mon May 15, 2017 2:55 pm

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?
 
User avatar
RokuNB
Posts: 404
Joined: Fri Mar 31, 2017 2:22 pm

Re: Accessing global functions in SceneGraph components?

Mon May 15, 2017 5:57 pm

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.
 
johnmarsden
Topic Author
Posts: 79
Joined: Wed Nov 18, 2015 1:37 pm

Re: Accessing global functions in SceneGraph components?

Tue May 16, 2017 4:03 pm

Ah yes, I'm seeing that now. Stripped away after passing them in :(  That's unfortunate.
 
User avatar
Tyler Smith
Posts: 118
Joined: Thu Apr 14, 2016 10:51 am
Location: Nova Scotia, Canada
Contact:

Re: Accessing global functions in SceneGraph components?

Fri Jul 07, 2017 10:06 pm

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
 
User avatar
georgejecook
Posts: 74
Joined: Thu Apr 05, 2018 2:17 pm

Re: Accessing global functions in SceneGraph components?

Wed Apr 11, 2018 3:46 pm

@tylersmith - you sir, rock!

I had missed functional fields, and their wonderful promise until I saw this - THANKS.
 
User avatar
Tyler Smith
Posts: 118
Joined: Thu Apr 14, 2016 10:51 am
Location: Nova Scotia, Canada
Contact:

Re: Accessing global functions in SceneGraph components?

Wed Apr 11, 2018 6:08 pm

You're welcome :)
Tyler Smith
Senior Developer, REDspace
redspace.com
 
tim_beynart
Posts: 224
Joined: Wed Jul 15, 2015 8:30 am

Re: Accessing global functions in SceneGraph components?

Wed Apr 11, 2018 7:17 pm

@tylersmith that’s awesome, thanks
 
User avatar
georgejecook
Posts: 74
Joined: Thu Apr 05, 2018 2:17 pm

Re: Accessing global functions in SceneGraph components?

Thu Apr 12, 2018 6:29 am

Hey @tylersmith - don't spose you've got an example of _any_ of your util methods.. just want to be sure I'm structuring things right.. i.e. what is your util class? xml node/task?

I'm trying to architect what I'm doing in a way that suits 7.6+ firmware - most all examples seem architecturally years old - I wish Roku woudl update their docs/apps to reflect their new provisions - God alone knows how many people are observing fields left right center haplessly, or how many people don't know that you can efficiently copy in .brs files to .xml since 7.5 - I think their own docs state that's a performance risk; yet the release notes for 7.5 clearly supersede that..

So many ways to scew up making an app..

sorry for rant - if you woulnd't, pleeasssse, mind just giving me a snippet with even just 1 util function to show how you're using it. I'd really be in your debt.

Thanks

Who is online

Users browsing this forum: No registered users and 2 guests