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: 
slingxshot
Level 7

Passing Data from main.brs

Does anyone know how to pass data from main.brs to the scene components?

I tried m.global and got an error.

Thanks!
0 Kudos
6 Replies
squirreltown
Level 8

Re: Passing Data from main.brs

m.global = screen.getGlobalNode()
  m.global.AddField("token", "string", false)
  m.global.token= "thisstring"


Put this in Main.brs and it should be available everywhere.
Kinetics Screensavers
0 Kudos
slingxshot
Level 7

Re: Passing Data from main.brs

awesome. I didn't realize that you need to associate it with the screen.
0 Kudos
ib_beat
Level 7

Re: Passing Data from main.brs

I stumbled across the same issue, but I couldn't make it work.
This is what I put in the main.brs
sub RunUserInterface() 

    user_id = regread("id","user_data")
    user_sub = regread("sub","user_data")
    ?user_id
    ?user_sub
    
    m.global = screen.getGlobalNode()
    m.global.AddField("userId", "string", false)
    m.global.AddField("subscription", "string", false)
    m.global.userId = user_id
    m.global.subscription = user_sub
    ? "User sub global = " + m.global.userId
    
    if user_id <> invalid then
       ShowContens(invalid)
    else
        ShowLogin(invalid)
    end if    
    
end sub


And I got this error here in the console:
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.


Current Function:
004:  sub RunUserInterface()
005:  
006:      user_id = regread("id","user_data")
007:      user_sub = regread("sub","user_data")
008:      ?user_id
009:      ?user_sub
010:      
011:*     m.global = screen.getGlobalNode()
012:      m.global.AddField("userId", "string", false)
013:      m.global.AddField("subscription", "string", false)
014:      m.global.userId = user_id
015:      m.global.subscription = user_sub
'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in pkg:/source/main.brs(11)
011:     m.global = screen.getGlobalNode()
Backtrace:
#0  Function runuserinterface() As Void
   file/line: pkg:/source/main.brs(11)
Local Variables:
global           Interface:ifGlobal
m                roAssociativeArray refcnt=2 count:0
user_id          roString (2.1 was String) refcnt=1 val:"test@example.com"
user_sub         roString (2.1 was String) refcnt=1 val:"False"
screen           <uninitialized>

Brightscript Debugger>

Any ideas?
0 Kudos
destruk
Level 10

Re: Passing Data from main.brs

If "screen" doesn't exist then you can't access it -- create the scene first, then create a screen, and there you go.
0 Kudos
joetesta
Level 10

Re: Passing Data from main.brs

or instead of using global, add the field to your component's interface and you can pass data to the component into that field. 

In component

<interface>
<field id="YourNewField" type="assocarray" onChange="myFunctionThatDoesSomething"/>
</interface>


choose "type" that makes sense.  Then in main,

comp = m.top.findnode("componentID")  (or probably you already have a handle to it)
comp.YourNewField = Data of the correct type Passed in to new field and seen by component via m.top.YourNewField


 As I understand this is less "expensive" and if you don't need the data globally, is the best practice.  Although I could be wrong about the expense...?
aspiring
0 Kudos
ib_beat
Level 7

Re: Passing Data from main.brs

Oh god... I missed that completely. I created the scene first and it solved the issue.
It now works properly.

Thanks!!
0 Kudos