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.
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.
Thank you Endless.
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?)
“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 FUNCTIONThat 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.
If you issue the stats command in the debugger, it’ll give you the current counts and the limits.
“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:
“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 whileYou 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
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? ![]()
“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 whileyou 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?
Wow, I didn’t realise a manifest with only whitespace is legal ![]()
Wait a second, does that say you have over 2800 functions?!?! :shock:
:shock:
I thought I had a lot with a little over 100!
Thanks for the tests and info. TheEndless, what is the stats command? When I type “help” and see the different commands, nothing looks like stats. I tried “bscs” (which tells me I have almost 14,000 roArray components among other things!), and I tried other things.
“Komag” wrote:
Wait a second, does that say you have over 2800 functions?!?! :shock::shock:
I thought I had a lot with a little over 100!
Thanks for the tests and info. TheEndless, what is the stats command? When I type “help” and see the different commands, nothing looks like stats. I tried “bscs” (which tells me I have almost 14,000 roArray components among other things!), and I tried other things.
Actually it’s closer to 3500
That command only counts top-level functions; it won’t include functions defined in-line like this:
function main() as Void
data = loadDataList()
applyTransformation(data, function(a) as Void
if type(a.date) <> invalid
a.date = dateCreateWithString(a.date)
end if
end function)
end function
What’s your program, what do you do? :?:
“Komag” wrote:
Is this confirmed in anyone’s experience, that it’s per function, and that it’s 255?
All variables in BrightScript are local, i.e. “per function”. The reason why locals are fast and why 8-bit number limit would have been easier to explain if “they” haven’t disabled the disassembler command.
“Rek” wrote:
The limit appears to be 253 on my Roku 2. The following snippet works fine, until adding one more variable (“x253 = 253”) …
+1 for the “m” local var (which you are granted regardless) = 254
“Komag” wrote:
TheEndless, what is the stats command? When I type “help” and see the different commands, nothing looks like stats.
Wow.. it looks like they got rid of the stats command in a recent firmware update. It used to list a count of all variable types and their limits, including functions. Wonder why they ditched it… :?:
“Rek” wrote:
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 FUNCTIONThat way Main() is only adding one more variable, an AA with all the sounds.
(Would this be the right approach?)
It’s acceptable. And with small modification, shortage of variables averted:
function loadSounds():
sounds = { }
for each name in ["hitMon", "missMon", "hitPlayer", "missPlayer", "deathMon"]:
sounds[name] = CreateObject("roAudioResource", "pkg:/sounds/" + name + ".wav")
end for
return sounds
end function
“TheEndless” wrote:
Wow.. it looks like they got rid of the stats command in a recent firmware update. It used to list a count of all variable types and their limits, including functions. Wonder why they ditched it… :?:
“Catch-22” wrote:
“Why not?” was Chief White Halfoat’s answer.
“squirreltown” wrote:
you don’t even have to wrap it in Sub Main()/End sub
Wait… WAT?!
How is that going to work, B/S does not allow executable code outside of a function. Did i miss some change in the language?
“EnTerr” wrote:
And with small modification, shortage of variables averted:function loadSounds(): sounds = { } for each name in ["hitMon", "missMon", "hitPlayer", "missPlayer", "deathMon"]: sounds[name] = CreateObject("roAudioResource", "pkg:/sounds/" + name + ".wav") end for return sounds end function
(slaps forehead) Of course! I’ve tried to train myself to search and destroy repeating code, but I missed it this time! I’ll use that, thanks.
“EnTerr” wrote:
“TheEndless” wrote:
Wow.. it looks like they got rid of the stats command in a recent firmware update. It used to list a count of all variable types and their limits, including functions. Wonder why they ditched it… :?:
“Catch-22” wrote:
“Why not?” was Chief White Halfoat’s answer.
My understanding is that the “stats” command along with the legacy firmware updaters was traded for a load of egyptian cotton. Cheer up Endless! you’ve got shares in the syndicate!
