Author Topic: Tip: Function at line has more than 60 upvalues  (Read 3757 times)

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Tip: Function at line has more than 60 upvalues
« 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)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #1 on: September 13, 2016, 08:48:41 am »
Why would you ever need a function with 60 upvalues???

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Tip: Function at line has more than 60 upvalues
« Reply #2 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.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #3 on: September 13, 2016, 09:22:29 am »
In your examples there are repetitive values so you should use an array anyway.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #4 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
« Last Edit: September 15, 2016, 02:51:38 pm by MetalZelda »

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Tip: Function at line has more than 60 upvalues
« Reply #5 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.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #6 on: September 13, 2016, 06:02:56 pm »

Diarandor

  • Hero Member
  • *****
  • Posts: 1053
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #7 on: September 13, 2016, 07:46:08 pm »
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.”

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Tip: Function at line has more than 60 upvalues
« Reply #8 on: September 13, 2016, 07:53:32 pm »
Christopho made my day! I do not laugh often.  :D

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #9 on: September 15, 2016, 01:44:20 pm »
lèl

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

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
« Last Edit: September 15, 2016, 01:51:37 pm by MetalZelda »

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Tip: Function at line has more than 60 upvalues
« Reply #10 on: September 15, 2016, 02:11:22 pm »
Post all the solutions you want. This is a "Tip" post.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #11 on: September 15, 2016, 02:22:11 pm »
He already did above, it takes 10 lines :)

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #12 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

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Tip: Function at line has more than 60 upvalues
« Reply #13 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.

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #14 on: September 15, 2016, 10:27:43 pm »
He already did above, it takes 10 lines :)

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

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.
« Last Edit: September 15, 2016, 10:31:13 pm by zutokaza »