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

#### MetalZelda

• Hero Member
• Posts: 551
##### Re: Tip: Function at line has more than 60 upvalues
« Reply #15 on: September 21, 2016, 11:00:54 am »
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.

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])   endend`
It does what you want, it print each value, because theorically, "name1" is the same thing as vars[1]

#### Diarandor

• Hero Member
• Posts: 1061
• Cats are cool! （ΦωΦ）
##### Re: Tip: Function at line has more than 60 upvalues
« Reply #16 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".
“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
##### Re: Tip: Function at line has more than 60 upvalues
« Reply #17 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)endif 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 = falselocal num1_place_2 = falselocal num1_place_3 = falselocal num1_place_4 = falselocal num1_place_5 = falselocal num1_place_6 = falselocal num1_place_7 = falselocal num1_place_8 = falselocal num1_place_9 = falselocal num1_place_10 = falselocal num1_place_11 = falselocal num1_place_12 = falselocal num1_place_13 = falselocal num1_place_14 = falselocal num1_place_15 = false`
I guess I could of used an array, but wanted to be picky with my variable names.

#### llamazing

• Full Member
• Posts: 210
##### Re: Tip: Function at line has more than 60 upvalues
« Reply #18 on: October 04, 2016, 05:27:44 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?

#### Zefk

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