Solarus-Games English Forum

Solarus => Development => Topic started by: Zefk on September 13, 2016, 03:13:13 AM

Title: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 13, 2016, 03:13:13 AM
I had more than 60 upvalues and got an error. A lua limit. Upvalues are variables declared outside of a particular function. I solved it with a table. This solved the 200 Local Variables Limit Problem as well.

Example:
https://forums.coronalabs.com/topic/6648-function-at-line-has-more-than-60-upvalues/?p=187272

Declaring over 60 arrays gives an over 60 upvalue error. Put the arrays in a table.

local yellow ={
      num1_place ={}
      num2_place ={}
      etc
}

yellow.num1_place[1] = 0
yellow.num1_place[2] = 0
etc

yellow.num2_place[1] = 0
yellow.num2_place[2] = 0
etc


Other interesting solutions:
I have not tested them, but go for it.
https://forums.coronalabs.com/topic/19555-solved-200-local-variables-limit-problem/ (Function table)
https://ask.wireshark.org/questions/46598/lua-60-upvalues-limit-in-function-solved (array)
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Christopho on September 13, 2016, 08:48:41 AM
Why would you ever need a function with 60 upvalues???
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 13, 2016, 09:15:14 AM
1. The person might want to go crazyyyyyyy
2. The programmer decides to not use coordinates for numbered png images. That requires more variables usage.
3. The programmer wants a picture based ticking clock. Each tick hand is a different design. Not just color change or direction rotation for a single image.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Christopho on September 13, 2016, 09:22:29 AM
In your examples there are repetitive values so you should use an array anyway.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: MetalZelda on September 13, 2016, 04:26:56 PM
Something like this is more convenient, faster, and cleaner

Code (lua) Select
local vars = {}
local max_vars = 61 -- Maximum variable, minimum is 1, maximum is infinite

-- This is done when the script is created, it does the same thing as the example
-- Except that instead of vars.var1, it is vars[1]

for i = 1, max_vars do
  vars[i] = 0
end

local function Update(event)
  for i = 1, #vars do
    print(vars)
  end
end

return vars


Declaring these thing 1 by one is a waste of time
But yeah, 60 upvalues is completely crazy
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 13, 2016, 05:26:18 PM
I can declared a thousand in a few minutes with copy, paste, search, and replace. Not much time in my opinion, but I do not see me ever needing that much. It all depends on how one wants to design their code.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Christopho on September 13, 2016, 06:02:56 PM
(http://memesvault.com/wp-content/uploads/How-About-No-13.jpg)
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Diarandor on September 13, 2016, 07:46:08 PM
I agree with the bear.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 13, 2016, 07:53:32 PM
Christopho made my day! I do not laugh often.  :D
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: MetalZelda on September 15, 2016, 01:44:20 PM
lèl

I did not really understand what OP want until now :P

Quote from: Zefk on September 13, 2016, 05:26:18 PM
I can declared a thousand in a few minutes with copy, paste, search, and replace. Not much time in my opinion, but I do not see me ever needing that much. It all depends on how one wants to design their code.

(http://i.imgur.com/5TJ64Gr.png)

There are faster solutions, the one you are saying is the most time wasting
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 15, 2016, 02:11:22 PM
Post all the solutions you want. This is a "Tip" post.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Christopho on September 15, 2016, 02:22:11 PM
He already did above, it takes 10 lines :)
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: MetalZelda on September 15, 2016, 02:32:36 PM
There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on September 15, 2016, 10:27:27 PM
That is what comments are for and I name my variables in a way that I can find them and modify all of them in seconds. Code is easy to follow for those with lots of practice. Plus gedit and most text editors have search options for people that have trouble with finding a variable. Never happened to me though.  I do not remember the last time I had trouble finding a variable.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: zutokaza on September 15, 2016, 10:27:43 PM
Quote from: Christopho on September 15, 2016, 02:22:11 PM
He already did above, it takes 10 lines :)

He meant to name 'more' solutions if he wanted.

Quote from: MetalZelda on September 15, 2016, 02:32:36 PM
There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P

Your solution is good, but like Zefk I like to give my variables good names. I would not use an array for good variable naming. I also like to see my variable list and not a few listed arrays for name changes. Your solution depends on many factors or whether the variables are related to each other. The table solution is flexible. Especially, when you are designing your code. I will not know how many variables I will end up with in a complex project.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: MetalZelda on September 21, 2016, 11:00:54 AM
Quote from: zutokaza on September 15, 2016, 10:27:43 PM
Quote from: Christopho on September 15, 2016, 02:22:11 PM
He already did above, it takes 10 lines :)

He meant to name 'more' solutions if he wanted.

Quote from: MetalZelda on September 15, 2016, 02:32:36 PM
There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P

Your solution is good, but like Zefk I like to give my variables good names. I would not use an array for good variable naming. I also like to see my variable list and not a few listed arrays for name changes. Your solution depends on many factors or whether the variables are related to each other. The table solution is flexible. Especially, when you are designing your code. I will not know how many variables I will end up with in a complex project.

Oh then it is simple, there is a solution that looks almost the same as I did above

Code (lua) Select
local vars = { name1 = value, name2 = value, etc, etc}
 
local function Update(event)
  for i = 1, #vars do
    print(vars[i])
  end
end


It does what you want, it print each value, because theorically, "name1" is the same thing as vars[1]
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Diarandor on September 21, 2016, 01:47:03 PM
Maybe I am wrong, but I think in this case it should be:
Code (Lua) Select
print(vars["name"..i])

Another way to do the loop is using the Lua "pairs" function and print both things, key and value, on each line (I think the order for the loop is random, or at least chaotic, but I am not sure). This would allow to have keys that are not of the form "nameX".
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on October 04, 2016, 05:01:01 AM
I normally use arrays when things are related to the same thing.

Lets say I have a image with a "1" on it. I might want to draw many at once and manipulate each one to a different position.

I would use an array like this

Code ( lua) Select
local num1_place = {}

if num1_place[1] == false then
  blah_img:draw(screen, 23)
end

if num1_place[2] == false then
  blah_img:draw(screen, 13)
end


etc

instead of listing bunch of variables

Code ( lua) Select
local num1_place_1 = false
local num1_place_2 = false
local num1_place_3 = false
local num1_place_4 = false
local num1_place_5 = false
local num1_place_6 = false
local num1_place_7 = false
local num1_place_8 = false
local num1_place_9 = false
local num1_place_10 = false
local num1_place_11 = false
local num1_place_12 = false
local num1_place_13 = false
local num1_place_14 = false
local num1_place_15 = false


I guess I could of used an array, but wanted to be picky with my variable names.
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: llamazing on October 04, 2016, 05:27:44 AM
Quote from: Zefk on October 04, 2016, 05:01:01 AM
I normally use arrays when things are related to the same thing.

Lets say I have a image with a "1" on it. I might want to draw many at once and manipulate each one to a different position.

I would use an array like this...
Ouch! Why are you drawing each number individually when you can make your life easier and use a text_surface to do the same thing?
Title: Re: Tip: Function at line has more than 60 upvalues
Post by: Zefk on October 04, 2016, 06:08:01 AM
Most of my fonts are not in any font package format. They are all bitmap fonts divided on separate png files. Plus I like doing it this way.  Well anyway, this is a topic for a different post.