function at line xxx has more than 60 upvalues - lua

I have written some lua code like this:
local a1 = 1
local a2 = 2
local a3 = 3
local a4 = 3
local a5 = 3
local a6 = 3
local a7 = 3
local a8 = 3
local a9 = 3
local a10 = 3
local a11 = 3
local a12 = 3
local a13 = 3
local a14 = 3
local a15 = 3
local a16 = 3
local a17 = 3
local a18 = 3
local a19 = 3
local a20 = 3
local a21 = 3
local a22 = 3
local a23 = 3
local a24 = 3
local a25 = 3
local a26 = 3
local a27 = 3
local a28 = 3
local a29 = 3
local a30 = 3
local a31 = 1
local a32 = 2
local a33 = 3
local a34 = 3
local a35 = 3
local a36 = 3
local a37 = 3
local a38 = 3
local a39 = 3
local a40 = 3
local a41 = 3
local a42 = 3
local a43 = 3
local a44 = 3
local a45 = 3
local a46 = 3
local a47 = 3
local a48 = 3
local a49 = 3
local a50 = 3
local a61 = 3
local a62 = 3
local a63 = 3
local a64 = 3
local a65 = 3
local a66 = 3
local a67 = 3
local a68 = 3
local a69 = 3
local a70 = 3
local function fun1(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
print('.......')
end
local function fun2()
fun1(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
fun1(a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
fun1(a21, a22, a23, a24, a25, a26, a27, a28, a29, a30)
fun1(a31, a32, a33, a34, a35, a36, a37, a38, a39, a40)
fun1(a41, a42, a43, a44, a45, a46, a47, a48, a49, a50)
fun1(a51, a52, a53, a54, a55, a56, a57, a58, a59, a60)
fun1(a61, a62, a63, a64, a65, a66, a67, a68, a69, a70)
end
and i got an error like this when ran this code:
78: function at line 71 has more than 60 upvalues
i know this kind of code is ugly and i can use some way else to do it (like table), but this kind of code may be written by my users.
can someone explain this for me, and tell me how to avoid this? thanks very much.

tell me how to avoid this?
There's the old joke. A guy walks into a doctor's office and says, "It hurts when I raise my arm like this." The doctor says, "So don't raise your arm like that."
If your users write Lua code that does not compile, that's not something you can fix. This is no more illegitimate a compile error than something like:
if condition --forgot the then
return something
end
So if your code is going to accept arbitrary Lua scripts to compile and execute, it will need to be able to deal with Lua scripts that don't compile. For whatever reason.
If compilation fails, report the error to the user and recover the best you can.

Maximum 60 upvalues, that is values from outer scopes that your closure is closed over, is one of internal Lua limits. Of course you can change it by recompiling Lua itself, but I'd advise against it. Pack your values in some table instead, with its layout dictated by logic of code. In your particular example you really should be using:
local a = {}
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 3
a[5] = 3
-- etc...

Related

Lua char spacing perfectly

Failed Spacing
I'm trying to get all of these names with a max char of 31 to line up all together in the same row by adding the number of spaces per player name that it needs. I've been trying to accomplish this for some time now and I just can't figure this out completely.
This is my current code which is a disaster I know..
local c = client.GetPlayerNameByIndex(i)
if c ~= nil and client.GetPlayerNameByIndex(i) ~= "nil" then
playerlist[i] = all_trim(client.GetPlayerNameByIndex(i))
local leve = 67
local namelength = #client.GetPlayerNameByIndex(i) --max 31 chars
local output = "" .. client.GetPlayerNameByIndex(i)
local newspace = ""
local neededspaces = 31 - #client.GetPlayerNameByIndex(i)
print(neededspaces)
for i=1, 31-#string.sub(client.GetPlayerNameByIndex(i), 1, 31) do
newspace = newspace .. " "
end
playerinfolist[i] = output .. newspace .. "a"
end
In simple terms I want all of the "a"s to line up with each string. Thanks for helping me!

Why does the code in the description not work, it is meant to change the rgb value gradually

This is in Roblox Lua by the way. If you are wondering what the result is, it is just a black block that doesn't change color.
local part = Instance.new("Part")
part.Parent = game.Workspace
local maxRGBValue = 99*3
local r = 0
local g = 0
local b = 0
while r + g + b ~= maxRGBValue do
wait(1)
local r = r + 10
local g = g + 10
local b = b + 10
part.Color = Color3.fromRGB(r,g,b)
end
local r = 0
local g = 0
local b = 0
The local syntax creates a brand new variable every time the program reaches that statement. So you actually have three different variables named r, three named g, and three named b, essentially as though the code were:
local part = Instance.new("Part")
part.Parent = game.Workspace
local maxRGBValue = 99*3
local r1 = 0
local g1 = 0
local b1 = 0
while r1 + g1 + b1 ~= maxRGBValue do
wait(1)
local r2 = r1 + 10
local g2 = g1 + 10
local b2 = b1 + 10
part.Color = Color3.fromRGB(r2,g2,b2)
end
local r3 = 0
local g3 = 0
local b3 = 0
So the values of r1, g1, and b1 never change from the initial zero values, r2, g2, and b2 are always exactly 10 every time, and variables r3, g3, and b3 are useless.
You should generally put local only at the first mention of each variable. Also, if you want the variable's value to stay what it was over different iterations of a loop or between different calls to a function, make sure that local declaration is before that loop or function.
So probably better (but not tested):
local part = Instance.new("Part")
part.Parent = game.Workspace
local maxRGBValue = 99*3
local r = 0
local g = 0
local b = 0
while r + g + b < maxRGBValue do
wait(1)
r = r + 10
g = g + 10
b = b + 10
part.Color = Color3.fromRGB(r,g,b)
end
I also changed ~= maxRGBValue to < maxRGBValue. The loop wouldn't ever terminate using the check for equality, since r+g+b will step from 90*3 to 100*3 and is never equal to 99*3.

How to arrange picture in lua like a grid?

I'm learning lua and I want to arrange my bubble picture with some specific x and y coordinates, here's my code so far, the value of my j and i is only incrementing by 1 instead of the +29, I know I'm lacking some knowledge so any help will be appreciated
local background = display.newImageRect("blueBackground.png",642, 1040)
background.x = display.contentCenterX
background.y = display.contentCenterY
local x = 15
local y=15
for i=15,25 do
for j=15, 25 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
j = j + 29
print("j",j)
end
i = i + 29
print("i",i)
end
This should helps you.
From Lua documentation
The for statement has two variants: the numeric for and the
generic for.
A numeric for has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1
to exp2, using exp3 as the step to increment var. This third
expression is optional; when absent, Lua assumes one as the step
value. As typical examples of such loops, we have
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
Use
for i=15, 29*10+15, 29 do
for j=15, 29*10+15, 29 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
print("j",j)
end
print("i",i)
end
or
for i=0, 10 do
for j=0, 10 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = 15 + i * 29
bubble.y = 15 + j * 29
...

Convert Table string to the actual table

local t = "{{2173,1},{2160,5}}"
print(#t) -- 19?? wrong
How to convert to appear in numbers?
local t = {{2173,1},{2160,5}}
print(#t) -- 2 correct
You can run the string through load or loadstring (depending on your Lua version), which will return the table you are looking for:
local t = "{{2173,1},{2160,5}}"
t = (loadstring or load)("return "..t)()
print(#t) -- 2
Here is an ad hoc solution for the input you gave:
local s = "{{2173,1},{2160,5}}"
local t = {}
local n = 0
for a,b in s:gmatch("(%d+),(%d+)") do
n = n + 1
t[n] = {a,b}
end
for k,v in ipairs(t) do print(k,v[1],v[2]) end

Having an id for images in Lua (Corona SDK)

I'm trying to make a game similar to candy crush in Lua. Here is the code:
local images = {
"images/beer.png",
"images/beef.png",
"images/canned_food.png",
"images/cup_ice_cream.png",
"images/french_fries.png",
"images/pepper.png"
}
local rowcount = 8
local colcount = 4
local blockWidth = display.contentWidth / (colcount*4)
local blockHeight = display.contentWidth / (rowcount*2)
local row
local col
local pan = 3
for row = 1, rowcount do
for col = 1, colcount do
local x = (col - 1) * blockWidth + pan
local y = (row + 1) * blockHeight + pan
local block = display.newImage(images[math.random(1, 6)], x, y)
block:addEventListener("touch", blockTouch)
end
end
I need to know which image is moving, to know if with the new position they made 3 in a line.
So my question is, how can i have an id or a identifier to know which image the user is moving in the table?
Thanks for your help
you must put ID in each object you create for example:
local function getID(event)
t = event.target
print(t.id)
end
local beef = display.newImage("images/beef.png",)
beef.id = "beef"
local canned_food= display.newImage("images/canned_foods.png",)
canned_food.id = "cannedfoods"
local fries = display.newImage("images/fench_fries.png",)
fries.id = "fries"
beef:addEventListener("tap", getID())
canned_food:addEventListener("tap", getID())
fries:addEventLister("tap", getID())
hopes this helps :)
I would put your blocks into a table to keep track of each of them. But to answer your specific question, Lua allows you to add any method or attribute to an object, so you can do:
block.name = "Beer"
block.color = "Green"
block.gobbldygook = 400
Then in your tap/touch handler, your "event.target" is the object, so you can say:
print(event.target.gobbldygook)

Resources