Quantcast More than 200 local variables - WoWInterface
Thread Tools Display Modes
02-09-20, 05:41 PM   #1
Walkerbo
An Aku'mai Servant
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 36
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?
  Reply With Quote
02-09-20, 08:30 PM   #2
Kanegasi
A Firelord
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 479
Define a local table at the top of your addon and use keys instead of separate variables.

For example:

Lua Code:
  1. -- instead of this
  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:
  1. local addonName, addonTable = ... -- the very first line of your addon file

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.
  Reply With Quote
02-09-20, 08:49 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,727
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

  Reply With Quote
02-09-20, 09:27 PM   #4
Walkerbo
An Aku'mai Servant
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 36
Hi Kanegasi & Seerah

Thanks for the feedback and advice, both really appreciated.
  Reply With Quote
02-11-20, 10:34 AM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,918
Originally Posted by Kanegasi View Post
Define a local table at the top of your addon and use keys instead of separate variables.

For example:

Lua Code:
  1. -- instead of this
  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
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.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
02-11-20, 11:38 AM   #6
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 214
Originally Posted by SDPhantom View Post
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
  Reply With Quote
02-11-20, 03:41 PM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,727
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

  Reply With Quote
02-12-20, 08:12 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,918
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.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
02-12-20, 04:23 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,727
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

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » More than 200 local variables

Thread Tools
Display Modes

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