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: 
squirreltown
Roku Guru

Variable limit

Just bumped into the variable limit. Manged to do what i needed but I'm within 3-4 of it.
What is the limit, and is it global per channel or per .brs file or per function?
Thanks.
Kinetics Screensavers
0 Kudos
27 REPLIES 27
TheEndless
Channel Surfer

Re: Variable limit

In the debugger, you can type "stats" to get a list of all of the limits. Variables is 255, which I believe is per function, but I'm not positive.
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
squirreltown
Roku Guru

Re: Variable limit

Thank you Endless.
Kinetics Screensavers
0 Kudos
Komag
Roku Guru

Re: Variable limit

Is this confirmed in anyone's experience, that it's per function, and that it's 255?

So if I want to set a bunch of sound variables, I can just do something like this:

' within Main()
sounds = getSounds()

FUNCTION getSounds() ' trig by Main(1) near top
hitMon = CreateObject("roAudioResource", "pkg:/sounds/hitMon.wav")
missMon = CreateObject("roAudioResource", "pkg:/sounds/missMon.wav")
hitPlayer = CreateObject("roAudioResource", "pkg:/sounds/hitPlayer.wav")
missPlayer = CreateObject("roAudioResource", "pkg:/sounds/missPlayer.wav")
deathMon = CreateObject("roAudioResource", "pkg:/sounds/deathMon.wav")
sounds = {hitMon: hitMon, missMon: missMon, hitPlayer: hitPlayer, missPlayer: missPlayer, deathMon: deathMon} ' I don't think this is best way to do it
RETURN sounds
END FUNCTION

That way Main() is only adding one more variable, an AA with all the sounds.
(Would this be the right approach?)
0 Kudos
Rek
Visitor

Re: Variable limit

"Komag" wrote:
Is this confirmed in anyone's experience, that it's per function, and that it's 255?

So if I want to set a bunch of sound variables, I can just do something like this:

' within Main()
sounds = getSounds()

FUNCTION getSounds() ' trig by Main(1) near top
hitMon = CreateObject("roAudioResource", "pkg:/sounds/hitMon.wav")
missMon = CreateObject("roAudioResource", "pkg:/sounds/missMon.wav")
hitPlayer = CreateObject("roAudioResource", "pkg:/sounds/hitPlayer.wav")
missPlayer = CreateObject("roAudioResource", "pkg:/sounds/missPlayer.wav")
deathMon = CreateObject("roAudioResource", "pkg:/sounds/deathMon.wav")
sounds = {hitMon: hitMon, missMon: missMon, hitPlayer: hitPlayer, missPlayer: missPlayer, deathMon: deathMon} ' I don't think this is best way to do it
RETURN sounds
END FUNCTION

That way Main() is only adding one more variable, an AA with all the sounds.
(Would this be the right approach?)


The limit appears to be 253 on my Roku 2. The following snippet works fine, until adding one more variable ("x253 = 253"):


function main() as Void
x0 = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 4
x5 = 5
x6 = 6
x7 = 7
x8 = 8

' ... snip a couple hundred lines ...

x247 = 247
x248 = 248
x249 = 249
x250 = 250
x251 = 251
x252 = 252

?"Done"
end function


The limit also appears to be imposed on individual functions, not the .brs file or channel as a whole. I verified this by creating two functions: `a`, and `b` which also have 253 variables. This gives a total of 759 variables across 3 functions in 1 file.

The following code uses all 3 functions: `main` calls `a`, then `a` calls `b`. While `b` is executing, all 759 variables are in-memory, and it works fine.

function main() as Void
?"enter Main"
x0 = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 4
x5 = 5
x6 = 6
x7 = 7
x8 = 8
x9 = 9

' ... snip a couple hundred lines ...

x247 = 247
x248 = 248
x249 = 249
x250 = 250
x251 = 251
x252 = 252

a()
?"exit Main (done)"
end function

function a() as Void
?"enter A"
x0 = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 4
x5 = 5
x6 = 6
x7 = 7
x8 = 8
x9 = 9

' ... snip a couple hundred lines ...

x247 = 247
x248 = 248
x249 = 249
x250 = 250
x251 = 251
x252 = 252

b()
?"exit A"
end function

function b() as Void
?"enter B"
x0 = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 4
x5 = 5
x6 = 6
x7 = 7
x8 = 8
x9 = 9

' ... snip a couple hundred lines ...

x247 = 247
x248 = 248
x249 = 249
x250 = 250
x251 = 251
x252 = 252

?"exit B"
end function


As an aside: I think its safe to say that if you are actually hitting this limit, its probably time to start breaking the function up into smaller ones... In general having monolithic functions makes your code harder to maintain and understand.

My general rule is that if a function is more than a handful of lines (~ 10) it's too long and should be broken up if possible. Additionally, if the exact same lines of code appear in more than 3 places, its time to make a function that does the job. There are sometimes performance, or architectural reasons not to do so, but in general I find it to be a good way to operate.
0 Kudos
TheEndless
Channel Surfer

Re: Variable limit

If you issue the stats command in the debugger, it'll give you the current counts and the limits.
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
squirreltown
Roku Guru

Re: Variable limit

"Rek" wrote:

My general rule is that if a function is more than a handful of lines (~ 10) it's too long and should be broken up if possible.

10 ? thats a typo right?
You can't even do this in 10 lines.
				while true		
draw something
swapbuffers()
msg = port.getmessage()
if type(msg) = "roUniversalControlEvent"
button = msg.getint()
if button=0
return
end if
end if
end while

You must have a bizzilion functions. :shock:
Kinetics Screensavers
0 Kudos
Rek
Visitor

Re: Variable limit

"squirreltown" wrote:
"Rek" wrote:

My general rule is that if a function is more than a handful of lines (~ 10) it's too long and should be broken up if possible.

10 ? thats a typo right?
You can't even do this in 10 lines.
				while true		
draw something
swapbuffers()
msg = port.getmessage()
if type(msg) = "roUniversalControlEvent"
button = msg.getint()
if button=0
return
end if
end if
end while

You must have a bizzilion functions. :shock:


As I say, there are architectural reasons to have longer functions. I only have 1 run-loop in my application, so the logic is not duplicated anywhere; the function it lives in is around 120 lines 😞 The key here is that it performs a specific task, and is implemented as a bunch of calls to other functions (with comments!). So, even though its long, its still readable and maintainable.

But I do have quite a few...

$ grep -inR "^function" . | wc -l
2831
0 Kudos
squirreltown
Roku Guru

Re: Variable limit

Well I cant write a function that small but here is a whole channel in 5 lines:
   screen = CreateObject("roScreen", true,1280, 720)
while true
screen.clear(&hFF0000FF)
screen.swapbuffers()
end while
you don't even have to wrap it in Sub Main()/End sub
the mainfest file only needs a single space in it to be legal. So we'll call it six lines. How's that for efficient? Smiley LOL
Kinetics Screensavers
0 Kudos
Rek
Visitor

Re: Variable limit

"squirreltown" wrote:
Well I cant write a function that small but here is a whole channel in 5 lines:
   screen = CreateObject("roScreen", true,1280, 720)
while true
screen.clear(&hFF0000FF)
screen.swapbuffers()
end while
you don't even have to wrap it in Sub Main()/End sub
the mainfest file only needs a single space in it to be legal. So we'll call it six lines. How's that for efficient? Smiley LOL


Wow, I didn't realise a manifest with only whitespace is legal Smiley LOL
0 Kudos