WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   More than 200 local variables (https://www.wowinterface.com/forums/showthread.php?t=57812)

Walkerbo 02-09-20 05:41 PM

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?

Kanegasi 02-09-20 08:30 PM

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.

Seerah 02-09-20 08:49 PM

It's a limit in Lua itself, and has to do with how much room it has for local variables in one scope, iirc.

Walkerbo 02-09-20 09:27 PM

Hi Kanegasi & Seerah

Thanks for the feedback and advice, both really appreciated.

SDPhantom 02-11-20 10:34 AM

Quote:

Originally Posted by Kanegasi (Post 335102)
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.

jeruku 02-11-20 11:38 AM

Quote:

Originally Posted by SDPhantom (Post 335113)
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.

Seerah 02-11-20 03:41 PM

At least it's a local table and not a global one. ;)

SDPhantom 02-12-20 08:12 AM

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.

Seerah 02-12-20 04:23 PM

I agree. It's possible that the code could be optimized.


All times are GMT -6. The time now is 06:40 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI