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: 
360tv
Level 7

Adding any given number of labels and/or posters during runtime

So I finally started to get my old channel converted to the new scenegraph, and I'm mostly understanding everything now, But the thing I was able to do with roScreen that I can't seem to figure out here is dynamically add and remove labels and posters to my scene based on info given to my channel by my server. It may call for me adding 1, 3 or lots of labels and/or posters of different sizes / fonts and locations. How can I add and remove nodes to a scene outside of XML?
0 Kudos
6 Replies
joetesta
Level 10

Re: Adding any given number of labels and/or posters during runtime

Generally you have a parent component that launches a task and observes the content (a node field on the task).  When the content updates, the parent component sets the content of some component (such as a rowlist or a some other component that takes a content field). Something like  m.rowlist.content = m.mytask.content
In this case the task will have received a response from the server (XML or Json, hopefully) then parse that response to turn it into an object that is a single content node with a bunch of child nodes.
make sense?

in the parent component's init() you'd have

sub init()
 'set rowlist component'
  m.rowlist = m.top.findnode("rowlistId") ' this assumes your XML has a rowlist with id = "rowlistId" '
 'setup task component'
  m.mytask = createObject("RoSGNode", "GetTask") ' this assumes you have a task component (xml,brs) with name = "GetTask" '
  m.mytask.observeField("content","onContentChange")
 ...
end sub


sub onContentChange()
  m.rowlist.content = m.mytask.content
end sub

then later in the parent init, or when the user presses a key, or some other observable event, you launch the task, first passing in any needed params through a field.
m.loadtask.yourField="some_param_value"
m.loadtask.control="RUN"
the GetTask will have a field named "content" and will do something like this with the parsed http response:

yourField = m.top.yourField
urlTransferObject = createObject("roUrlTransfer")
urlTransferObject.SetCertificatesFile("common:/certs/ca-bundle.crt")
urlTransferObject.setUrl(m.url + "?myParam=" + yourField)
response = ParseJson(urlTransferObject.GetToString())
content = createObject("RoSGNode", "ContentNode")
for each item in response
  child = content.createChild("Node") 'change node here to component type that matches the rowlists itemComponentName '
  child.id = item.id
  child.title = item.title
end for 
m.top.content = content ' this triggers the parent's observer`
aspiring
0 Kudos
360tv
Level 7

Re: Adding any given number of labels and/or posters during runtime

Thanks for the response. I read over your code a few times and I get what it's saying.I mostly understand passing data back and forth between and render node and a task node and how to use that to change existing properties of, say a label or poster. But I have a scene with a video node and a label node, but I want to add additional labels to a scene, not change existing ones. For example, one minute I'll need a image in the corner of the screen, and the next minute, I'll need One image in one corner, another in another corner, a label in one spot and three more labels somewhere else. If I'm reading your code right, that would allow me to change an existing label, but doesn't show me how to create multiple new labels (and remove them when I'm done with them).Unless I'm missing something.
0 Kudos
joetesta
Level 10

Re: Adding any given number of labels and/or posters during runtime

If those dynamic elements aren't coming from the server, then you don't need to use the separate task node.
You have some options, you can either
* Have all the elements pre-defined in your XML and just alter their visibility (item.visible = true /  item.visible = false) when you need to show / hide them.
* Create them ad hoc (for example,

m.newItem = createObject("RoSGNode", "Label")
xVal = 720
yVal = 500
m.newItem.translation = [xVal,yVal]


You can also combine these things with stuff coming from a task node to change titles or uri's of labels and poster.
aspiring
0 Kudos
360tv
Level 7

Re: Adding any given number of labels and/or posters during runtime

Great! "ad hoc" method I guess was what I wanted to ask about. So if I were to create an array of nodes, how can I add and remove them to a scene "ad hoc"?
0 Kudos
joetesta
Level 10

Re: Adding any given number of labels and/or posters during runtime

You could loop over the array and append them using "m.top.appendChild(newNode)" and remove using m.top.removeChild() 
see other related methods here: https://sdkdocs.roku.com/display/sdkdoc ... asroSGNode)
Or you could make them all children of a single node and just append / delete that one parent node to m.top as needed
aspiring
0 Kudos
360tv
Level 7

Re: Adding any given number of labels and/or posters during runtime

Thanks for the help! I think I understand everything I need to understand. Finally! 
0 Kudos