What is the maximum lua can take

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!

fwiw, for those whose eyes gloss over when they start seeing words like “closure”, “environment”, “upvalue”, etc…

an upvalue is a variable that has been bound to the environment of a function.

great, but what the heck does THAT mean??  :D  ie, the answer is more confusing than the question, right?

functions in Lua are first-class citizens, and may “travel” outside of the scope in which they were defined.  in order to continue working outside of their defining scope, they need to retain their original environment.  this combination of a function with its environment is called a closure.  all functions in Lua are closures, though they may not necessarily need/use their environment, because they may not actually reference any variables outside of their local scope.  any variables from outer scopes that ARE referenced and DO need to be bound into the closure environment are called upvalues.

example:  ponder the following code, and once you “get” how ‘n’ is “remembered” then you’ll have a workable understanding

print("at this point 'n' does not exist", n) local function makeSquaringFunction(n) print("at this point 'n' is a local of function makeSquaringFunction", n) return function() print("at this point 'n' is an upvalue of the returned squaring function", n) return n\*n end end print("at this point 'n' does not exist (again)", n) local threeSquared = makeSquaringFunction(3) print("at this point 'n' does not exist (still)", n) print("three squared = ", threeSquared()) -- WHAT?!?! -- ie, how can it possibly still "know" that n = 3?!?! that seems like "voodoo"!!!

hth

Simple solution is put variables in tables.

One table = one variable, no matter how many values or sub-tables contained within.

As for lines of code, I wouldn’t worry about line number limits, and would encourage you to split your code into multiple small files each doing different jobs.

The max 60 upvalues limit is per function so it’s not really an issue even if you dont use tables.

@Beluga,
 
Don’t do this:

local function poorWay( arg1, arg2, arg3, ..., argN ) end

Do it this way:

local function betterWay( stdArg1, stdArg2, params ) params = params or {} local someArg1 = params.someArg1 or "some default" local someArg2 = params.someArg2 or "some default" ... end

Later called like this:

betterWay( 10, 20, { someArg1 = "Bob", someArg2 = "Bill", coins = 15 } )

You will notice, this is the way most SSK builders work:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/#the-ssk-way

Hi,

Use the module pattern and abstract your code functionality where appropriate. And as @nick_sherman pointed out, utilize tables (which is pretty much what modules are anyway).

-dev

Even using tables I struggle to get why any function needs that many inputs?

I max at around 5 params per function call.

The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.

Rob

[quote name=“Rob Miracle” post=“384325” timestamp=“1529623644”]The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.   Rob[/quote] Sorry but what do you mean “chunk”?

Lua uses the term “chunk” to mean any one block of code.  A for loop for instance is a chunk, and If-then-end block is a chunk. But more importantly the top level of a module is it’s self a chunk.  Since you’re very likely not going to have 200 local variables inside an if-then-end block it’s only really a problem at the whole module (or main.lua) level.

Rob

Rob probably means per single .lua file