How to wait a function finishes and pass variable between files in Roku BrightScript

50 Views Asked by At

I'm building a Roku channel and I'm facing a problem where I need a function to finish in order to run the lines afterwards & also I cant figure how to access a variable between components, this is the flow I have:

file1.brs

MainFunction.observeField("itemSelected", "func1") -first wait for this to finish
MainFunction.observeField("itemSelected", "func2") -then run this

sub func2()
    print var1
end sub

file2.brs

sub func1()
    var1 = (get content from some database)
end sub
1

There are 1 best solutions below

0
TwitchBronBron On

Observers all fire independently of each other, so you will need to call func2 from inside func1.

Also, every roku component has its own copy of m, which is a global AA unique for each instance of the component. (similar to this in other languages). So you could share data between func1 and func1 like this:

file1.brs

MainFunction.observeField("itemSelected", "func1")

sub func2()
    print m.var1
end sub

file2.brs

sub func1()
    m.var1 = (get content from some database)
    func2()
end sub