Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
tvjay
Channel Surfer

return variable not changing

I am trying to do a custom canvas. In doing so I want to present the user a back button. What I am having trouble with is returning a variable to tell the main function to clsoe. The variable in question is goBack. Basically. I define goBack as 0. I then track the users selection (0,1 or 2), pass it a function to make a decision to either play a video or close. If they chose the close button (selectedIndex 2) then change the variable goBack to be 1. However, it changes it in the subfunction (onOk) but inside showImageCanvas it is still 0.

function showImageCanvas(episodes, selectedEpisode, leftBread, rightBread)

episode = episodes[selectedEpisode]
goBack = 0

canvas = CreateObject("roImageCanvas")
port = CreateObject("roMessagePort")
posArray = GetPositions()
items = []
selectedIndex = 0

canvas.SetMessagePort(port)
canvasRect = canvas.GetCanvasRect()
'onOK(2)
'stop
items.Push({
url:"http://apache.303north.com/Roku/1280x165_overhang.png"
TargetRect: {x: 0, y: 0}
})
items.Push({
url:episode.hdposterurl
TargetRect: {x: 200, y: 200}
})
items.Push({
url:"pkg:/images/ptl-button-play.png"
TargetRect: posArray[0]
})
items.Push({
url:"pkg:/images/ptl-button-playall.png"
TargetRect: posArray[1]
})
items.Push({
url:"pkg:/images/ptl-button-back.png"
TargetRect: posArray[2]
})
items.Push({
Text:episode.description
TextAttrs:{Color:"#FFCCCCCC", Font:"Medium",
HAlign:"HCenter", VAlign:"VCenter",
Direction:"LeftToRight"}
TargetRect: {x: -50, y: 200}
})
ring = {
url: "pkg:/images/ptl-button-selection.png",
TargetRect: {x: posArray[selectedIndex].x-2, y: posArray[selectedIndex].y-2}
}
canvas.SetLayer(0, { Color: "#1d2535", CompositionMode: "Source" })
canvas.SetLayer(1, items)
canvas.SetLayer(2, ring)
canvas.Show()

while true
event = wait(0, port)
if (event<> invalid)
if (event.isRemoteKeyPressed())
index = event.GetIndex()
print selectedIndex
if (index = 4) OR (index = 2) 'Left or Up
selectedIndex = selectedIndex-1
if (selectedIndex < 0)
selectedIndex = 2
endif
else if (index = 5) OR (index = 3) 'Right or Down
selectedIndex = selectedIndex+1
if (selectedIndex > 2)
selectedIndex = 0
endif
else if (index = 6) 'OK
onOK(selectedIndex, episode, goBack)

print goBack
print "POST OK"

if goBack = 1
print "EXIT WHILE"
exit while
endif

print "END"

endif

ring.TargetRect = {x: posArray[selectedIndex].x-2, y: posArray[selectedIndex].y-2}
canvas.SetLayer(0, { Color: "#1d2535", CompositionMode: "Source" })
canvas.SetLayer(1, items)
canvas.SetLayer(2, ring)
endif
endif
end while

return selectedEpisode
End Function

function onOK(selectedIndex, episode, goBack)


screen = CreateObject("roVideoScreen")
screen.SetMessagePort(CreateObject("roMessagePort"))
screen.SetPositionNotificationPeriod(1)

screen.SetContent(episode)

print goBack

if selectedIndex = 0
print "Test 0"
episode.playStart = 0 ' Play
PlayVideo(episode)

else if selectedIndex = 1 ' Play All
print "Test 1"

PlayVideo(episode)
else if selectedIndex = 2 ' Back
print "Test 2"
goBack = 1
print goBack
end if

return goBack
end function

Function GetPositions() as object
posArray = []
posArray.Push({x: 500, y: 250})
posArray.Push({x: 500, y: 300})
posArray.Push({x: 500, y: 350})
return posArray
End Function
0 Kudos
7 REPLIES 7
TheEndless
Channel Surfer

Re: return variable not changing

Primitives are passed by value. You'll need to box them in order to pass them by reference, then call the appropriate SetXXX() method to update the value.

Something along the lines of (stupidly verbose to illustrate the steps)...
Sub Main()
x = 0
x = Box(x)
print "x=";x
UpdateX(x)
print "x=";x
End Sub

Sub UpdateX(x)
' You have to call SetInt() to update the value, otherwise
' you'll just be re-assigning the local variable
x.SetInt(2)
End Sub

Should output:
------ Running ------
x= 0
x= 2

If you skip the Box() step, you'll get what you're running into now...
------ Running ------
x= 0
x= 0


Alternatively, you could pass it in as part of an AA, which always get passed by reference.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
Komag
Roku Guru

Re: return variable not changing

Wow, that's very clear TheEndless. I discovered using AAs to always get referencing, but it's nice to know the exact method you outlined if only to have a better understanding of how it really works. 🙂
0 Kudos
RokuMarkn
Visitor

Re: return variable not changing

The more usual technique to return a single value from a function is to pass it as the return value. You already have a "return goBack" as the last statement in your onOK function, so instead of


onOK(selectedIndex, episode, goBack)


you could just call it as


goBack = onOK(selectedIndex, episode)


--Mark
0 Kudos
Komag
Roku Guru

Re: return variable not changing

Slightly messier if you need a few variables returned is to bundle them in an array and return that.
FUNCTION Main()
answerArray = getAnswers(input1, input2, input3)
PRINT answers[0] ' prints whatever answer1 is
END FUNCTION

FUNCTION getAnswers(input1, input2, input3) AS OBJECT
doThis()
doThat()
answer1 = input1.doSomething()
answer2 = input2.doOtherThing()
answer3 = answer1 + answer3.else()
answers = [answer1, answer2, answer3]
RETURN answers
END FUNCTION
0 Kudos
tvjay
Channel Surfer

Re: return variable not changing

Thank you guys for the great information and the fast response.

I did have two questions though...

What do you guys mean using AA's?

What happens if I want to pass text instead of a number. Is there a different SetXXX() setup? I have been searching the Roku SDK documentation but can't find anything.
0 Kudos
Komag
Roku Guru

Re: return variable not changing

The documentation has all this info, but its spread out a little.
Read this whole thing about 5 times:
http://sdkdocs.roku.com/display/sdkdoc/ ... +Reference
And go through "all" of these, especially things like roArray, roAssociativeArray (AA), roBoolean, roFloat, etc, all the stuff mentioned in the language reference.
Passing text is no different, just using "string" instead of "integer/float/double float"
0 Kudos
EnTerr
Roku Guru

Re: return variable not changing

"tvjay" wrote:
What do you guys mean using AA's? What happens if I want to pass text instead of a number.

meaning doing something like this, it's idiomatic:
function someFun()
...
it = {x: 10, y: 100, alpha: 0.5, visible: true, text: "something something"}
return it
end function

...
obj = someFun()
print "and the text is:", obj.text


Is there a different SetXXX() setup? I have been searching the Roku SDK documentation but can't find anything.

"SetXXX()" in particular, if existing - i am sure is undocumented for NSFW NC-17 reasons
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.