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 Also I can define variables using Code:
local a, b, c, d, e = 1, 2, 3, 4, 5 |
Define a local table at the top of your addon and use keys instead of separate variables.
For example: Lua Code:
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. |
It's a limit in Lua itself, and has to do with how much room it has for local variables in one scope, iirc.
|
Hi Kanegasi & Seerah
Thanks for the feedback and advice, both really appreciated. |
Quote:
|
Quote:
|
At least it's a local table and not a global one. ;)
|
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.
|
I agree. It's possible that the code could be optimized.
|
Hi all
Sorry for the necro thread but I like to keep the same subjects together instead of creating multiple threads. Some follow up questions. I have hit the 200 limits again as I have implemented new features and functionality. Should functions all be local as well or is it better to use complicated/unusual names to ensure there is no cross-pollination? is the same for frame names as well? Is it better to have functions and frames to be in separate lua files? Cheers |
It's 200 local variable per function, correct? So if you exceeded it you probably have a gigantic, unwieldy function that can be split into simpler functions. Keep in mind that the top level of a Lua file is also a function.
|
Hi Zlodo
I don't have locals inside functions as each variable exists within its function. It is each frame, button and function that are local; I have a few workarounds such as having frame and button names in a table, eg; Lua Code:
I was just wondering if there is a better way to approach this issue. Further, could I do the same thing with functions? Lua Code:
I have tried this with no success, yet I am not sure if this is a correct way of using functions in a table or not. |
Lua Code:
|
Quote:
Lua Code:
Just do: Lua Code:
|
Or create single frames with their component parts as keys on the frame.
Lua Code:
|
I think your code is severely flawed if you have 200 local variables in a single scope. You should read tutorials on how Lua works, especially I think you'll find tables interesting, and some tutorials on programming in general.
|
Hi all
@MunkDev; I can almost guarantee that my code is flawed and likely very inefficient; I am self-taught with no background in coding at all. :p @Seerah; thanks for detailing how the table laid out, this was extremely helpful as it really showed how I was using some tables wrong. @Fizzlemizz; thanks for the example code, this put Seerah's explanation into action and has been a massive help in the way I approach tables and frames. Just a follow-up question; Is this chunk; Lua Code:
Lua Code:
In the first chunk, I add the button and the button properties directly to the frame. In the second I add the button to the frame and then add the properties to the button itself. Both chunks seem to work but I do not know which one is the proper/correct way of coding this. |
Lua Code:
frameA is a local frameAButton1 is a global I added quotes around the frame names as without them, it would just be the same as using nil unless as variables they had been initialised earlier. As someone once told me, frames are just special types of tables. Lua Code:
Even: Lua Code:
Lua Code:
This being the case, Code:
local frameA = CreateFrame("Frame", "frameA", UIParent) "frameA" would be rubbish as a frame name. Being created as a global there's too much chance of a clash (I forget how it woks but I'm pretty sure first frame with a name is added to the global table, any others with the same name are ignored). |
Hi all
@Fizzlemizz Thanks for your more detailed explanation, the simple statement of "As someone once told me, frames are just special types of tables." makes things crystal clear for me. :) Thanks to all for your help. Cheers |
All times are GMT -6. The time now is 04:09 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI