## Tip: Function at line has more than 60 upvalues

Started by Zefk, September 13, 2016, 03:13:13 AM

##### September 13, 2016, 03:13:13 AMLast Edit: October 05, 2016, 05:11:34 AM by Zefk
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)

##### September 13, 2016, 08:48:41 AM #1
Why would you ever need a function with 60 upvalues???

##### September 13, 2016, 09:15:14 AM #2 Last Edit: September 13, 2016, 09:19:00 AM by Zefk
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.

##### September 13, 2016, 09:22:29 AM #3
In your examples there are repetitive values so you should use an array anyway.

##### September 13, 2016, 04:26:56 PM #4 Last Edit: September 15, 2016, 02:51:38 PM by MetalZelda
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] = 0end local function Update(event)  for i = 1, #vars do    print(vars)   endendreturn vars`

Declaring these thing 1 by one is a waste of time
But yeah, 60 upvalues is completely crazy

##### September 13, 2016, 05:26:18 PM #5
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.

##### September 13, 2016, 07:46:08 PM #7
I agree with the bear.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

##### September 13, 2016, 07:53:32 PM #8
Christopho made my day! I do not laugh often.

##### September 15, 2016, 01:44:20 PM #9 Last Edit: September 15, 2016, 01:51:37 PM by MetalZelda
lèl

I did not really understand what OP want until now

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.

There are faster solutions, the one you are saying is the most time wasting

##### September 15, 2016, 02:11:22 PM #10
Post all the solutions you want. This is a "Tip" post.

##### September 15, 2016, 02:22:11 PM #11
He already did above, it takes 10 lines

##### September 15, 2016, 02:32:36 PM #12
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.

##### September 15, 2016, 10:27:27 PM #13
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.

##### September 15, 2016, 10:27:43 PM #14 Last Edit: September 15, 2016, 10:31:13 PM by zutokaza
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.

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.
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616