Tip: Function at line has more than 60 upvalues

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

Previous topic - Next topic
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]

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."

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.

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?

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.