More than 200 local variables - WoWInterface
02-09-20, 05:41 PM   #1
Walkerbo
An Aku'mai Servant

Join Date: Oct 2010
Posts: 30
More than 200 local variables

Hi all

My addon stops working once I have more than 200 local variables, the error gives me the following message.
Code:
4x AAA\AAA 8.4.8.3.lua:5589: main function has more than 200 local variables
I understand that I can reduce the number of variables to keep under that 200 cap but I would like to know why such a cap exists?

Also I can define variables using
Code:
local a, b, c, d, e = 1, 2, 3, 4, 5
However this still counts as 5 local variables, is there a way to define local variables in a way that dose not increase the overall count of local variables?

02-09-20, 08:30 PM   #2
Kanegasi
A Firelord

Join Date: Apr 2007
Posts: 463
Define a local table at the top of your addon and use keys instead of separate variables.

For example:

Lua Code:
2. local a, b, c, d, e = 1, 2, 3, 4, 5
3.
4. -- use this
5. local vars = {}
6. vars.a, vars.b, vars.c, vars.d, vars.e = 1, 2, 3, 4, 5

You can even use the provided addon table in the second vararg return that's handed to each of your addon's files.

Lua Code:

This addon table is shared across all your Lua files under a single .toc file.

I honestly wasn't aware of a local variable cap, but I've had tables with thousands of entries in them.

Last edited by Kanegasi : 02-09-20 at 08:35 PM.

02-09-20, 08:49 PM   #3
Seerah
Fishing Trainer

Join Date: Oct 2006
Posts: 10,721
It's a limit in Lua itself, and has to do with how much room it has for local variables in one scope, iirc.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

02-09-20, 09:27 PM   #4
Walkerbo
An Aku'mai Servant

Join Date: Oct 2010
Posts: 30
Hi Kanegasi & Seerah

Thanks for the feedback and advice, both really appreciated.

02-11-20, 10:34 AM   #5
SDPhantom
A Pyroguard Emberseer

Join Date: Jul 2006
Posts: 1,906
 Originally Posted by Kanegasi Define a local table at the top of your addon and use keys instead of separate variables. For example: Lua Code: -- instead of thislocal a, b, c, d, e = 1, 2, 3, 4, 5 -- use thislocal vars = {}vars.a, vars.b, vars.c, vars.d, vars.e = 1, 2, 3, 4, 5
The entire appeal of using local variables aside from limiting scope is you save an index lookup in the global table. Putting variables in a local table completely negates this since you're re-adding the index operation you were trying to save.
__________________
 "All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools." -Anders (Dragon Age: Origins - Awakening)

02-11-20, 11:38 AM   #6
jeruku
A Cobalt Mageweaver

Join Date: Oct 2010
Posts: 214
 Originally Posted by SDPhantom The entire appeal of using local variables aside from limiting scope is you save an index lookup in the global table. Putting variables in a local table completely negates this since you're re-adding the index operation you were trying to save.
Yes, but in this case the number of local variables exceeds Lua's capacity for a single scope. I mean, if you're using 200+ local variables a table can be upvalued no differently and though table lookups aren't as fast as a local variable it's not using 200+ local variables.
__________________
"I have not failed, I simply found 10,000 ways that did not work." - Thomas Edison

02-11-20, 03:41 PM   #7
Seerah
Fishing Trainer

Join Date: Oct 2006
Posts: 10,721
At least it's a local table and not a global one.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

02-12-20, 08:12 AM   #8
SDPhantom
A Pyroguard Emberseer

Join Date: Jul 2006
Posts: 1,906
There are still better options. Making sure you put local variables only in the scope they're being used (you can force a scope if you need using a do ... end block). Limiting what functions you make local references of. Etc.
__________________
 "All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools." -Anders (Dragon Age: Origins - Awakening)

02-12-20, 04:23 PM   #9
Seerah
Fishing Trainer

Join Date: Oct 2006
Posts: 10,721
I agree. It's possible that the code could be optimized.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

 WoWInterface » More than 200 local variables

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off