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: 
greubel
Visitor

Brightscript parameter reference

Is it possible to pass a parameter by reference instead of by value ? According to the documentation, arrays types are passed by reference but is there a way to pass the address of a value into a subroutine ?
0 Kudos
7 REPLIES 7
belltown
Roku Guru

Re: Brightscript parameter reference

You can make the value an array element and pass the array to your function. To access the value you would use value [0]:


value = [12345]

callByValue (value)

print value [0]

sub callByValue (value as object)
value [0] = 54321
end sub
0 Kudos
TheEndless
Channel Surfer

Re: Brightscript parameter reference

I'd probably go with an associative array wrapper (e.g., refParam = { Value: valueToPassByRef }), similar to what belltown suggested, but as an alternative for intrinsic types, if you box them first (or explicitly CreateObject them) and specify the function parameter "as Object", you can use the setter to reset the value.

Sub Main()
myInt = 1
myInt = Box(myInt)
print "Before:"; myInt
ResetInteger(myInt)
print "After:"; myInt
End Sub

Sub ResetInteger(input As Object)
input.SetInt(10)
End Sub
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
RokuJoel
Binge Watcher

Re: Brightscript parameter reference

On a related note, the performance issues that were seen with regards to Associative Array access should be fixed or at least significantly improved in the latest 4.8 release.

- Joel
0 Kudos
greubel
Visitor

Re: Brightscript parameter reference

Thanks ! This appears to work.

Sub test()
a = 1
a = box(a)
b = "Now is the time"
b = box(b)
? "a = " Type(a) " val = " a
? "b = " Type(b) " val = " b
chk( [9,a,b,0] )
? "a = " Type(a) " val = " a
? "b = " Type(b) " val = " b
end
End Sub

Sub chk( in as dynamic )
? "in = " type(in)
? in
in[1].SetInt(2)
in[2].SetString("The quick brown fox")
End sub
-----------------------------------------
a = roInt val = 1
b = roString val = Now is the time
in = roArray
9
1
Now is the time
0

a = roInt val = 2
b = roString val = The quick brown fox
0 Kudos
MazeWizzard
Visitor

Re: Brightscript parameter reference

"RokuJoel" wrote:
On a related note, the performance issues that were seen with regards to Associative Array access should be fixed or at least significantly improved in the latest 4.8 release.

- Joel


Not to hijack the thread or anything...glad you got the byvalue/byreference thing working OP. But this statement is monumental. I guess I'll have to pick up my Roku development again, since performance of AA's (and objects in AA's) was a bottleneck that caused me to stop. At least I should do some benchmarking/profiling again.

Wish it had been in the release notes. I had all but given up and put it aside. Didn't bother. Now I missed a month. My bad. Should check occasionally anyway I guess.

Thanks for the info. 🙂
0 Kudos
kbenson
Visitor

Re: Brightscript parameter reference

Efficient dissemination of changes/fixes/features in new firmware releases has always been a problem. At least they have a wiki now, so maybe they can make some use of it to help this.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
RokuJoel
Binge Watcher

Re: Brightscript parameter reference

Yeah, in the release publication we just said "performance improvements". I checked with engineering and was told that the fixes they were working on for AA's are indeed in the most recent release, (@ktbenson you might want to check with your performance testing suite to measure how much if any, significant improvement there is).

- Joel
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.