sdpetersen
7 years agoVisitor
Passing a variable out of a function
I'm making an API call to gather associative arrays of content to display in my channel. I'm doing all this through a Task, but I'm running into trouble getting the variables back to the Main script for use.
In a nutshell, I have this function called from the Init of the navigation.brs file like so:
APICall() is a function that makes an API call that returns a dynamic list of content that should be loaded (it changes based on the user). loadMovieContent() is another function that makes another API call to get the content (lists of movies) I'm looking to store, based on the APICall() result. Both of these are working fine.
My problem is, I cannot access m.movies ( m.top.findNode(jsonItem.id) ) outside of this function. What I don't understand is if I make the assignment like this I can access it outside of the function without problems:
I've read bits in the Roku documentation that suggest that m.top.movieContent and m.top.findNode("movieContent") are synonymous, however I'm finding that's not true, at least not in terms of scope of the variable.
First, can you help me find a way of assigning dynamic variables that I can access outside of the function?
Next, I'd love to read up on the scope of variables, if someone has a link to read (I'm not impressed with what I'm finding on the Roku documentation site)
Finally, I'm a PHP dev that's making this up as I go, so if you see a better way of doing things, please, point me in the right direction.
Much appreciated!
In a nutshell, I have this function called from the Init of the navigation.brs file like so:
Sub Init()
m.navItems = m.top.findNode("navigation")
m.top.functionName = "loadNavContent"
End Sub
Sub loadNavContent()
oneRow = APICall("main-navigation")
movieArray = {}
for each jsonItem in oneRow
if jsonItem.DoesExist("api")
movieArray[jsonItem.id] = loadMovieContent(jsonItem.api)
m.movies = m.top.findNode(jsonItem.id)
m.movies = movieArray[jsonItem.id]
m.movies.id = jsonItem.id
end if
end for
End Sub
APICall() is a function that makes an API call that returns a dynamic list of content that should be loaded (it changes based on the user). loadMovieContent() is another function that makes another API call to get the content (lists of movies) I'm looking to store, based on the APICall() result. Both of these are working fine.
My problem is, I cannot access m.movies ( m.top.findNode(jsonItem.id) ) outside of this function. What I don't understand is if I make the assignment like this I can access it outside of the function without problems:
m.top.movieContent = movieArray[jsonItem.id]
I've read bits in the Roku documentation that suggest that m.top.movieContent and m.top.findNode("movieContent") are synonymous, however I'm finding that's not true, at least not in terms of scope of the variable.
First, can you help me find a way of assigning dynamic variables that I can access outside of the function?
Next, I'd love to read up on the scope of variables, if someone has a link to read (I'm not impressed with what I'm finding on the Roku documentation site)
Finally, I'm a PHP dev that's making this up as I go, so if you see a better way of doing things, please, point me in the right direction.
Much appreciated!