What is the maximum lua can take

if you are hitting lua max issues then the issue is yours not lua and it is time to refactor your code.

the only exception to this rule is the amount of functions per DEX - which is usually eaten up by plugins - especially ad networks.

would using parameters instead of local variables affect the chunk?

ex.

local personpic ------->  person.pic

**UPDATE**  This post has been updated to fix some errors that @davebollinger corrected/pointed out.  

Thanks Dave!

@Beluga,

This is essentially how it works:

-- This is a module file as an example -- local m = {} local bob = 1 local bill = 2 -- At this point, and in this scope there are three(3) locals -- m, bob, bill function m.doit1() -- Zero (0) locals end function m.doit2() print( bob ) -- Zero (0) locals -- One upvalue: bob end function m.doit3( arg1, arg2, arg3, arg4 ) -- Four(4) locals: arg1, arg2, arg3, arg4 end function m.doit4( params ) -- One (1) local: params -- This is true, event if doit4() is later called like this: -- ???.doit3( { arg1 = "a", arg2 = "b", arg3 = "c", arg4 = "d" } ) end function m:doit5( params ) -- notice use of colon notation local sue = bill print( bob ) -- Three (3) locals: this, params, sue -- Two (2) upvalues: bill, bob end return m

In the code above, you have these chunks:

  • The entire file
  • doit1()  … doit5()

Yes, chunks can contain sub-chunks.

https://www.lua.org/pil/1.1.html

Tip: Take some time to read the these chapters of the PIL:

  • 1…8
  • 11, 12
  • 18 … 23

It will help you a lot with understanding Lua and writing better code.

Basically, a chunk is a block that has been successfully compiled and can be called. The formal details can be found here in the 5.1 docs.

So a block like

local x = 4 print(x + 7)

gets run through loadstring(), or as a file through loadfile() or require(), and basically becomes

local function Chunk (...)   local x = 4   print(x + 7) end

With require() and main.lua you never actually see this, since the require() machinery and Corona, respectively, call the function behind the scenes. But with the other two you can dig into it. For instance:

local chunk = loadstring[[local a, b = ... -- gets 7, "dog" function WhatAnimal () return b end return a + 3]] if chunk then local how\_many = chunk(7, "dog") print(how\_many, WhatAnimal()) end

Some of the limits such as maximum number of variables are defined in luaconf.h in the Lua source. (If you dig around in the Native directory a bit, you’ll find Corona’s slightly modified version; I don’t think it’s touched those particular values, though.) As far as the “must be less than 250” comments, while I can’t say for sure, I imagine that keeps indices to them in 8 bits while reserving a few special values. I do know Lua’s authors are very concerned about keeping data structures tight.

For anybody curious about Lua’s code, the author of LuaJIT has a recommended reading order.

Ah thanks for all that information for chunks.

As chunks have a maximum of 250 local variables, would using paramaters help?

ex.

local personpic ------->  person.pic

  1. Why do you keep asking the same question?

  2. What does it mean?  That line of code is NOT a parameter table.  Is a local variable, which adds to your local count.

Please go back and more carefully read my long and detailed reply showing you how many locals are in scope at various times.

  1. I would stick to 200 or fewer locals actually.  I think that is safer.

I feel like theses questions have been answered:

  • Maximum allowed arguments passed as parameters to a function?  <= 60
  • (Safe) Maximum allowed locals in any specific scope?  <= 200
  • How does passing a table of named parameters affect the local count?  See m.doit3() in my clearly coded example.

If you still have a question related to local counts, passing named parameter tables, etc.

Please ask it more clearly and concisely, and don’t just re-ask the same question.

Right well nobody answered my question earlier.

I understand the chunks part but all I’m asking whether parameters add up the number of local variables used.

By parameter I assume you mean a value passed to a function?  

Limits are there not to get in your way, but to make you code properly.

So instead of

local a = 1 local b = 2 local c = 3

you have

local values = {1,2,3}

The first uses 3 “slots” the second only one.

Like I said earlier, if you are hitting hard limits you need to refactor out to smaller modules.

every time you reference something in a more-outer scope it will count against the 60 limit, fe

local a,b,c,d local foo = function() return 7 end local bar = function() a = b + c -- 3 here d = foo() -- 2 more end -- 5 upvalues total

@BelugaWhale It looks like they must: parlist

When a parameter name is parsed it does that new_localvar() call, which is what’s enforcing the limit. (And the count is indeed 8 bits. Also looks like is one of the “special values”.)

But to echo the others, if you get to this point, time to clean things up a bit.  :slight_smile:

the 200 limit is for “locals” - local to a module, local to a function, local to a block, … whatever.

the 60 limit is upvalues per closure (ie “# of references to outer-scopes”)

both of these limits are far more than enough for well-structured code.

local a0,a1,a2,a3,a4,a5,a6,a7,a8,a9 local b0,b1,b2,b3,b4,b5,b6,b7,b8,b9 local c0,c1,c2,c3,c4,c5,c6,c7,c8,c9 local d0,d1,d2,d3,d4,d5,d6,d7,d8,d9 local e0,e1,e2,e3,e4,e5,e6,e7,e8,e9 local f0,f1,f2,f3,f4,f5,f6,f7,f8,f9 local g0 -- keep going til 200 of these to break the local limit function foobar() a0 = a1 or a2 or a3 or a4 or a5 or a6 or a7 or a8 or a9 b0 = b1 or b2 or b3 or b4 or b5 or b6 or b7 or b8 or b9 c0 = c1 or c2 or c3 or c4 or c5 or c6 or c7 or c8 or c9 d0 = d1 or d2 or d3 or d4 or d5 or d6 or d7 or d8 or d9 e0 = e1 or e2 or e3 or e4 or e5 or e6 or e7 or e8 or e9 f0 = f1 or f2 or f3 or f4 or f5 or f6 or f7 or f8 or f9 -- just one more will break the upvalue limit: g0 = 0 end

Thanks to everyone!

I finally get it.

I would like each of these posts but I have a limit.

As said by roaming gamer i will read the guides.

Thanks a bunch!

Prior to this post I was unaware of these limits so I’ve learned something tonight.

Now considering I have large and complicated games… maybe I do actually know a thing or two about coding… who knew?

Note: this is a light-hearted comment

_ “in this scope” _ (ie, the scope of function m.doit1) there are _ no _ locals (nor upvalues)

at this point, at module scope, there are still only three locals yet defined

Perhaps also worth mentioning is that you can use do blocks to limit the lifetime of locals:

-- stuff do local a, b, c -- do stuff with a, b, c end -- left scope, so slots for a, b, c up for grabs

but I wouldn’t go too crazy with this.

Are you saying the locals from the file level scope are not visible or that they don’t count against your maximum limit within the scope of the function?

Are you saying I could have 200 locals in the file level scope and an extra 200 in the function?

In truth, I have always been a little unclear on the  upvalue concept.  Looks like I need to do more reading too.

@davebollinger,

So, locals at the file/module level are ‘upvalues’ (external locals) in the scope of the function.

Thanks!  I learned something new too!  Also, very elegant example now that I know what is going on.

I’ll update my post in a moment to properly reflect the local, upvalue, etc. terms.

they’re visible, yes, but they’re never referenced, so they’re not upvalues of the function, so don’t count against that limit (and they’re certainly not locals of the function, so don’t count against that limit either)

yes - you can have 200 at module level and another 200 at function level

OK, I updated the example and I believe I have it right this time.

Thanks again @davebollinger!