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